How to get a equivalent of global variable like $_SESSION in PHP ?

403 views
Skip to first unread message

Grégoire Vincke

unread,
Dec 12, 2013, 6:01:41 PM12/12/13
to shiny-...@googlegroups.com
Hi,

http://sites.uclouvain.be/selt/shiny/testhypic/ is a simulation to understand the dynamic caracteristics of confidence intervals of the mean.

Source code is available here : https://github.com/uclouvain-selt/shiny/tree/master/testh

In this animation, each time the button "Echantillonner" (means "get samples") is hit, a sample is created as a vector of values, and this vector is added to a global list defined as SP$samples.z<<-list() where SP is a global list defined as SP<<-list()

The <<- method of attribution allows to store the content of this list between hits of the button, allowing the list to be implemented at each hits.

Everything works fine unless the sample list stored has this is NOT specific to a client (a user using a browser, with a unique IP), but is shared between all clients.

That's NOT what i expected as the aim is to ask arround 1000 students to play with, so it need to store the samples regarding to the client . I was thinking that <<- attribution was working like $_SESSION in php but it's not.

How can i create a global list specific for each clients, and that can be implemented at each hit of the button ?

Is the a variable session like to use in addition of input and output ?

I hope my explanations are clear :/

Thanks for advises,

Grégoire

Yihui Xie

unread,
Dec 12, 2013, 8:50:32 PM12/12/13
to Grégoire Vincke, shiny-discuss
The tutorial has covered this topic:
http://rstudio.github.io/shiny/tutorial/#scoping In particular, the
subsection "Per-session objects".

Regards,
Yihui

Vincke Grégoire

unread,
Dec 13, 2013, 3:16:06 AM12/13/13
to shiny-discuss
Hi,

Damn, I missed that part of the tutorial... :-(
Thanks for mentioning it, I will read it, test, and come back only if I
don't succed to achieve my goal.

Regards,

Gr�goire


Le 13/12/2013 02:50, Yihui Xie a �crit :
> The tutorial has covered this topic:
> http://rstudio.github.io/shiny/tutorial/#scoping In particular, the
> subsection "Per-session objects".
>
> Regards,
> Yihui
>
>
> On Thu, Dec 12, 2013 at 5:01 PM, Gr�goire Vincke
>> Gr�goire
>

Grégoire Vincke

unread,
Dec 13, 2013, 4:01:52 PM12/13/13
to shiny-...@googlegroups.com

Well, I just went to RTFM, but i still stay without a solution...

If I have well understood, what i've done is the only solution to pass a variable from inside the ShinyServer function to the next execution of this ShinyServer function (understand : the next page call).
What i've done ?
1) define a list outside ShinyServer() : samples <-list()
2) during ShinyServer() exécution, create some samples (vectors of z values from Z~N(0,1) ), and, store them in the list defined outside ShinyServer() to be able to use theses samples at the next call of the ShinyServer() : samples<<-c(samples, z.values)

PROBLEM : storing the samples as this makes them available for ALL sessions of ALL users. Each user adding samples to the same common list. I want to store the samples for ALL sessions of ONE client (couple IP-browser). Each user having his OWN list of samples.

The only solution that came to my mind is to store the samples inside a key specific for the client, the IP for exemple : samples$127.0.0.1<<-c(samples$127.0.0.1,z.values)

The question is now : How can I create a client specific key to store this client samples only availables for him ? It should be possible to ask him to fill a field, but I prefer an automatic solution. Is it possible to get IP from inside the ShinyServer() function for exemple ?

Thanks for advises,

Gregoire






Le vendredi 13 décembre 2013 09:16:06 UTC+1, Grégoire Vincke a écrit :
Hi,

