简体   繁体   中英

Having a “global” variable only for session in Shiny

I have multiple Shiny apps with multiple sessions, and I would like to have a global variable but only across each session, not to override values between different sessions. Due to that requirement, I cannot use the <<- assignment operator.

The reason I need it is that I have several variables that are being used by many sourced functions, and I don't want to send them as a parameter to all of the functions.

Any ideas on how to do that?

EDIT

I created a simple example to better explain my problem. Let's say this is my server.R file:

shinyServer(function(input, output, session) {
  source('shinyCommons.R')
  reportId <<- generateReportID()
  createLogFile()
})

and this is the shinyCommons.R functions file that contains non-reactive functions :

createLogFile <- function()
{
  system(paste(touch,reportId,".log",sep=""))
}

Now the problem is, that if I use the <<- operator, and different sessions are active at the same time, they override each other's reportID value. But if I put it in a reactive context, then the non-reactive functions can't reach it.

Can someone help me to understand how should I design it? BTW - I know I can send it as a param to the function, but this is just a small example, I have a lot of vars and a lot of functions that use them

Apparently, I won the bet: you are using incorrectly the <<- operator. Here is a working example.

In ui.R :

barraLaterale<-sidebarPanel(
    fluidRow(column(numericInput("numObs",label="Num Obs.",value=10000,min=100,step=1),width=6),column(helpText("Something"),actionButton("Bottone",label="Go!"),width=6)),
    sliderInput("media",label="Pick gaussian mean",min=-50,max=50,value=0),
    sliderInput("varianza",label="Pick gaussian standard deviation",min=0,max=10,value=5)
)
principale<-mainPanel(plotOutput("plotRisultato"),plotOutput("plotEsatto"))
shinyUI(fluidPage(
    titlePanel("Applicazione Prova"),
    sidebarLayout(barraLaterale,principale)
))

In server.R :

shinyServer(function(input, output) {
    #HERE WE DEFINE COMMON OBJECTS
    object<-0
    calcolaIstogramma<-reactive({
        rnorm(input$numObs,input$media,input$varianza)  
    })
    output$plotRisultato<-renderPlot({
        a<-input$Bottone
        variabile<-isolate(calcolaIstogramma())
        hist(variabile,breaks=50,col="blue")
    })
    output$plotEsatto<-renderPlot({
        a<-input$Bottone
        variabile<-isolate(calcolaIstogramma())
        #HERE WE ARE UPDATING THE VALUES
        object<<-object+1
        cat(object,"\n")
        plot(variabile,xlab="Variable trace",ylab="",ty="l")
    })
})

Open a couple of sessions when you run the app. Every time you press the button, you should see the counter on the shell. You can see that counters are not shared. Common objects are defined in the scope of the function argument of shinyServer . Then, inside other function/reactive contexts, you can use <<- to update/overwrite the values.

Did you try to use the local option while sourcing?

This should work:

shinyServer(function(input, output, session) {
    source('shinyCommons.R', local=TRUE)
    reportId <- generateReportID()
    createLogFile()
})

Please note that you should use <- instead of <<- to keep the variable in the local environment (and therefore to be independent across sessions).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM