【问题标题】:Rendering multiple plots in R shiny from the same model从同一模型中渲染 R 中的多个图
【发布时间】: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)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我将绘图移到了 renderPlot 服务器函数并修改了绘图调用:

    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)
    
        # return all object as a list
        list(random = random, 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
        mod_list=model()
        data=mod_list$table
        # create a plot
        plot(data[,1], data[,2], pch = 16, xlab ="This is the first plot", ylab ="")
    
      })
    
      output$plot2 <-renderPlot({
        # render second plot
        mod_list=model()
        data=mod_list$table
        # create a second plot
        plot(data[,1], data[,2], pch=16, xlab="This is the second plot", ylab ="")
    
      })
    
      output$table <- renderTable({
    
        model()$table
      })
    })
    

    您的绘图函数的 x 和 y 参数令人困惑。如果您希望每个图有两条线,请尝试使用 qplot 和 melt 函数将您的数据框重塑为长格式。如果你只想要一个有 10 个随机值的图,不要使用矩阵函数

    【讨论】:

    • 非常感谢!!!它工作得很好。 Shiny对我来说是新的,这真的解决了这个问题。我以代码为例,其中关键是从生成的相同数据中渲染多个图。这真太了不起了。非常感谢。
    猜你喜欢
    • 2014-11-23
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2020-12-27
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多