Shiny:从自己的 JS 脚本访问输入对象

RSz*_*SzT 5 javascript r shiny

在 ShinyconditionalPanel中的conditionarg 中,可以访问 JSinput对象,因此,如果有小部件,则可以通过 JS 表达式numericInput("num", label = h3("Numeric input"), value = 1)访问该小部件的值。conditioninput.num

问题是如何以同样的方式访问input自己的 JS 脚本(在 Shiny 应用程序中启动)中的对象,或者仅从在 Shiny 应用程序页面上打开的浏览器控制台访问对象?

gre*_*g L 6

最好的方法可能是监听shiny:inputchanged事件

library(shiny)

shinyApp(
  ui = fluidPage(
    tags$head(tags$script("
      $(document).on('shiny:inputchanged', function(event) {
        console.log(event);
        console.log('[input] ' + event.name + ': ' + event.value);
      });
    ")),
    numericInput("num", "", 0, 0, 5),
    textInput("txt", ""),
    actionButton("action", "Action")
  ),
  server = function(input, output) {}
)
Run Code Online (Sandbox Code Playgroud)

session$sendCustomMessage您还可以使用和从服务器发送输入值Shiny.addCustomMessageHandler


一种未记录的、不太推荐的方法是直接通过 访问输入值Shiny.shinyapp.$inputValues,该对象在name:type键下存储值:

{
  "action:shiny.action": 4,
  "num:shiny.number": 2,
  "txt": "text"
}
Run Code Online (Sandbox Code Playgroud)