R Shiny - 从 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中可用。

最新更新