I'm trying to build a form that collects an identification number and allows file uploads to that id.
The id number has a valid form (5 or 6 digits). I have the following code, which works as expected except when an invalid number is entered, and a file is uploaded anyway.
The file isn't uploaded (good), and the "invalid" message appears. Unfortunately, when a valid number is then entered, two directories are created, and the file is uploaded twice.
I'm new to reactivity and shiny, and some of this code was copy/pasted off the web. I've obviously done something wrong and would appreciate guidance on how to solve the issue.
I can imagine two ways forward. I'm unsure which way to go - either fixing the following code so that a file is only uploaded once or requiring id number validity before allowing the upload field to be used or seen. Advice on best practices for this end would also be appreciated.
server <- function(input, output, session) {
iv <- InputValidator$new()
iv$add_rule("identifier",sv_regex("^#?[0-9]{5,6}$", "Please input an id number and try again"))
output$contents <- renderTable({
req(input$file)
observeEvent(input$file, {
if(iv$is_valid()) {
reset_form()
removeNotification("submit_message")
#create new folder in shared Google Drive with id number
newdr <- drive_mkdir(paste(input$identifier), path = drive_id)
new_path <- newdr$id
mapply( function(datapath, name){
datetime = format(as.POSIXct(Sys.time()),"%Y-%m-%d-%H:%M:%S")
new_name = paste(datetime,input$file$name,sep="-")
drive_upload(media = datapath,
name = new_name,
path = new_path)},
input$file$datapath,
input$file$name)
}
else {
iv$enable()
showNotification(
"Please correct the errors in the form and try again",
id = "submit_message", type = "error")
}
})
reset_form <- function() {
iv$disable()
removeNotification("submit_message")
updateTextInput(session, "identifier", value = "")
}
return()
})
}
I have tried using code similar to that found on the post insertUI/removeUI based on a checkboxInput to insert the file upload field on is_valid()
but it's not working either - the file input field flashes up and disappears. I don't understand why that is happening.
server <- function(input, output, session) {
UI_exist = FALSE
iv <- InputValidator$new()
iv$add_rule("identifier",sv_regex("^#?[0-9]{5,6}$", "Please input an id number and try again"))
output$contents <- renderTable({
req(input$file)
observeEvent(input$identifier, {
if(iv$is_valid()) {
reset_form()
removeNotification("submit_message")
insertUI(
selector = "#fileinput",
where = "afterBegin",
ui = fileInput("file", "Select the files to upload", multiple = TRUE),
)
UI_exist <<- TRUE
}
else {
iv$enable()
showNotification(
"Please correct the errors in the form and try again",
id = "submit_message", type = "error")
removeUI(selector = "div#fileinput > div")
UI_exist <<- FALSE
}
})
reset_form <- function() {
iv$disable()
removeNotification("submit_message")
updateTextInput(session, "identifier", value = "")
}
observeEvent({input$identifier; input$file}, {
if(iv$is_valid() && UI_exist) {
#create new folder in shared Google Drive with id
newdr <- drive_mkdir(paste(input$identifier), path = drive_id)
new_path <- newdr$id
mapply( function(datapath, name){
datetime = format(as.POSIXct(Sys.time()),"%Y-%m-%d-%H:%M:%S")
new_name = paste(datetime,input$file$name,sep="-")
drive_upload(media = datapath,
name = new_name,
path = new_path)},
input$file$datapath,
input$file$name)
}
return()
})
})
}