RSzT
RSzT

Reputation: 337

Shiny: access input object from own JS script

In Shiny conditionalPanel in condition arg it is possible to access JS input object, so in case of having for example numericInput("num", label = h3("Numeric input"), value = 1) widget, value of this widget is reachable in condition JS expression via input.num.

The question is how to access, in the same way, input object in own JS script (launched within Shiny app) or just from the browser's console opened on the Shiny app page?

Upvotes: 5

Views: 1692

Answers (1)

greg L
greg L

Reputation: 4124

The best way is probably by listening for shiny:inputchanged events.

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) {}
)

You could also send input values back up from the server using session$sendCustomMessage and Shiny.addCustomMessageHandler.


An undocumented, much less recommended way would be to access the input values directly through Shiny.shinyapp.$inputValues, an object that stores values under name:type keys:

{
  "action:shiny.action": 4,
  "num:shiny.number": 2,
  "txt": "text"
}

Upvotes: 6

Related Questions