R Shiny:刷新/覆盖actionButton()输出

3

我正在尝试将R 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(
      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的某种组合,但我的尝试至今未果。
1个回答

3
您可以使用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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接