R shiny - 上传后更改文本文件输入

R shiny - Change text fileInput after upload

我想在文件上传后更改fileInput中的占位符,即自定义写入的文件名。

我找到了如何自定义进度条标签,所以我猜代码应该非常相似。这是我到目前为止尝试过的:

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)

开始,好像输入框在第二个位置。但是,我不确定如何编写jscode。有什么建议吗?

library(shiny)

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').parent().parent().parent().find('input[type=text]');
  target.val(txt);
}); "

ui <- fluidPage( 
  tags$script(HTML(jscode_upload_msg)),
  tags$script(HTML(jscode_upload_txt)),
  fileInput("fileUpload",  "File to upload") 
)

server <- function(input, output, session ) {
  observeEvent(input$fileUpload, {
    session$sendCustomMessage("upload_msg", "YOUR TEXT")
    session$sendCustomMessage("upload_txt", "SOME OTHER TEXT")
  })
}

shinyApp(ui = ui, server = server)