[英]Duplicating GoogleVis chart in Rshiny
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.我的 Rshiny 仪表板中不止一次需要相同的 googlevis 图表,但是当我尝试这样做时,图表无法正确加载。
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)
To my knowledge, Shiny does not allow one output variable more than once in UI.据我所知,Shiny 不允许在 UI 中多次使用一个输出变量。 One quick and easy fix is just to replicate your chart as following:
一种快速简便的修复方法是复制您的图表,如下所示:
## 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.