Chart not generated in R shiny when run locally us

2019-04-14 08:43发布

问题:

These are the codes for my UI and server. The issue that I am facing is that when the app is run locally the charts are not being generated.

ui.R

library(googleVis)
library(shiny)
shinyUI(fluidPage(
  titlePanel(" Tool"),
  sidebarLayout(
    sidebarPanel(
      radioButtons(inputId="choice", label="What would you like to see?", 
                   choices=c("Overall ","Individual"))
      ),
  mainPanel(
      htmlOutput("View")

    )
  )
))

server.R

library(googleVis)
require(googleVis)
shinyServer(function(input, output) {
  n = 100 
  dates = seq(Sys.Date(), by = 'day', length = n)
  x = 10 * rnorm(n)
  y = 3 * x + 1 + rnorm(n)
  label = rep(LETTERS[1:4], each=25)
  label[1] = "D"

  my.data = data.frame(Date = dates, x, y, label)
  output$view <- renderGvis({
    gvisMotionChart(my.data, idvar ='label', xvar = 'x', yvar = 'y', timevar= 'Date')
  })

}
)

回答1:

Looks like you have a couple things going wrong here. First, you should have a library open to shiny in both server.R and ui.R; it looks like you reproduced googleVis twice in server.R. In addition I found you capitalized the 'v' in htmlOutput('view'), but this should match the output$view path in server.R which is not capitalized.

On top of this the radio buttons seem superfluous or I do not understand the intent. Typically radio buttons are used so that their input can be fed to a reactive environment in server.R to change a dataset or some other parameter (see shiny tutorial or this example: https://github.com/rstudio/shiny-examples/blob/master/006-tabsets/server.R).

Code below will produce the plot and I have left the radio buttons even though they serve no purpose.

ui.R

library(googleVis)
library(shiny)

shinyUI(fluidPage(
  titlePanel(" Tool"),
  sidebarLayout(
    sidebarPanel(
      radioButtons(inputId="choice", label="What would you like to see?", 
                   choices= c("Overall ","Individual"))
    ),
    mainPanel(
      htmlOutput("view")

    )
  )
))

server.R

library(googleVis)
library(shiny)

shinyServer(function(input, output) {

  n = 100 
  dates = seq(Sys.Date(), by = 'day', length = n)
  x = 10 * rnorm(n)
  y = 3 * x + 1 + rnorm(n)
  label = rep(LETTERS[1:4], each=25)
  label[1] = "D"

  my.data = data.frame(Date = dates, x, y, label)

  output$view <- renderGvis({
    gvisMotionChart(my.data, 
                    idvar ='label', 
                    xvar = 'x', 
                    yvar = 'y', 
                    timevar= 'Date')
  })

})

Be sure to also open it to a browser after the app is launched. Hope that helps.