闪亮没有像我期望的那样显示我的ggplot

时间:2013-08-04 01:33:43

标签: r ggplot2 shiny

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

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 个答案:

答案 0 :(得分:18)

这里有两个问题。

首先,你不应该在aes中进行子集 - 它需要列名。相反,将您提供的data.frame子集化为ggplot(感谢来自R聊天的@Roland)

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

试试这个:

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

答案 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())

完整的服务器.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 :(得分: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)