【问题标题】:Shiny not displaying my ggplot as I'd expect闪亮的没有像我期望的那样显示我的 ggplot
【发布时间】:2013-08-05 00:37:18
【问题描述】:

是什么让我的小闪亮应用无法显示我的 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])


  })
})

【问题讨论】:

  • 尝试将 ggplot 调用封装在 printprint(ggplot(...) + geom_point)
  • 您应该显示您收到的错误消息,而不是仅仅说“不起作用”。 Jake 是正确的,您应该在 ggplot 调用周围包装 print,但我认为您的 ggplot 调用还有其他问题(范围问题)。

标签: r ggplot2 shiny


【解决方案1】:

这里有两个问题。

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

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

试试这个:

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

【讨论】:

  • +1 漂亮优雅。我没有看到你的回复,否则我不会输入我的。
【解决方案2】:

您的代码需要进行一些更改才能使ggplot 呈现。正如上述 cmets 所述,需要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])


  })
})

【讨论】:

    【解决方案3】:

    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)
    

    【讨论】:

      猜你喜欢
      • 2016-12-03
      • 1970-01-01
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 2019-12-04
      • 1970-01-01
      相关资源
      最近更新 更多