Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collect All user inputs throughout the Shiny App

Tags:

r

shiny

Is there a way to show the value from textInput() elsewhere in the UI without having to go through server.R with something very verbose like the following?

ui.R

library(shiny)
shinyUI(
  fluidPage(
  textInput('text_in', label = 'Write text here'),

  # elsewhere in the UI...

  textOutput('text_out')
))

server.R

library(shiny)
shinyServer(function(input, output) {
  output$text_out = renderText(input$text_in)
})

It's not too bad for this example, but it becomes very verbose when I need to do it many times. My desire is to collect all the inputs the user enters throughout the app and compile them into a nice table at the end so they can confirm everything is laid out right.

I've seen you can reference input elements without going through the server when using a JavaScript expression in conditionalPanel() but I'm not sure how to implement that outside of this specific instance.

like image 496
CephBirk Avatar asked Dec 08 '16 04:12

CephBirk


2 Answers

For accessing all inputs, you can use reactiveValuesToList server-side. You can access input values via Javascript Events like below (I have taken the example from @Pork Chop) :

library(shiny)

ui <- basicPage(

  fluidRow(
    column(
      width = 6,
      textInput('a', 'Text A',"a1"),
      textInput('b', 'Text B',"b1"),
      textInput('c', 'Text A',"c1"),
      textInput('d', 'Text B',"d1"),
      textInput('e', 'Text A',"e1"),
      textInput('f', 'Text B',"f1")
    ),
    column(
      width = 6,
      tags$p("Text A :", tags$span(id = "valueA", "")),
      tags$script(
        "$(document).on('shiny:inputchanged', function(event) {
          if (event.name === 'a') {
            $('#valueA').text(event.value);
          }
        });
        "
      ),
      tableOutput('show_inputs')
    )
  )
)

server <- shinyServer(function(input, output, session){

  AllInputs <- reactive({
    x <- reactiveValuesToList(input)
    data.frame(
      names = names(x),
      values = unlist(x, use.names = FALSE)
    )
  })

  output$show_inputs <- renderTable({
    AllInputs()
  })
})
shinyApp(ui = ui, server = server)
like image 109
Victorp Avatar answered Oct 21 '22 05:10

Victorp


Since your overall objective is to collect all the user inputs and then compile them into a table I will show you how to achieve that with example below. As you can see all of the input variables can be accessed by names from server. I kept them in a reactive just in case you need it for further analysis or for some renderUI functionality.

#rm(list=ls())
library(shiny)

ui <- basicPage(
  textInput('a', 'Text A',"a1"),
  textInput('b', 'Text B',"b1"),
  textInput('c', 'Text A',"c1"),
  textInput('d', 'Text B',"d1"),
  textInput('e', 'Text A',"e1"),
  textInput('f', 'Text B',"f1"),
  tableOutput('show_inputs')
)
server <- shinyServer(function(input, output, session){

  AllInputs <- reactive({
    myvalues <- NULL
    for(i in 1:length(names(input))){
      myvalues <- as.data.frame(rbind(myvalues,(cbind(names(input)[i],input[[names(input)[i]]]))))
    }
    names(myvalues) <- c("User Input","Last Value")
    myvalues
  })

  output$show_inputs <- renderTable({
    AllInputs()
  })
})
shinyApp(ui = ui, server = server)

enter image description here

like image 7
Pork Chop Avatar answered Oct 21 '22 04:10

Pork Chop