gpt4 book ai didi

r - 在 downloadHandler 内部验证

转载 作者:行者123 更新时间:2023-12-04 17:48:40 24 4
gpt4 key购买 nike

我想在 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)
}
)
}
)

如何验证??

最佳答案

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

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)

关于r - 在 downloadHandler 内部验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46903285/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com