我在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)
答案 0 :(得分:2)
复制代码风格不好。你最好使用(反应)函数。在你的情况下,反应是没有必要的(因为它是一个固定的图表),但在大多数实际情况下,你将使用反应值:
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)
答案 1 :(得分:-1)
据我所知,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)