在 R Shiny 中复制 GoogleVis 图表

Duplicating GoogleVis chart in Rshiny

我在 Rshiny 仪表板中不止一次需要同一个 googlevis 图表,但是当我尝试这样做时,图表无法正确加载。

例如,在下面的代码中,如果我只绘制图表一次,它运行良好。否则,两个图表都不会加载。有什么想法吗?

## 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)

据我所知,Shiny 在 UI 中不允许一个输出变量超过一次。一种快速简便的解决方法是按如下方式复制您的图表:

## app.R ##
library(shiny)
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) {
  set.seed(122)
  histdata <- rnorm(500)

  output$plot1 <- renderGvis({ gvisBubbleChart(Fruits, idvar="Fruit", 
                                              xvar="Sales", yvar="Expenses",
                                              colorvar="Year", sizevar="Profit",
                                              options=list(
                                                hAxis='{minValue:75, maxValue:125}')) })

  output$plot2 <- renderGvis({ gvisBubbleChart(Fruits, idvar="Fruit", 
                                               xvar="Sales", yvar="Expenses",
                                               colorvar="Year", sizevar="Profit",
                                               options=list(
                                                 hAxis='{minValue:75, maxValue:125}')) })

}

shinyApp(ui, server)

重复的代码是糟糕的风格。您最好为此使用(反应性)功能。在你的情况下,反应不是必需的(因为它是一个固定的图表),但在大多数实际情况下你会在那里使用反应值:

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)