【问题标题】:Click through multiple plots within each iteration in a Shiny App在 Shiny App 的每次迭代中单击多个绘图
【发布时间】:2019-02-04 17:11:55
【问题描述】:

在我的 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)

【问题讨论】:

  • 你可以尝试使用不同的outputIds(output$plot1output$plot2
  • 会在应用程序的同一位置一次显示一个图吗? :)

标签: r shiny


【解决方案1】:

就像 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)

【讨论】:

  • 你想展示哪个情节?另外,如果只显示一个,为什么还要创建两个图?
  • 我希望它迭代地绘制 k-means,所以一个图将是簇颜色的变化,而另一个图将是质心移动。这就是为什么我需要每次迭代制作两个图并且只需要一个图显示在我的应用程序中:)
  • 是的,你可以这样做。创建第二个图作为反应元素并使用 plotOutput 仅显示您要显示的图。见编辑。
  • 抱歉造成混淆,是否可以在屏幕上的相同位置显示两个图,并且一个接一个地显示?如果我不明白,我很乐意将实际代码发送给您。
  • 如果我理解正确,请告诉我: 1. 用户单击“运行”按钮 2. 显示一个绘图 3. 延迟一段时间后,另一个绘图同时延迟位置 4。这个情节保持不变。 5. 用户再次点击按钮,重复步骤 1 到 4。
猜你喜欢
  • 2021-05-07
  • 1970-01-01
  • 2016-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多