R Shiny - 从 fileInput 获取占位符值

R Shiny - Get placeholder value from fileInput

我可以使用以下代码读取占位符值作为弹出警报:

library(shiny)
library(shinyjs)

jscode_upload_txt <- " Shiny.addCustomMessageHandler('upload_txt', function(txt) {
  var target = $('#fileUpload').parent().parent().parent().find('input[type=text]');
  target.val(txt);
}); "

jscode_get_txt <- " Shiny.addCustomMessageHandler('get_txt', 
  function(message) {
    var target = $('#fileUpload').parent().parent().parent().find('input[type=text]');
    alert(JSON.stringify(target.val()));
  }
); "

ui <- fluidPage( 
  tags$script(HTML(jscode_upload_txt)),
  tags$script(HTML(jscode_get_txt)),
  fileInput("fileUpload",  "File to upload", placeholder = "Initial Text") 
)

server <- function(input, output, session ) {
  showLog()
  observe({
    session$sendCustomMessage("upload_txt", "SOME OTHER TEXT")
    session$sendCustomMessage("get_txt" , "")
  })
}

shinyApp(ui = ui, server = server)

如何直接在 R 中读取它(并与之交互)?我尝试 logjs 而不是 alert,但没有任何显示。

你可以做到

jscode_get_txt <- " Shiny.addCustomMessageHandler('get_txt', 
  function(message) {
    var target = $('#fileUpload').parent().parent().parent().find('input[type=text]');
    Shiny.setInputValue('placeholder', target.val());
  }
); "

那么占位符在 input$placeholder 中可用。