title: Is it possible to initialize Shiny input values without including them in the ui?
body: <p>I'm building a Shiny app where elements of the output can be clicked to set input variables. I'm doing so by including some Javascript that calls the <code>Shiny.setInputValue</code> method.</p>
  
  <p>I'd like to initialize these values but don't want the user to be aware of them, since they're only used behind the scenes. My current hack is to use the <code>hidden</code> function from the <code>shinyjs</code> library around some buttons in the ui, but that's not very satisfying. Here's an example of my approach:</p>
  
  <pre><code>library(shiny)
  library(shinyjs)
  library(tidyverse)
  
  ui &lt;- fluidPage(
    useShinyjs(),
    hidden(
      numericInput(label = NULL, inputId = 'INPUT', value = 0)
    ),
    DT::dataTableOutput("one_row")
  )
  
  server &lt;- function(input, output) {
    script &lt;- 'Shiny.setInputValue("INPUT", Math.random());'
    btn &lt;- as.character(actionButton(inputId = 'button', 
                                     label = "Click me!", 
                                     onclick = script))
    one_row &lt;- iris[1, ] %&gt;% mutate(button = btn)
    output$one_row &lt;- DT::renderDataTable({
      print(input$INPUT)
      one_row}, escape = FALSE)
  }
  
  shinyApp(ui = ui, server = server)
  </code></pre>
  
  <p>So my main question is: is there a better way to set the default of these "invisible" inputs? Should I just use the <code>runjs</code> function somewhere in the app?</p>
  
  <p>A related question is: is it possible to access these input values using javascript? I was hoping there was something like <code>Shiny.getInputValue</code> but there doesn't seem to be.</p>
  
  <p>EDIT:</p>
  
  <p>Here's a more thorough example that shows precisely what I'm trying to do. I'm using<code>visNetwork</code>, but I'd like to override some of the default behavior. Usually, when you click on a node it shows all connected edges, incoming or outgoing.</p>
  
  <p>I'd like a node selection to show only edges in one direction, and then to show the other direction if you click again (in, out, in, out, ...). To do so, I need to keep track of clicks (<code>click</code>), which node is selected (<code>selected_node</code>), and what direction to currently show (<code>out</code>).</p>
  
  <p>To accomplish this, I'm currently using <code>visEvents</code> to redefine what happens on a click and <code>visNetworkProxy</code> to select nodes and edges based on that click. </p>
  
  <pre><code>library(shiny)
  library(shinyjs)
  library(tidyverse)
  library(visNetwork)
  
  ui &lt;- fluidPage(
    useShinyjs(),
    hidden(
      numericInput(label = NULL, inputId = 'click', value = 0),
      numericInput(label = NULL, inputId = 'selected_node', value = 0)
    ),
    visNetworkOutput("network")
  )
  
  server &lt;- function(input, output) {
  
    network &lt;- reactive({
      nodes &lt;- data.frame(id = 1:3)
      edges &lt;- data.frame(expand.grid(to = 1:3, from = 1:3)) %&gt;%
        mutate(id = row_number())
      list(nodes = nodes, edges = edges)
      })
  
    output$network &lt;- renderVisNetwork({
  
      network &lt;- network()
  
      visNetwork(network$nodes, network$edges) %&gt;% 
        visPhysics(enabled = FALSE) %&gt;%
        visLayout(randomSeed = 123) %&gt;%
        visEdges(smooth = list(type = 'curvedCW'),
                 arrows =list(to = list(enabled = TRUE, scaleFactor = 0.5))) %&gt;% 
        visEvents(type = "on", 
                  click = "function (properties) {
                            var nodeID = properties.nodes[0]
                            var edgeID = properties.edges[0];
                            if (nodeID) { Shiny.onInputChange('click', 1, {priority: \"event\"});
                                          Shiny.onInputChange('selected_node', nodeID)} 
                            else if (edgeID){ Shiny.onInputChange('click', 2);}
                            else { Shiny.onInputChange('click', 3);}}")
    })
  
    out &lt;- FALSE
  
    observe({
      out &lt;&lt;- !out
      nodeID &lt;- input$selected_node
      network &lt;- network()
  
      # only fire if node was selected
      if(input$click == 1){
        edgeIDs &lt;- network$edges %&gt;% filter(to == nodeID) %&gt;%
          pull(id)
  
        if(out){edgeIDs &lt;- network$edges %&gt;% filter(from == input$selected_node) %&gt;% pull(id)}
  
        visNetworkProxy("network") %&gt;%
          visSetSelection(nodesId = nodeID, edgesId = edgeIDs, highlightEdges = FALSE)
      }
    })
    }
  
  shinyApp(ui = ui, server = server)
  </code></pre>