0

I'm trying to find my way around building some basic shiny tools to display some of the data I'm generating and analysis. Part of the intended workflow is to use one app for the user to select the data that should be displayed and then call a second module that would be specific for the dataset selected. I'm playing around with a toy-example to figure out how to collect inputs and then start a new module based on that info.

I got everything about the data input working, but I am not able to figure out how to pass information from the shinyApp(ui = ui_parameter_selection, server = server_parameter_selection) call on to the main code. Specifically, I would like to retrieve the last values for input$number, input$range[[1]], and input$range[[2]] once the shinyApp is closed and continue working with them.

library(shiny)

#// Define UI for game inputs ----
ui_parameter_selection <- fluidPage(

     #// App title ----
     titlePanel("Learn Spanish numbers"),
     
     #// What's the diff between sidebar and fluidrow
     fluidRow ( 
     
          #// Input: Sliders for the number of guesses and range ----
          
          #// column width and title
          column(10, h3("Let's play a game!"),
                
               #// Input: Slider1 for the number of guesses ---- 
               sliderInput("number",
               #// Slider title and parameters ----
               h4("How many numbers?"),  
               min = 0,
               max = 25,
               value = 10
               ), #// end slider1 input
          
               #// Input: Slider2 for the range of numbers ----  
               sliderInput("range", 
               #// Slider title and parameters ----
               h4("What range of numbers?"),
               min = 0,
               max = 100,
               value = c(25, 75)
               ), #// end slider1 input
                
          ), #// end column
 
          #// Column width and title for output panel ----
          column(10, h3("This is your selection!"),
          
               #// Output: Text ----
               textOutput(outputId = "out_num"),
               br(), 
               textOutput(outputId = "out_range"),
               
               #// action button: Let's play ----
               br(), 
               br(), 
               actionButton("action", "Let's Play")
          
          ) #// end column
      ) #// end fluidrow
) # end fluidpage

# Define server logic ----
server_parameter_selection <- function(input, output, session) { 
 
     observeEvent( input$action, stopApp() )
   
     #// defines output for "number"
     output$out_num <- renderText({
          #// number of games
          paste("Number of games: ", input$number)
     })

     #// defines output for "number"
     output$out_range <- renderText({
          #// range of numbers
          paste("Numbers will range from ", input$range[[1]], "to", input$range[[2]] )
     })
   
}

# Run the app ----
shinyApp(ui = ui_parameter_selection, server = server_parameter_selection)

I read that the output needs to be set as reactive to be passed on, but neither out <- reactive({input$number}) or return(reactive({input$number})) included in the server function seem to work. I'm obviously not getting something basic about how values are passed on from the shinyApp call, but I thought that shinyApp is simply a function that can be made to return a value. And input/output seem to be lists that shuttle the data between the server and UI part of the shiny App. So I figure if all of input gets returned I should be able to extract all parameters I need?

Mario Niepel
  • 1,095
  • 4
  • 19
  • What are you looking to do? What is your expected output? – Ronak Shah Jan 02 '21 at 03:52
  • Sorry, I wasn't clear. I want to retrieve the parameters that were selected as input$number, input$range[[1]], input$range[[2]] and continue working with them once the shinyApp is closed. I'll clarify in the post. – Mario Niepel Jan 02 '21 at 04:36

1 Answers1

0

You can use <<- to get access of values outside shiny. Change the server function to:

server_parameter_selection <- function(input, output, session) { 
  
  observeEvent( input$action, stopApp() )
  
  #// defines output for "number"
  output$out_num <- renderText({
    #// number of games
    num <<- input$number
    paste("Number of games: ", input$number)
    
  })
  #// defines output for "number"
  output$out_range <- renderText({
    #// range of numbers
    val <<- input$range
    paste("Numbers will range from ", input$range[1], "to", input$range[2])
    
  })
  
}

After you close the app you can get the last value selected in num and val.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Ugh, so easy! Thank you. So the double arrow in this case defines the variables `num` and `val` not in the environment of the function but of the global environment? Or just in the environment that's 'one level higher' (i.e. the environment that called the function)? – Mario Niepel Jan 02 '21 at 05:04
  • 1
    `<<-` saves the values in the environment that is one level higher. It might not necessarily be global environment always. – Ronak Shah Jan 02 '21 at 05:06
  • It seems like this is an easier solution in some cases when a custom function may have to return multiple disparate values that gets dumped into a list within the function and then has to be pulled back apart once the list is returned. But I guess just assigning the multiple different variables via `<<-` is not the preferred solution because it's easier to overwrite already existing variables? – Mario Niepel Jan 02 '21 at 05:12
  • Yes, usually it is not preferred to use `assign` or `<<-`. This post discusses that https://stackoverflow.com/questions/17559390/why-is-using-assign-bad – Ronak Shah Jan 02 '21 at 07:16