简体   繁体   中英

Cannot plot with ggvis in Shiny App

After several hours of tinkering around and Googling, I think I might after all be at a dead end. I'm learning Shiny R and am trying to create a scatter plot using ggvis library. So far I have the following code:

app.R:

library(shiny)
library(ggvis)
library(dplyr)

# Define UI for application
ui <- fluidPage(
         p("hello world"),
         ggvisOutput("plot"),
         p("hello world")
)

# Define server logic
server <- function(input, output, session) {

   #Plot a scatter plot for employment counts
   output$plot <- reactive({

      year <- c(2011, 2012, 2013, 2014, 2015, 2016)
      plotData <- c(123456, 1234567, 234561, 213465, 465791, 222222)

      dataDF <- data.frame(Year = year, Data = plotData)
      layer_points(ggvis(dataDF, ~Year, ~Data))

   })
}

# Knit the ui and server, then run the application 
shinyApp(ui = ui, server = server)

This should be quite simple I guess but I get no plots when I run this. It just shows hello world twice with a space in between. I've replicated this using just Rstudio without the Shiny App addons, and it seemed to graph fine! I'm following the code here and obviously I tried to simplify the work. I've also been following this link and unfortunately no luck.

Would anyone have any ideas? Really appreciate the help.

From ?ggvis::ggvisOutput :

Server-side

When you run ggvis plot interactively, it is automatically plotted because it triggers the default print method. In shiny apps, you need to explicitly render the plot to a specific placeholder with bind_shiny :

p %>% bind_shiny("plot")

So you can do bind_shiny(layer_points(ggvis(dataDF, ~Year, ~Data)), "plot")

Or using %>% :

data.frame(Year = year, Data = plotData) %>%
  ggvis(~Year, ~Data) %>%
  layer_points() %>%
  bind_shiny("plot")

Note you don't need to put this in a reactive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM