点击Shiny App中每次迭代中的多个图

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了点击Shiny App中每次迭代中的多个图相关的知识,希望对你有一定的参考价值。

在我的Shiny应用程序中,我使用for循环在每次迭代中制作两个不同的绘图,我希望用户能够单独点击每个。

count变量保持单击按钮的次数,但是,每次单击时,都会生成两个绘图,并且只显示最后渲染的绘图。

如何使用动作按钮显示每个情节?

library(shiny)
server <- function(input, output, session) {          
  # data
  v <- c(9,8,7,8,9,5,6,7,4,3)
  w <- c(3,4,2,3,3,3,2,3,4,5)
  x <- c(1,3,4,6,2,4,6,8,6,3)
  y <- c(4,5,2,4,2,1,2,5,7,8)
  z <- c(5,9,8,6,4,6,8,9,6,7)
  df <- data.frame(v, w, x, y, z)

  # initial plot that will allow user to change parameters (haven't implemented yet)
  output$plot <- renderPlot(plot(df[[1]],df[[2]]))        

  count<-0   # This is the counter which keeps track on button count

  observeEvent(input$run, {
    count <<- count + 1 # Increment the counter by 1 when button is click
    if(count<6){
      # Draw the plot if count is less than 6
      output$plot <- renderPlot(plot(df[[1]],df[[count]],main = count))
      output$plot <- renderPlot(plot(df[[3]],df[[count]],main = count))
    }
    else{
      # Reset the counter if it is more than 5
      count <- 0
    }             
  })
}

ui <- fluidPage(
  actionButton("run", "Generate"),
  plotOutput("plot")
)

shinyApp(ui = ui, server = server)
答案

就像Gregor de Cillia所说,你可以使用不同的情节。

编辑1:仅显示plot1

library(shiny)
server <- function(input, output, session) {          
  # data
  v <- c(9,8,7,8,9,5,6,7,4,3)
  w <- c(3,4,2,3,3,3,2,3,4,5)
  x <- c(1,3,4,6,2,4,6,8,6,3)
  y <- c(4,5,2,4,2,1,2,5,7,8)
  z <- c(5,9,8,6,4,6,8,9,6,7)
  df <- data.frame(v, w, x, y, z)

  # initial plot that will allow user to change parameters (haven't implemented yet)
  # Make two different plots here
  output$plot1 <- renderPlot(plot(df[[1]],df[[2]]))        
  output$plot2 <- renderPlot(plot(df[[3]],df[[1]])) 

  count<-0   # This is the counter which keeps track on button count

  observeEvent(input$run, {
    count <<- count + 1 # Increment the counter by 1 when button is click
    if(count<6){
      # Draw the plot if count is less than 6
      # Update both these plots
      output$plot1 <- renderPlot(plot(df[[1]],df[[count]],main = count))
      # Generate this plot but do not output
      plot2 <- reactive({
        renderPlot(plot(df[[3]],df[[count]],main = count))
      })
    }
    else{
      # Reset the counter if it is more than 5
      count <- 0
    }             
  })
}

ui <- fluidPage(
  actionButton("run", "Generate"),
  # Output both plots separately
  plotOutput("plot1")
  # Don't show this
  # plotOutput("plot2")
)

shinyApp(ui = ui, server = server)

编辑2:根据更新的理解,这里是如何实现所需的:

library(shiny)
server <- function(input, output, session) {          
  # data
  v <- c(9,8,7,8,9,5,6,7,4,3)
  w <- c(3,4,2,3,3,3,2,3,4,5)
  x <- c(1,3,4,6,2,4,6,8,6,3)
  y <- c(4,5,2,4,2,1,2,5,7,8)
  z <- c(5,9,8,6,4,6,8,9,6,7)
  df <- data.frame(v, w, x, y, z)

  # initial plot that will allow user to change parameters (haven't implemented yet)
  output$plot1 <- renderPlot(plot(df[[1]],df[[2]]))

  count<-0   # This is the counter which keeps track on button count

  observeEvent(input$run, {
    count <<- count + 1 # Increment the counter by 1 when button is click
    if(count<6){
      # Draw plot if count < 6

      # In every iteration, first click is an odd number,
      # for which we display the first plot

      if(count %% 2 != 0){
        # Draw first plot if count is odd
        output$plot1 <- renderPlot(plot(df[[1]],df[[count]],main = count))  
      }
      else{
        # Second click is an even number, 
        # so we display the second plot in the previous iteration,
        # hence df[[count - 1]]

        # Draw second plot if count is even
        output$plot1 <- renderPlot(plot(df[[3]],df[[count-1]],main = count))
      }
    }

    else{
      # Reset the counter if it is more than 5
      count <- 0
    }             
  })
}

ui <- fluidPage(
  actionButton("run", "Generate"),
  # Output plot
  plotOutput("plot1")
)

shinyApp(ui = ui, server = server)

以上是关于点击Shiny App中每次迭代中的多个图的主要内容,如果未能解决你的问题,请参考以下文章

如何在Django视图中使用for循环返回每次迭代[关闭]

如何处理 Shiny App 部署中的文件计数超出限制错误 [关闭]

如何在 R Shiny 中仅使用一个下载按钮下载多个图?

片段存储和重用:使用TabView的多个子片段

如何在 R Shiny App 中保留复选框中的值?

如何在Shiny app中的仪表板主体中输出数据库表?