2013-08-04 148 views
10

什么让我的小Shiny应用程序不显示我的ggplot?当我用一个使用基本绘图函数的例子替换renderPlot()中的代码时,它将汇集在一起​​。我在Windows Vista上使用RStudio,R v3.0.1,输出到Chrome浏览器。闪亮不显示我的ggplot因为我期望

ui.r

library(ggplot2) 

cities <- c("Anchorage","Fairbanks","Juenau","Wasilla","Homer") 
years <- 2003:2013 
Table <- "Capital Assets" 
Account <- c("Land", "Art", "Buildings", "Equipment") 
dat <- data.frame(City = sort(rep(cities, length(years))), Year = rep(years,length(cities)), Table) 
sampleDat <- rbind(data.frame(dat,Acount = Account[1]), data.frame(dat, Acount = Account[2]), data.frame(dat, Acount = Account[3]), data.frame(dat, Acount = Account[4])) 
finalDat <- data.frame(sampleDat, Value = runif(length(sampleDat[,1]), 1000,10000)) 

shinyUI(pageWithSidebar(

    headerPanel("CAFR Explorer"), 

    selectInput("city","City", as.list(levels(finalDat$City)), selected = NULL, multiple = FALSE), 

    mainPanel(
    h3(textOutput("caption")), 

    plotOutput("CAFRplot") 
))) 

server.r

library(shiny) 
library(ggplot2) 

cities <- c("Anchorage","Fairbanks","Juenau","Wasilla","Homer") 
years <- 2003:2013 
Table <- "Capital Assets" 
Account <- c("Land", "Art", "Buildings", "Equipment") 
dat <- data.frame(City = sort(rep(cities, length(years))), Year = rep(years,length(cities)), Table) 
sampleDat <- rbind(data.frame(dat,Acount = Account[1]), data.frame(dat, Acount = Account[2]), data.frame(dat, Acount = Account[3]), data.frame(dat, Acount = Account[4])) 
finalDat <- data.frame(sampleDat, Value = runif(length(sampleDat[,1]), 1000,10000)) 

shinyServer(function(input, output) { 

    formulaText <- reactive({ 
    paste(input$city) 
    }) 

    output$caption <- renderText({ 
    formulaText() 
    }) 

    output$CAFRplot <- renderPlot({ 

    ## this one isn't working. 
    ggplot(finalDat, aes(x = finalDat[which(finalDat$City == input$city),2], 
         y = finalDat[which(finalDat$City == input$city),5])) + 
    geom_point() 

    ## this one is working 
    #plot(finalDat[which(finalDat$City == input$city),2], y = finalDat[which(finalDat$City == input$city),5]) 


    }) 
}) 
+3

尝试包装你ggplot在'print'即'打印(ggplot(...)+ geom_point)调用' –

+0

你应该表现出错误消息,而不是仅仅说“不起作用”。杰克是正确的,你应该围绕你的ggplot调用打印打印,但我认为你的ggplot调用还有其他问题(作用域问题)。 – GSee

回答

17

这里有两个问题。

首先,你不应该在aes子集 - 它期望列名称。相反,子集,您提供给ggplot(感谢来自R聊天@Roland)

秒data.frame,您必须在闪亮的应用明确print您ggplot对象。

试试这个:

p <- ggplot(finalDat[finalDat$City == input$city,], aes(x = Year, y = Value)) 
p <- p + geom_point() 
print(p) 
+1

+1漂亮,优雅。我没有看到你的回复,或者我不会输入我的。 –

3

您的代码需要一些改动让ggplot呈现。如上述评论所述,需要print(ggplot)。但是,ggplot里的aes也不能处理子集。

因此,你将你感兴趣的城市作为一个独立的被动子集进行分类,并从ggplot中调用它。

city.df <- reactive({ 
    subset(finalDat, City == input$city) 
    }) 

    output$CAFRplot <- renderPlot({ 
    city <- city.df() 

    print(ggplot(city, aes(x = Year, y=Value)) + geom_point()) 

全server.R(这工作)

library(shiny) 
library(ggplot2) 

cities <- c("Anchorage","Fairbanks","Juenau","Wasilla","Homer") 
years <- 2003:2013 
Table <- "Capital Assets" 
Account <- c("Land", "Art", "Buildings", "Equipment") 
dat <- data.frame(City = sort(rep(cities, length(years))), Year = rep(years,length(cities)), Table) 
sampleDat <- rbind(data.frame(dat,Acount = Account[1]), data.frame(dat, Acount = Account[2]), data.frame(dat, Acount = Account[3]), data.frame(dat, Acount = Account[4])) 
finalDat <- data.frame(sampleDat, Value = runif(length(sampleDat[,1]), 1000,10000)) 

shinyServer(function(input, output) { 

    formulaText <- reactive({ 
    paste(input$city) 
    }) 

    output$caption <- renderText({ 
    formulaText() 
    }) 

    city.df <- reactive({ 
    subset(finalDat, City == input$city) 
    }) 

    output$CAFRplot <- renderPlot({ 
    city <- city.df() 
    ## this one isn't working. 
# print(ggplot(finalDat, aes(x = finalDat[which(finalDat$City == input$city),2], 
#       y = finalDat[which(finalDat$City == input$city),5])) + geom_point()) 

    print(ggplot(city, aes(x = Year, y=Value)) + geom_point()) 

    ## this one is working 
    #plot(finalDat[which(finalDat$City == input$city),2], y = finalDat[which(finalDat$City == input$city),5]) 


    }) 
}) 
2

ggplot2可以子集整体数据传递到所有图层(@ GSEE的答案),或者对于蹦床网上单图层可以使用subset参数来为该图层添加子集。如果你正在构建更复杂的地块,这可能会很有用。

使用plyr功能.在这里很有构建参数

# required in server.R (along with the other calls to library) 
library(plyr) 

p <- ggplot(finalDat, aes(y =Year, x = Value)) + 
     geom_point(subset = .(City ==input$city)) 
print(p) 
+0

非常酷。感谢那额外的信息。 – cylondude

相关问题