I need the same googlevis chart more than once in my Rshiny dashboard but when I try to do this, charts do not load properly.
For example, in the following code, if I plot the chart one time only it runs fine. Otherwise, both charts don't load. Any thoughts?
## app.R ##
library(shiny)
library(shinydashboard)
suppressPackageStartupMessages(library(googleVis))
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
fluidRow(box(htmlOutput("plot", height = 350))),
fluidRow(box(htmlOutput("plot", height = 350)))
)
)
server <- function(input, output) {
set.seed(122)
histdata <- rnorm(500)
output$plot <- renderGvis({ gvisBubbleChart(Fruits, idvar="Fruit",
xvar="Sales", yvar="Expenses",
colorvar="Year", sizevar="Profit",
options=list(
hAxis='{minValue:75, maxValue:125}')) })
}
shinyApp(ui, server)
Replicated code is bad style. You better use a (reactive) function for that. In your case, reactive is not necessary (since it's a fixed chart), but in most practical cases you will use reactive values there:
library(shinydashboard)
suppressPackageStartupMessages(library(googleVis))
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
fluidRow(box(htmlOutput("plot1", height = 350))),
fluidRow(box(htmlOutput("plot2", height = 350)))
)
)
server <- function(input, output) {
# a bubble chart that can be called anytime
chart <- reactive({
gvisBubbleChart(Fruits, idvar="Fruit",
xvar="Sales", yvar="Expenses",
colorvar="Year", sizevar="Profit",
options=list(
hAxis='{minValue:75, maxValue:125}'))
})
# two plots using the bubble chart
output$plot1 <- renderGvis({ chart() })
output$plot2 <- renderGvis({ chart() })
}
shinyApp(ui, server)