Damn, I missed that part of the tutorial... :-(
Thanks for mentioning it, I will read it, test, and come back only if I
don't succed to achieve my goal.

Regards,

Gr�goire


Le 13/12/2013 02:50, Yihui Xie a �crit :
> The tutorial has covered this topic:
> http://rstudio.github.io/shiny/tutorial/#scoping In particular, the
> subsection "Per-session objects".
>
> Regards,
> Yihui
>
>
> On Thu, Dec 12, 2013 at 5:01 PM, Gr�goire Vincke

>> Hi,
>>
>> http://sites.uclouvain.be/selt/shiny/testhypic/ is a simulation to
>> understand the dynamic caracteristics of confidence intervals of the mean.
>>
>> Source code is available here :
>> https://github.com/uclouvain-selt/shiny/tree/master/testh
>>
>> In this animation, each time the button "Echantillonner" (means "get
>> samples") is hit, a sample is created as a vector of values, and this vector
>> is added to a global list defined as SP$samples.z<<-list() where SP is a
>> global list defined as SP<<-list()
>>
>> The <<- method of attribution allows to store the content of this list
>> between hits of the button, allowing the list to be implemented at each
>> hits.
>>
>> Everything works fine unless the sample list stored has this is NOT specific
>> to a client (a user using a browser, with a unique IP), but is shared
>> between all clients.
>>
>> That's NOT what i expected as the aim is to ask arround 1000 students to
>> play with, so it need to store the samples regarding to the client . I was
>> thinking that <<- attribution was working like $_SESSION in php but it's
>> not.
>>
>> How can i create a global list specific for each clients, and that can be
>> implemented at each hit of the button ?
>>
>> Is the a variable session like to use in addition of input and output ?
>>
>> I hope my explanations are clear :/
>>
>> Thanks for advises,
>>
>> Gr�goire
>

Stéphane Laurent

unread,
Dec 13, 2013, 4:22:34 PM12/13/13
to shiny-...@googlegroups.com
I'm not sure to understand, but instead of using the global environment, couldn't you generate a new environment with a random name to store the simulations ? (a random name among a sufficiently large number of possibilities to avoid two similar names). 

Yihui Xie

unread,
Dec 13, 2013, 5:25:43 PM12/13/13
to Grégoire Vincke, shiny-discuss
Regarding the IP address, I'm afraid it is not possible without Shiny
Server: https://groups.google.com/forum/#!topic/shiny-discuss/EGQhEyoEk3E

I guess IP does not always reflect the same user. Multiple users in
the same local network can have the same public IP address, or even
share the same computer to interact with your app. Perhaps the only
reliable approach to identify clients is to let clients identify
themselves, e.g. through registration. Otherwise you have to make
assumptions.

Regards,
Yihui

Stéphane Laurent

unread,
Dec 13, 2013, 5:37:49 PM12/13/13
to shiny-...@googlegroups.com, Grégoire Vincke
Is there such a problem if we use a reactive list instead of a list in the global environment ?

Grégoire Vincke

unread,
Dec 15, 2013, 5:09:21 PM12/15/13
to shiny-...@googlegroups.com, Grégoire Vincke
Thanks Stéphane,

In fact, this was the solution of my problem. ;-)

Your comment triggered my curiosity, so I re-read this part of the tutorial, made some small test, and it gives me exactly what I whanted.

The code is updated on github : https://github.com/uclouvain-selt/shiny/tree/master/testhypmu

The browsable page was also updated : http://sites.uclouvain.be/selt/shiny/testhypmu/

I don't understand why reactive values are not specified in the Scope part of the tutorial as variable able to store values, lists, or vectors, between page calls of a single visitor, which is mostly important when pages are hosted with shiny-server.

If my animation was only used locally, the global method was really well fitted. But since it have to be deployed on the web with shiny-server (my own), and taht quite 1000 student have to use it several time a year, sharing values with global method is not acceptable. Reactive values made the deal.

Thanks guys for have drived me to the solution.

Gregoire

Stéphane Laurent

unread,
Dec 16, 2013, 4:15:43 AM12/16/13
to shiny-...@googlegroups.com, Grégoire Vincke
Actually I don't see what prevents you to use a classical (non-reactive) list in the local environment of the shinyServer() function, but I confess I have not looked at your code. Anyway this is surely more convenient with a reactive list.

Grégoire Vincke

unread,
Dec 16, 2013, 4:23:58 AM12/16/13
to shiny-...@googlegroups.com, Grégoire Vincke
Each time a visitor hit the "Get Sample" button, one or more samples of Z~N(0,1) values are created, and added to the previous values that have been created during previous hits on the button.
A classical list in the shinyServer() can not be used because its content will not be kept in memory between two buttons hits (i.e. two page calls).

Gregoire

Stéphane Laurent

unread,
Dec 16, 2013, 4:38:46 AM12/16/13
to shiny-...@googlegroups.com, Grégoire Vincke
What do you mean by "page call" ? This is when the user goes to another tab ? 

Stéphane Laurent

unread,
Dec 16, 2013, 4:51:38 AM12/16/13
to shiny-...@googlegroups.com, Grégoire Vincke
Ok I see now (experimental code below). I didn't know that.

library(shiny)

runApp(list(
  server=function(input, output, session) {
    x <- 0
    output$x <- renderPrint({
      input$go
      x <- c(x, input$go)
      x
    })
  },
  
  ui=pageWithSidebar(
    
    headerPanel(""),
    
    sidebarPanel(
      
      actionButton("go", "Go")
      
    ),
    
    mainPanel(
      verbatimTextOutput("x")
    )
  )
))

Yihui Xie

unread,
Dec 17, 2013, 1:13:32 AM12/17/13
to Stéphane Laurent, shiny-discuss, Grégoire Vincke
You can use x <<- c(x, input$go).

Or use an environment, e.g. env <- new.env(); env$x <- c(env$x, input$go).

Regards,
Yihui

Stéphane Laurent

unread,
Dec 17, 2013, 2:28:29 AM12/17/13
to shiny-...@googlegroups.com, Stéphane Laurent, Grégoire Vincke
Yes, actually I had already encountered this problem when I did this app:  http://glimmer.rstudio.com/stla/reactive1/ 

But if we do  x <<- c(x, input$go), the same problem as Grégoire will occur, no ? Or there's no problem when we do a global assignment inside the body of shinyServer() ?

Yihui Xie

unread,
Dec 17, 2013, 2:47:27 AM12/17/13
to Stéphane Laurent, shiny-discuss, Grégoire Vincke
"<<-" is commonly misunderstood by R users. It is not an operator for
"global assignment". It only means a non-local assignment. When the
object is not defined in any of the parent environments, <<- becomes a
global assignment operator. In your case, as long as you have defined
"x" in a parent environment, you can use <<- to modify it in child
environments. See ?`<<-` for a detailed explanation.

ls()
f <- function() {
x <- 0
g <- function() {x <<- 1}
g()
x
}
f()
ls()

Regards,
Yihui

Vincke Grégoire

unread,
Dec 17, 2013, 3:18:21 AM12/17/13
to shiny-discuss
Thanks Yihui for this complementary explanaition.

May I suggest to change the "Scoping" section of the tutorial regarding
to this ?

http://rstudio.github.io/shiny/tutorial/#scoping

Because this tutorial describes "<<-" only for "Objects visible across
all sessions", which is not that you described.

Thanks for your return,

Gregoire



Le 17/12/2013 08:47, Yihui Xie a �crit :
> "<<-" is commonly misunderstood by R users. It is not an operator for
> "global assignment". It only means a non-local assignment. When the
> object is not defined in any of the parent environments, <<- becomes a
> global assignment operator. In your case, as long as you have defined
> "x" in a parent environment, you can use <<- to modify it in child
> environments. See ?`<<-` for a detailed explanation.
>
> ls()
> f <- function() {
> x <- 0
> g <- function() {x <<- 1}
> g()
> x
> }
> f()
> ls()
>
> Regards,
> Yihui
>
>
> On Tue, Dec 17, 2013 at 1:28 AM, St�phane Laurent <lauren...@yahoo.fr> wrote:
>> Yes, actually I had already encountered this problem when I did this app:
>> http://glimmer.rstudio.com/stla/reactive1/
>>
>> But if we do x <<- c(x, input$go), the same problem as Gr�goire will occur,
>> no ? Or there's no problem when we do a global assignment inside the body of
>> shinyServer() ?
>>
>>
>> Le mardi 17 d�cembre 2013 07:13:32 UTC+1, Yihui Xie a �crit :
>>>
>>> You can use x <<- c(x, input$go).
>>>
>>> Or use an environment, e.g. env <- new.env(); env$x <- c(env$x, input$go).
>>>
>>> Regards,
>>> Yihui
>>>
>>>
>>> On Mon, Dec 16, 2013 at 3:51 AM, St�phane Laurent <lauren...@yahoo.fr>

Yihui Xie

unread,
Dec 19, 2013, 3:53:17 PM12/19/13
to Vincke Grégoire, shiny-discuss
I read the tutorial again and I think it is accurate. In the tutorial,
the object bigDataSet was defined outside of shinyServer(), and you
can modify it inside shinyServer() using <<-. That is basically what I
said: <<- is for non-local assignments.

Regards,
Yihui
Reply all
Reply to author
Forward
0 new messages