简体   繁体   English

只有Shiny会话的“全局”变量

[英]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. 我有多个Shiny应用程序有多个会话,我想有一个全局变量,但只在每个会话中,不要覆盖不同会话之间的值。 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: 假设这是我的server.R文件:

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

and this is the shinyCommons.R functions file that contains non-reactive functions : 这是包含非反应函数shinyCommons.R函数文件:

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. 现在的问题是,如果我使用<<-运算符,并且不同的会话同时处于活动状态,它们会覆盖彼此的reportID值。 But if I put it in a reactive context, then the non-reactive functions can't reach it. 但是如果我把它置于reactive环境中,那么非反应函数就无法达到它。

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 : 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 : 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 . 公共对象在shinyServer的function参数范围内定义。 Then, inside other function/reactive contexts, you can use <<- to update/overwrite the values. 然后,在其他函数/反应上下文中,您可以使用<<-来更新/覆盖值。

Did you try to use the local option while sourcing? 您是否尝试在采购时使用local选项?

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). 请注意,您应该使用< - 而不是<< - 来保持变量在本地环境中(因此在会话中保持独立)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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