在downloadHandler中验证

时间:2017-10-24 06:07:13

标签: r validation shiny

我想在downloadHandler内执行一些验证,这样当条件为真时,会给出自定义消息,否则下载数据。我有以下代码示例,我想要执行验证,但它似乎无法正常工作。

shinyApp(
  ui = basicPage(
    textInput("jobid", label = "Enter job ID", value = ""),
    downloadLink("downloadData", "Download")

  ),
  server <- function(input, output) {
    # Our dataset
    data <- mtcars

    output$downloadData <- downloadHandler(
      filename = function() {
        paste("data-", Sys.Date(), ".csv", sep="")
      },
      content = function(file) {
        if(input$jobid ==""){
          session$sendCustomMessage(type = 'testmessage',
                                    message = 'No job id to submit')
        }else
          write.csv(data, file)
      }
    )
  }
)

如何验证??

1 个答案:

答案 0 :(得分:1)

嗯,也许这不是最优雅的解决方案,但我希望它有所帮助!

library(shiny)

ui <- fluidPage(
  textInput("jobid", label = "Enter job ID", value = ""),
  uiOutput("button"),
  textOutput("downloadFail")
)

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

  output$button <- renderUI({
    if (input$jobid == ""){
      actionButton("do", "Download", icon = icon("download"))
    } else {
      downloadButton("downloadData", "Download")
    }
  })

  available <- eventReactive(input$do, {
    input$jobid != ""
  })

  output$downloadFail <- renderText({
    if (!(available())) {
      "No job id to submit"
    } else {
        ""
    } 
  })

  output$downloadData <- downloadHandler(
    filename = function() {
      paste("data-", Sys.Date(), ".csv", sep="")
    },
    content = function(file) {
        write.csv(data, file)
    })
  }


shinyApp(ui, server)