Shiny not displaying my ggplot as I'd expect

后端 未结 3 1696
悲哀的现实
悲哀的现实 2021-01-11 15:39

What\'s keeping my little Shiny app from displaying my ggplot? When I replace the code in renderPlot() with an example using the base plot function it comes together. I\'m

3条回答
  •  臣服心动
    2021-01-11 16:22

    Your code needed a couple of changes to get ggplot to render. As the comments above state, print(ggplot) was needed. But also, aes inside ggplot can't deal with subsetting.

    So you subset your city of interest in a separate reactive, and call that from 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())
    

    The full server.R (this works)

    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])
    
    
      })
    })
    

提交回复
热议问题