【问题标题】:In RShiny, using actionButton values within conditionalPanel causing many headaches在 R Shiny 中,在 conditionalPanel 中使用操作按钮值会引起很多麻烦
【发布时间】:2017-03-20 07:41:31
【问题描述】:

我花了最后半个小时阅读有关 conditionalPanel 以及 actionButtons 以及每次按下按钮时它们的值如何递增 1 的内容。话虽如此,这就是我在下面尝试做的事情以及我面临的问题:

问题 我正在使用 conditionalPanel 在两个不同的渲染图之间切换。我使用两个不同的渲染图而不是同一个渲染图中的两个图的原因是,两个图具有不同的高度/宽度尺寸很重要。我在 conditionalPanel 中使用 actionButton 的值,这很糟糕。

我想要什么 如果最近按下了 2 个按钮,我希望显示移动图表。如果最近按下了 toggleshotchart,我希望显示 shot.chart。

我希望我能做什么 如果我只能在条件中使用 plotData$value,我就准备好了。 plotData$value 用于在 renderPlots 中输入 if 语句以确定应该绘制哪些图,但我不能在 conditionalPanel 中使用它们。

说了这么多,下面是我项目的简化版本。

library(shiny)

# 2. UI layout
# ============
ui <- fluidPage(
        fluidRow(
          column(width = 4, align = 'center', 
              actionButton(inputId = 'toggleshotchart', label = 'Launch Shots'), 
              actionButton(inputId = 'togglemovement', label = 'Launch Movements')
          ),

          # This displays either the shot or movement chart
          # ===============================================
          column(width = 8, align = 'left',
                 conditionalPanel("input.togglemovement > input.toggleshotchart",
                                  plotOutput('movement.chart', height = 650, width = 1128)
                 ),
                 conditionalPanel("input.togglemovement <= input.toggleshotchart",
                                  plotOutput('shot.chart', height = 846, width = 900)
                 )
          )
        )
)

# 3. Server Layout
# ================
server <- shinyServer(function(input, output) {

  # Create some reactive stuff to toggle charts
  plotData <- reactiveValues(value = NULL)

  observeEvent(input$toggleshotchart, {
    plotData$value <- 0
  })

  observeEvent(input$togglemovement, {
    plotData$value <- 1
  })

  # create the first chart
  output$shot.chart <- renderPlot({

    # this chart is displayed at launch
    if (is.null(plotData$value)) {
      plot(c(1,2,3,4,5), c(1,2,3,4,5))
    }

    # this chart SHOULD BE displayed after clicking toggleshotchart
    else if(plotData$value == 0) {
      hist(rnorm(10))
    }      

    # Do nothing (prev displayed motion chart here)
    else {
      # do nothing
    }
  })

  # this chart SHOULD BE displayed after clicking togglemovementchart
  output$movement.chart <- renderPlot({
    if(plotData$value == 1) {
      hist(c(1,2,3,2,1))
    }
  })
})

shinyApp(ui = ui, server = server)

我阅读了很多关于重置 actionButton 值的内容,但找不到任何可以解决我的问题的内容。似乎将 actionButton 值重置为 0 并不容易/无法完成。对我的这项任务的任何想法将不胜感激!

编辑 - 我之前问过这个相关问题 - In RShiny, change plot width / height for separate plots within same renderPlot() - 解决方案在哪里有效,但问题略有不同。我在这个问题上工作得越多,我就越意识到我最初的问题并没有解决我的实际问题。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    使用radioButtons 在两个绘图之间切换怎么样?

    library(shiny)
    
    # 2. UI layout
    # ============
    ui <- fluidPage(
      fluidRow(
        column(width = 4, 
               radioButtons("choice_plot","Launch",choices=list("Shots","Movements"), selected="Shots")),
    
        # This displays either the shot or movement chart
        # ===============================================
        column(width = 8, align = 'left', uiOutput("plot_ui"))
      )
    )
    
    # 3. Server Layout
    # ================
    server <- shinyServer(function(input, output) {
    
      output$plot_ui <- renderUI({
        if(input$choice_plot == 'Shots') {
          plot.width = 1128
          plot.height = 650
        }else{
          plot.width = 900
          plot.height = 846
        }
        plotOutput('plot', width = plot.width, height = plot.height)
    
      })
    
      output$plot <- renderPlot({
        if(input$choice_plot == 'Shots'){
          hist(rnorm(10))
        }else{
          hist(c(1,2,3,2,1))
          }
      })
    
    })
    
    shinyApp(ui = ui, server = server)
    

    这也是我对actionButton的尝试:

    library(shiny)
    
    # 2. UI layout
    # ============
    ui <- fluidPage(
      fluidRow(
        column(width = 4, 
               actionButton("button1", "Shots"),
               actionButton("button2", "Movements")),
    
        # This displays either the shot or movement chart
        # ===============================================
        column(width = 8, align = 'left', uiOutput("plot_ui"))
      )
    )
    
    # 3. Server Layout
    # ================
    server <- shinyServer(function(input, output) {
    
      output$plot_ui <- renderUI({
        if(input$button1 == 1) {
          plot.width = 1128
          plot.height = 650
        }else{
          plot.width = 900
          plot.height = 846
        }
        plotOutput('plot', width = plot.width, height = plot.height)
    
      })
    
      v <- reactiveValues(data = NULL)
    
      observeEvent(input$button1, {
        v$data <- rnorm(10)
      })
    
      observeEvent(input$button2, {
        v$data <- c(1,2,3,2,1)
      })
    
      output$plot <- renderPlot({
        if (is.null(v$data)) return()
        hist(v$data)
      })
    
    })
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 您好,马尔维娜,感谢您的 cmets 以及我的其他帖子。在我的完整应用程序中,还有几个 selectInput 切换开关,它们改变了每个绘图的特性。更改 selectInput 切换按钮后单击操作按钮以更新图表,这就是为什么我强烈希望将操作按钮保留在单选按钮之上。
    • 如果我无法让操作按钮按预期工作,那么我想这将是一个不错的选择。
    • 你能解释一下吗?
    • 我已尝试仅使用actionButton 解决您的问题,看看答案
    猜你喜欢
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多