Saving a Reactive Variable as a Global Variable

4,419 views
Skip to first unread message

Howard S

unread,
Apr 2, 2015, 3:16:14 PM4/2/15
to shiny-...@googlegroups.com

I have a reactive variable called "dataset" in server.R. I want to save this reactive variable as a global variable called "HELLOKITTY". Then, on ui.R, I wanted to use this "HELLOKITTY" global variable. However, I am unable to do so and get the following error message: object "HELLOKITTY" not found.

Does anyone know the solution to this? Please and thank you.

server.R
ui.R

Joe Cheng

unread,
Apr 2, 2015, 4:55:41 PM4/2/15
to Howard S, shiny-...@googlegroups.com
By the time server.R begins executing, ui.R is already done. So it doesn't make sense to try to use a reactive expression or value from ui.R; it has no way to respond to any changes that happen in the future.

Instead, check out renderUI, which should do what you want.

Alternatively, if the value is something that won't change over time, you can define the variable in global.R and then access it from both ui.R and server.R.

On Thu, Apr 2, 2015 at 12:16 PM, Howard S <feelos...@gmail.com> wrote:

I have a reactive variable called "dataset" in server.R. I want to save this reactive variable as a global variable called "HELLOKITTY". Then, on ui.R, I wanted to use this "HELLOKITTY" global variable. However, I am unable to do so and get the following error message: object "HELLOKITTY" not found.

Does anyone know the solution to this? Please and thank you.

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/e85f2a33-f977-46fd-b270-579fe49eda31%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

howar...@formsdirect.net

unread,
Apr 3, 2015, 3:02:31 AM4/3/15
to shiny-...@googlegroups.com, feelos...@gmail.com
Hi Joe. Thanks for the reply. Is there another option other than renderUI? I am not sure if renderUI would be the solution. The reactive variable stores a dataset that I'm uploading (CSV) from a local machine; I'm not just manipulating the UI/interface.

I apologize if that makes little sense. I don't have enough grasp on shiny's scoping rules to be able to perfectly describe my needs. :(

Best,
Howard

Howard S

unread,
Apr 3, 2015, 9:44:42 PM4/3/15
to shiny-...@googlegroups.com, feelos...@gmail.com
Ah, you were right. renderUI did the trick. Thank you for pointing me to the right direction.

Best,
Howard

On Thursday, April 2, 2015 at 1:55:41 PM UTC-7, Joe Cheng [RStudio] wrote:

Joe Cheng

unread,
Apr 7, 2015, 1:47:43 AM4/7/15
to howar...@formsdirect.net, shiny-...@googlegroups.com, Howard S
Usually you'll just wrap it in a reactive expression, and use that from everywhere. There is such thing as reactive values but I'm not sure they're needed here.

shinyServer(function(input, output, session) {

  helloKittyData <- reactive({
    validate(need(input$file1, "Please upload a file"))

    read.csv(input$file1$datapath)
  })

  output$contents <- renderTable({
    helloKittyData()
  })

  output$dynamicUI <- renderUI({
    checkboxGroupInput("columns", "Choose columns", names(helloKittyData()))
  })
})

Reply all
Reply to author
Forward
0 new messages