Is there a way to get the list of input values in R shiny

Hi all,

below is the sample UI. I wanted to check if there is way to list the values of input values. For example

ui.R

library(shiny)


# Define UI for application that draws a histogram
shinyUI(fluidPage(
  
  # Application title
  titlePanel("Old Faithful Geyser Data"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
       selectInput("n", factors(iris$Species)),
                   actionButton("add", "Add")
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      verbatimTextOutput("sum", placeholder = TRUE)
    )
  )))

Expected Output
Since we have iris$Species as one of the inputs. Is there a way to list them in R console?

input$a
setosa, versicolor and virginica
1 Like

Try using print(input$n) command in one of the reactive elements on the server side.

Thanks!
Heramb

Thanks for this. I did this actually :blush: but wanted to check if I can get outside server side. I mean in r console?

I am not able to understand. print will actually print it in console. If you mean, you want to capture in a variable use <<- operator to export it in globalEnv

Outside of server, you will have to use print(isolate(input$n)). But this will capture only the first output and will no longer remain reactive.

Let me know if this helps!

-Heramb

Will check. Actually friend :blush: my question was little tricky. Let me put it this way. Say I have an application like above. I have not run the application. But still I want to know what goes into input$a. This is actually I meant by outside server side​:blush:

Interesting. Others might be able to help you out with this. My question is, why do you want to do that?

I am trying to build a program/function where in it lists all inputs and check whether we are getting desired output or not. This is my plan

Given a predefined UI looking like:

library(shiny)

ui <- fluidPage(
  titlePanel("Old Faithful Geyser Data"),
  sidebarLayout(
    sidebarPanel(
      selectInput("n", label = "Select", choices = unique(factor(iris$Species))),
      actionButton("add", "Add")
    ),
    mainPanel(
      verbatimTextOutput("sum", placeholder = TRUE)
    )
  )
)

We could do:

library(xml2)

parsed_html <- read_html(as.character(ui))

xml_find_all(parsed_html, "//*[@id='n']//option") %>% 
  xml_attr(attr = "value")
# [1] "setosa"     "versicolor" "virginica"

Thanks for your time. I replicated the same to my another application

xml_find_all(parsed_html, "//*[@id='Tic']//option") %>% xml_attr(attr = "value")
character(0)

i only changed Tic

Is "Tic" of type selectInput?

I never thought to try this before, but it seems to work, listing all the things that can be referenced by input$mywhatever.

#somewhere in your reactive server code
 list_of_inputs <- reactiveValuesToList(input)
    print(list_of_inputs) # send to console
# or to have access to it globally when have run your program and exited it
 list_of_inputs <<- reactiveValuesToList(input)

Yes it is :slight_smile: ......................

full example :

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("Old Faithful Geyser Data"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30),
      textInput(inputId = "mytextinput",label="mytextinput",value="text"),
      numericInput(inputId="mynumericinput",label="mynumericinput",value = 5)
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  output$distPlot <- renderPlot({
    # generate bins based on input$bins from ui.R
    x    <- faithful[, 2]
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    
    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'darkgray', border = 'white')
  })
  
  print("started shiny")
  observeEvent(input$bins,{
    list_of_inputs <<- reactiveValuesToList(input)
    print(list_of_inputs)
    cat("input$bins: ", input$bins,"\n")
  })

  
}

# Run the application 
shinyApp(ui = ui, server = server)

my console shows:

Listening on http://127.0.0.1:7697
[1] "started shiny"
$mytextinput
[1] "text"

$mynumericinput
[1] 5

$bins
[1] 30

Thanks a lot Nir. I did try this approach. But actually before running the application it self I should be able to get those. Like what Aurele did😊 thanks for this Aurele. But really not sure why it is not working in my system

Ok, so I feel like I should just make a gentle warning, that it would only be possible for a given complexity of app to do exactly what you desire. I believe this is because apps can be dynamic, they can have modules, inputs can exist and disappear, you can work with some apps, where you have 20 inputs and then 10 minutes later 7 inputs or 70 or whatever... So just to caution that approaches of scanning the top level UI is only practical for simpler cases of static apps.

This is even truer for any app that wants to read in available selection choices for some input$s from columns from tables that haven't even been loaded yet...

Thanks for the suggestion.

Perfect and it is working. Thanks a lot. But in case of all input types, like checkboxinput, radiobutton etc... can we list them as well?

Hi all,

Hope you are having good weekend.

I wanted to check with you all that is there a way to check all inputs and outputs declared in Shiny app. For example for the below ui.R there are 2 inputs and 1 outputs

ui.r

library(shiny)


# Define UI for application that draws a histogram
shinyUI(fluidPage(
  
  # Application title
  titlePanel("Old Faithful Geyser Data"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
       numericInput("n", "Number to add", 5),
                   actionButton("add", "Add")
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      verbatimTextOutput("sum", placeholder = TRUE)
    )
  )))

Expected output

Inputs 
>"n", "add"

outputs
>"sum"

Maybe you can try reactiveValuesToList and work on the result of this function in shiny

1 Like

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.