首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >复制GoogleVis图表

复制GoogleVis图表
EN

Stack Overflow用户
提问于 2015-12-09 01:40:55
回答 2查看 225关注 0票数 2

我需要相同的googlevis图表不止一次在我的发亮仪表板,但当我试图这样做,图表不能正确加载。

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

代码语言:javascript
复制
## 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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-25 07:35:50

复制的代码是错误的样式。为此,您最好使用(反应性)函数。在您的示例中,不需要使用反应性(因为它是一个固定的图表),但在大多数实际情况下,您将在那里使用反应性值:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2017-07-24 23:52:09

据我所知,在UI中不允许多个输出变量。一个快速而简单的解决方法就是复制图表,如下所示:

代码语言:javascript
复制
## 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)
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34169331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档