updating global variables

2,937 views
Skip to first unread message

Sam

unread,
Mar 31, 2014, 4:29:08 AM3/31/14
to shiny-...@googlegroups.com
Hi all

I would like to find some idea to help me on the following point : 
In my ShinyApp, I have a global variable from which I build some summaries and graphs in tabPanels.
This variable may be modified by a click on an ActionButton.
I would like that all graphs and text be updated when a change in this variable has been detected

Here is an extract of my code in server.R : 
Server.R

shinyServer(function(input, output, session) {
#initialisation of 'obj' variable
obj <- read.csv("file") # current obj which may be modified
original <- read.csv("file") ## backup of the original variable


#------ to return to the original object---------------
observe({
  input$GetOriginalDataButton
  if (input$GetOriginalDataButton == 0) {
     return(NULL)
  }
  obj <<- objOriginal
}


#---------- view the content of the variable 'obj' --------------------------#
output$MSnsetView <- renderPrint({
  input$GetOriginalDataButton
  if (input$openVisualiseButton == 0) {return(NULL)}

 obj
})


#------------ modification of the variable -------------------------#
  observe({
    input$ValidateFilters
     if (input$ValidateFilters == 0) {return(NULL) }

    #code to modify the variable
    obj <-- NewValue
)}

}

ui.R

[...]
actionButton("ValidateFilters","Valider les filtres"),
actionButton("GetOriginalDataButton","Retour aux donnees originales")
      
main(
tabPanel(title="Open", value = "open", verbatimTextOutput("MSnsetView"))
)
[...]


The first time I click on ValidateFilters, obj is modified and the new variable is shown in the tabPanel MSnsetView. Then, I click on GetOriginalDataButton to return to the original data, the tabPanel is updated correctly. But, from now, each time I click on ValidateFilters, the object 'obj' seems to be correctly updated but the tabPanel is not updated. And I do not undersatnd why.

Is there somebody who can help me ?

Regards

Sam

Joe Cheng

unread,
Mar 31, 2014, 5:10:52 PM3/31/14
to Sam, shiny-...@googlegroups.com
Replace the declaration of obj with this:

values <- reactiveValues(obj = read.csv("file"))

And anytime you want to read/write the variable, refer to values$obj. The reactiveValues function creates a new list-like object on which you can store named values; reads and writes to these named values are reactive-aware.

The way you were doing it doesn't work because "obj" is just a normal, non-reactive variable; Shiny's reactive framework only knows when reactive things change. Inputs and reactive expressions both fall into this category.

The other possibility is a new feature introduced in Shiny 0.9 called "makeReactiveBinding"; just leave your code as it is in your email, and add the line "makeReactiveBinding(obj)" right before the declaration of obj. That will let you use obj as if it is a normal variable, but it's actually a reactive variable. I haven't decided if this is actually a good idea or not (as it's not obvious from looking at a read/write of this variable that anything out of the ordinary is going on), if you want you can give it a try and let me know whether it helps or hurts vs. the reactiveValues approach.



--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sam

unread,
Apr 3, 2014, 3:24:46 AM4/3/14
to shiny-...@googlegroups.com, Sam
ok, I try to use the new functionality.
here is an extract of my code in server.R : 


 currentObj <- NULL
 makeReactiveBinding("currentObj")
#------------------------------------
 observe({ 
    input$openVisualiseButton
    if (input$openVisualiseButton == 0) {return(NULL)}
    
    isolate( input$file)
      currentObj <- readRDS(input$file$datapath)
  })

#------------------------------------
output$MSnsetView <- renderPrint({
  input$openVisualiseButton
  if (input$openVisualiseButton == 0) {return(NULL)}
   currentObj
})


And do not understand why the variable currentObj is not updated and showed in the tabPanel MSnsetView. Even when I clik on the "openVisualiseButton", the panel show "NULL" but currentObj was well updated with  currentObj <- readRDS(input$file$datapath)

Thanks

Sam
Message has been deleted

Paul Curcean

unread,
Apr 3, 2014, 4:46:56 AM4/3/14
to shiny-...@googlegroups.com
Hi,

I had the same problem and I've solved by writing "<<-" instead of "<-"
that means you should write  
      
      isolate( input$file)
      currentObj <<- readRDS(input$file$datapath)
  })


Best regards
Paul

Reply all
Reply to author
Forward
0 new messages