Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate inside downloadHandler

I want to perform some validation inside downloadHandler, so that when the condition is true a custom message is given else download of data. I have the following code example where I want to perform validation, but it seems not working.

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

How to validate??

like image 412
AwaitedOne Avatar asked Dec 31 '25 10:12

AwaitedOne


2 Answers

Well, maybe this is not the most elegant solution, but I hope it helps!

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)
like image 166
Adela Avatar answered Jan 03 '26 03:01

Adela


You could use showNotification, or to write to the UI look at renderUI and outputUI functions.

`output$download <- downloadHandler(
    filename = function(){
        "filename.csv")
    },
    content = function(file){
        if(nrow(data() < 10000) {
            write.csv(data(), file)
        } else {
            showNotification("Data too big")
        }
)`
like image 29
kraggle Avatar answered Jan 03 '26 02:01

kraggle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!