任何输入更改都会自动清除下载按钮

Download button auto clear by any input change

我有两个 input selection 和一个 action button 来生成绘图并下载数据。每当输入选择发生变化时,我想清除输出内容(绘图和下载按钮)。下面的代码只会清除情节而不是下载按钮。不确定downloadhandler下面的reactiveValues是否正确。

library(shiny)
library(ggplot2)
library(openxlsx)


ui = fluidPage(
  textInput("textT", label = "Title", value = ""),
  textInput("textX", label = "X-Axis Label", value = ""),
  actionButton("Btn", "Run", icon=icon("play-circle")),
  plotOutput('plot1'),
  conditionalPanel(condition = "input.Btn>0", downloadButton("dwload", "Download"))
  )

server = function(input, output, session) {    

  v <- reactiveValues(clearAll = TRUE)

  observeEvent(c(input$textT, input$textX), {
    v$clearAll <- TRUE
  }, priority = 10)

  observeEvent(input$Btn, {

    output$plot1 = renderPlot({
      if (v$clearAll) 
        return()
      else
        ggplot(mtcars, aes(x= gear, y= carb)) + geom_line() +ggtitle(input$textT) + xlab(input$textX)
    })

    output$dwload <- downloadHandler(
        filename = function() {
          paste0("Checks-", gsub(" ", "_", gsub(":", ".", Sys.time())), ".xlsx")
        },
      content = function(file) {
        if (v$clearAll) 
          return()
        else
          quick_xlsx(mtcars, file=file)
      }
    )

    v$clearAll <- FALSE

  }, priority = 10)

}

shinyApp(ui, server)

如有任何帮助,我将不胜感激。

谢谢!

这是使用 renderUIreq 的解决方案:

library(shiny)
library(ggplot2)
library(openxlsx)


ui <- fluidPage(
  textInput("textT", label = "Title", value = ""),
  textInput("textX", label = "X-Axis Label", value = ""),
  actionButton("Btn", "Run", icon=icon("play-circle")),
  uiOutput("widgets")
)

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

  hideAll <- reactiveVal(TRUE)

  observeEvent(list(input$textT, input$textX), {
    hideAll(TRUE)
  })

  observeEvent(input$Btn, {
    req(input$textT)
    req(input$textX)
    hideAll(FALSE)
  })


  output$plot1 <- renderPlot({
    ggplot(mtcars, aes(x= gear, y= carb)) + geom_line() + 
      ggtitle(input$textT) + xlab(input$textX)
  })

  output$dwload <- downloadHandler(
    filename = function() {
      paste0("Checks-", gsub(" ", "_", gsub(":", ".", Sys.time())), ".xlsx")
    },
    content = function(file) {
      quick_xlsx(mtcars, file=file)
    }
  )

  output$widgets <- renderUI({
    req(!hideAll())
    tagList(
      plotOutput('plot1'),
      downloadButton("dwload", "Download")
    )
  })

}

shinyApp(ui, server)