Sri
Sri

Reputation: 1160

R shiny environments

I'm little confused about environment scoping in Shiny applications. I read that any object defined outside of shinyServer function in server.R is available to all user sessions. But if I create an object using assign function and envir=.GlobalEnv option, is that object available to other user sessions?

I want to create some objects within shinyServer function and retain them between user clicks but not share them with other user sessions - how can I achieve this?

Is the Global Environment in R shiny user session, the parent environment to the environment in which all objects are created within shinyServer function?

Appreciate any help in clarifying this.

Upvotes: 2

Views: 1125

Answers (1)

ThankGoat
ThankGoat

Reputation: 666

below I gave an example with comments at the position that correspond to the different environments in shiny. It is quite straightforward actually.

Also refer to the cheat sheet provided by the RStudio core team:

http://shiny.rstudio.com/articles/cheatsheet.html

# This will only run once when the app is launched.
# Load libraries, data or other objects that should be
# available globally for all users/sessions.

shinyServer(function(input, output) {

  # User/session specific objects go here.
  # This will be run each time a user visits the app or 
  # reloads the browser.

  output$text <- renderText ({

    input$myInput

    # This is a reactive object so this code will
    # be run everytime the parameter myInput is changed.
    # The objects inside the render element or not available
    # outside of the function.

    })
 })

Upvotes: 1

Related Questions