【发布时间】:2019-04-28 06:43:07
【问题描述】:
在 R Shiny 中,我试图从一个模型(包括一组模拟)生成多个图,但它只返回一个图。我尝试了another post on stack overflow 中答案中的代码,它可以工作,但是当我在模型中添加第二个图时,只能显示第二个图,但不能显示第一个图。有人可以就此提出建议吗?上述帖子中的答案代码如下:
library(shiny)
ui <- shinyUI(fluidPage(
br(),
actionButton("numb", "generate a random numbers"),
br(),
br(),
verbatimTextOutput("text"),
plotOutput("plot"),
plotOutput("plot2"),
tableOutput("table")
))
server <- shinyServer(function(input, output) {
model <- eventReactive(input$numb, {
# draw a random number and print it
random <- sample(1:100, 1)
print(paste0("The number is: ", random))
# generate data for a table and plot
data <- rnorm(10, mean = 100)
table <- matrix(data, ncol = 2)
# create a plot
Plot <- plot(1:length(data), data, pch = 16, xlab ="This is the first plot", ylab =
"")
# create a second plot
Plot2 <- plot(1:length(data), data, pch=16, xlab="This is the second plot", ylab =
"")
# return all object as a list
list(random = random, Plot = Plot, Plot2=Plot2, table = table)
})
output$text <- renderText({
# print the random number after accessing "model" with brackets.
# It doesn't re-run the function.
youget <- paste0("After using model()$random you get: ", model()$random,
". Compare it with a value in the console")
print(youget)
youget
})
output$plot <- renderPlot({
# render saved plot
model()$Plot
})
output$plot2 <-renderPlot({
# render second plot
model()$Plot2
})
output$table <- renderTable({
model()$table
})
})
shinyApp(ui = ui, server = server)
【问题讨论】: