【问题标题】:R Shiny: refreshing/overriding actionButton() outputR Shiny:刷新/覆盖 actionButton() 输出
【发布时间】:2018-06-11 19:44:55
【问题描述】:

我正在尝试使 R Shiny: automatically refreshing a main panel without using a refresh button 适应一个新的最小工作示例:

ui <- fluidPage(

  pageWithSidebar(
    headerPanel("actionButton test"),
    sidebarPanel(
      numericInput("n", "N:", min = 0, max = 100, value = 50),
      br(),
      actionButton("goButton", "Go!"),
      p("Click the button to update the value displayed in the main panel."),
      actionButton("newButton", "New Button"),
      actionButton("newButton2", "Another New Button")
    ),
    mainPanel(
      verbatimTextOutput("nText"),
      textOutput("some_text_description"),
      plotOutput("some_plot")
    )
  )

)


server <- function(input, output, session) {

  # builds a reactive expression that only invalidates 
  # when the value of input$goButton becomes out of date 
  # (i.e., when the button is pressed)
  ntext <- eventReactive(input$goButton, {
    input$n
  })

  output$nText <- renderText({
    ntext()
  })


  # Prep some text for output
  output$some_text_description <- renderText({ 
    if (input$newButton == 0) {return(NULL)}
    else { 
      "Lorem ipsum dolorom." 
    }
  })

  # Prep some figure for output
  # Simple Bar Plot 
  output$some_plot <- renderPlot({ 
    if (input$newButton2 == 0) {return(NULL)}
    else { 
      counts <- table(mtcars$gear)
      barplot(counts, main="Car Distribution", xlab="Number of Gears")
    }
  })

}



shinyApp(ui = ui, server = server)

在上面的代码中,我有三个actionButton 命令,一个产生绘图,一个产生文本输出,一个产生一个数字(作为逐字文本输出)。当您单击每个按钮时,新的输出会出现在先前生成的输出旁边(来自您按下的最后一个按钮)。

无需实现手动清除所有内容的刷新按钮,我如何让每个actionButton 自动覆盖(即擦除)其他人的输出,而不会在主面板中将它们全部堆叠在一起。我的理解是我需要使用observeEventNULLreactiveValues 的某种组合,但到目前为止我的尝试都没有成功。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您可以为此使用renderUI()

      output$all <- renderUI({
        global$out
      })
    

    在全局响应值global$out 中,您可以存储要显示的 ui 元素。 (最初它应该是空的,因此是NULL)。

      global <- reactiveValues(out = NULL)
    

    然后监听按钮中的点击并相应地更新global$out

      observeEvent(input$goButton, {
        global$out <- verbatimTextOutput("nText")
      })
    
      observeEvent(input$newButton, {
        global$out <- textOutput("some_text_description")
      })
    
      observeEvent(input$newButton2, {
        global$out <- plotOutput("some_plot")
      })
    

    完整的应用程序会这样写:

    library(shiny)
    ui <- fluidPage(
    
      pageWithSidebar(
        headerPanel("actionButton test"),
        sidebarPanel(
          numericInput("n", "N:", min = 0, max = 100, value = 50),
          br(),
          actionButton("goButton", "Go!"),
          p("Click the button to update the value displayed in the main panel."),
          actionButton("newButton", "New Button"),
          actionButton("newButton2", "Another New Button")
        ),
        mainPanel(
          uiOutput("all")
        )
      )
    
    )
    
    
    server <- function(input, output, session) {
    
      global <- reactiveValues(out = NULL)
    
      observeEvent(input$goButton, {
        global$out <- verbatimTextOutput("nText")
      })
    
      observeEvent(input$newButton, {
        global$out <- textOutput("some_text_description")
      })
    
      observeEvent(input$newButton2, {
        global$out <- plotOutput("some_plot")
      })
    
      output$all <- renderUI({
        global$out
      })
    
      output$nText <- renderText({
        input$n
      })
    
      output$some_text_description <- renderText({ 
          "Lorem ipsum dolorom."
      })
    
      # Simple Bar Plot 
      output$some_plot <- renderPlot({ 
        counts <- table(mtcars$gear)
        barplot(counts, main="Car Distribution", xlab="Number of Gears")
      })
    
    }
    
    
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 2021-07-28
      • 1970-01-01
      • 2021-04-30
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 2018-05-18
      • 2021-04-05
      • 2020-07-31
      相关资源
      最近更新 更多