Кнопка загрузки автоматически очищается при любом изменении ввода - PullRequest
0 голосов
/ 17 февраля 2020

У меня есть два input selection и action button для создания графика и загрузки данных. Я хотел бы очистить содержимое вывода (график и кнопка загрузки) каждый раз, когда происходит изменение в выборе ввода. Код ниже будет только очистить сюжет, а не кнопку загрузки. Не уверен, что reactiveValues под downloadhandler правильно.

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)

Буду признателен за любую помощь.

Спасибо!

1 Ответ

1 голос
/ 17 февраля 2020

Вот решение с использованием renderUI и req:

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)
...