I want to change the placeholder in fileInput after a file has been uploaded, i.e. customize the "No file selected" to any string I choose.
I found how to customize the progress bar label, so I'm guessing the code should be quite similar. This is what I tried so far:
library(shiny)
library(shinyjs)
jscode_upload_msg <- " Shiny.addCustomMessageHandler('upload_msg', function(msg) {
var target = $('#fileUpload_progress').children()[0];
target.innerHTML = msg;
}); "
jscode_upload_txt <- " Shiny.addCustomMessageHandler('upload_txt', function(txt) {
var target = $('#fileUpload_header').children()[1].children()[0];
target.innerHTML = txt;
}); "
ui <- fluidPage(
useShinyjs(),
tags$script(jscode_upload_msg),
tags$script(jscode_upload_txt),
fileInput("fileUpload", "File to upload")
)
server <- function(input, output, session ) {
observe({
req(input$fileUpload)
session$sendCustomMessage("upload_msg", "YOUR TEXT")
session$sendCustomMessage("upload_txt", "SOME OTHER TEXT")
})
}
shinyApp(ui = ui, server = server)
From r - Shiny customise fileInput - Stack Overflow , it seems that the input field is in the second position. However, I am not sure how to write the jscode. Any advice?
you can achieve this effect while avoiding writing javascript.
I make the fileInput have no label,
I control the 'label' by constructing a label explicitly, and tie it to the activity of the fileUpload
library(shiny)
library(shinyjs)
jscode_upload_msg <- " Shiny.addCustomMessageHandler('upload_msg', function(msg) {
var target = $(\"#fileUpload_progress\").children()[0];
target.innerHTML = msg;
}); "
ui <- fluidPage(
useShinyjs(),
tags$script(jscode_upload_msg),
div(
uiOutput("fileUpload_label"),
fileInput("fileUpload", label=NULL) )
)
server <- function(input, output, session ) {
observe({
req(input$fileUpload)
session$sendCustomMessage("upload_msg", "YOUR TEXT")
})
output$fileUpload_label<-renderUI({
if(isTruthy(input$fileUpload))
tags$label(paste0("File Input ",sample.int(9999,1)))
else
tags$label("initial label")
})
}
shinyApp(ui = ui, server = server)
Thank you very much for your answer. I think my question was not precise enough, I edited it to explain more clearly what I am trying to achieve.
The initial placeholder can be adjusted at initialisation
fileInput("fileUpload",placeholder="my own string")
I am basically looking for an updateFileInput
that could work as: updateFileInput("fileUpload",placeholder="my own string")
having googled this, I found that you already received an answer on stackoverflow.
jscode_upload_txt <- " Shiny.addCustomMessageHandler('upload_txt', function(txt) {
var target = $('#fileUpload').parent().parent().parent().find('input[type=text]');
target.val(txt);
}); "
system
Closed
July 6, 2020, 9:36am
7
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.