【问题标题】:Duplicating GoogleVis chart in Rshiny在 R Shiny 中复制 GoogleVis 图表
【发布时间】:2015-12-09 01:40:55
【问题描述】:

我在我的 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)

【问题讨论】:

    标签: r shiny googlevis


    【解决方案1】:

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

    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)
    

    【讨论】:

    • 这当然是一个更简洁的解决方案...谢谢!
    【解决方案2】:

    据我所知,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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-25
      相关资源
      最近更新 更多