0

Is it possible to get some R object used in Shiny?

For example, in the following code, I want to get text string inputted by users. However, the .Last.value dose not the desired text string.

ref

How to store the returned value from a Shiny module in reactiveValues?

Ex code

returnUI = function(id) {
  ns <- NS(id)
  
  tagList(
    textInput(ns("txt"), "Write something")
  )
}

returnServer = function(input, output, session) {
  myreturn <- reactiveValues()
  
  observe({ myreturn$txt <- input$txt })
  
  return(myreturn)
}


library(shiny)
source("modules/module_1.R")

ui <- fluidPage(
  
  returnUI("returntxt"),
  textOutput("mytxt")
  
)

server <- function(input, output, session) {
  
  myvals <- reactiveValues(
    txt = NULL
  )
  
  mytxt <- callModule(returnServer, "returntxt")
  
  observe({ 
    myvals$txt <- mytxt$txt 
    print(myvals$txt)
  })
  
  output$mytxt <- renderText({ myvals$txt })
  
}

shinyApp(ui, server) 

.Last.value
Camford Oxbridge
  • 834
  • 8
  • 21
  • 2
    If you want the value when the shiny app has finished, you'd need something like `https://stackoverflow.com/questions/27365575/how-to-exit-a-shiny-app-and-return-a-value`. The `.Last.value` variable only works with values run in the console. – MrFlick Dec 22 '20 at 18:19
  • Thank you. I will try your suggesting page and this question may be duplicated. It should be closed... or I will delete this question. – Camford Oxbridge Dec 23 '20 at 05:50
  • @CamfordOxbridge, I don't think this is a duplicate question, and it is possible to pass R objects from a Shiny app back to your normal workspace to inspect them (see my answer). – da11an Dec 23 '20 at 06:19

1 Answers1

1

Yes, you can push variables to the global environment (your usual workspace) from a Shiny app running in your console:

library(shiny)

ui <- fluidPage(

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),
        mainPanel(NULL)
    )
)

server <- function(input, output) {

    observe({
        my_global_env <- globalenv()
        my_global_env$x <- input$bins
    })
}

shinyApp(ui = ui, server = server)
da11an
  • 701
  • 4
  • 8