11

是什么让我的小闪亮应用无法显示我的 ggplot?当我将 renderPlot() 中的代码替换为使用基本绘图函数的示例时,它就组合在一起了。我在 Windows Vista 上使用 RStudio,R v3.0.1,输出到 Chrome 浏览器。

用户界面

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")
)))   

服务器.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])


  })
})
4

3 回答 3

18

这里有两个问题。

首先,您不应该在aes- 它需要列名。相反,将您提供给的 data.frame 子集ggplot(感谢 R chat 中的@Roland)

其次,您必须print在闪亮的应用程序中明确显示您的 ggplot 对象。

试试这个:

p <- ggplot(finalDat[finalDat$City == input$city,], aes(x = Year, y = Value))
p <- p + geom_point()
print(p)
于 2013-08-04T01:44:19.353 回答
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])


  })
})
于 2013-08-04T17:55:06.167 回答
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)
于 2013-08-05T04:40:38.150 回答