0
votes

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?

1
What are you looking to do? What is your expected output? - Ronak Shah
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

1 Answers

0
votes

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.