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

问题描述 投票:0回答:1

在我的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)
r shiny
1个回答
0
投票

就像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)
© www.soinside.com 2019 - 2024. All rights reserved.