我需要相同的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)发布于 2017-07-25 07:35:50
复制的代码是错误的样式。为此,您最好使用(反应性)函数。在您的示例中,不需要使用反应性(因为它是一个固定的图表),但在大多数实际情况下,您将在那里使用反应性值:
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)发布于 2017-07-24 23:52:09
据我所知,在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)https://stackoverflow.com/questions/34169331
复制相似问题