Shiny check reactiveValue existence with validate — Not Found

1,409 views
Skip to first unread message

Alperen Taciroglu

unread,
Feb 21, 2016, 3:36:15 PM2/21/16
to Shiny - Web Framework for R

I have a shiny code like in the below. I need to define variables as reactiveValues to be updatable (or I could define them I think as global but then I have to press clean objects from Rstudio which is not very user-friendly).I try to run a validate code to check for existence of the data I have defined as reactiveValues. validate(need(exists("GSEmRNA$d"),message="Dataframe not found")) yields "Dataframe not found" thus, does not plot my boxplot. If I define them as global variables and forget to press clean objects, code might mix up as old data can be passed as if it is new. Any help is appreciated.

server.R

shinyServer(function(input, output) {
  observeEvent(input$GoButton,{
    dataset <- data.frame(first= c(1,5,9),second=c(8,5,13), third=c(10,3,17))
    GSEmRNA <- reactiveValues(d=dataset)
  })
  output$BoxplotDataset <- renderPlot({
    if (input$GoButton== 0) {return()}       
    else{
      validate(need(exists("GSEmRNA$d"),message="Dataframe not found"))
      boxplot(GSEmRNA$d)}
  })
})

ui.R

library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Dataset Selection"),

sidebarPanel(

  actionButton("GoButton","GO")

),
mainPanel(
  wellPanel(
    column(8, plotOutput("BoxplotDataset")
  )
)
)))

## FOR THE RECORD, I ALSO POSTED THIS QUESTION TO STACKOVERFLOW. http://stackoverflow.com/questions/35541662/shiny-check-reactivevalue-existence-with-validate-not-found

Dean Attali

unread,
Feb 21, 2016, 11:33:30 PM2/21/16
to Shiny - Web Framework for R
I haven't fully read and understood your question, but it seems like you're defining GSEmRNA only inside the observer, and trying to access it from somewhere else.  GSEmRNA is not accessible outside of the observer where it's defined.  Just like if you define a variable inside one function, it's not accessible for another function.  Try defining GSEmRNA outside of the observer 

Joe Cheng

unread,
Feb 23, 2016, 1:41:05 AM2/23/16
to Alperen Taciroglu, Shiny - Web Framework for R
This would work but not my preferred approach:

shinyServer(function(input, output) {
  GSEmRNA <- reactiveValues(d=NULL)
  observeEvent(input$GoButton,{

    dataset <- data.frame(first= c(1,5,9),second=c(8,5,13), third=c(10,3,17))

    GSEmRNA$d <- dataset
  })
  output$BoxplotDataset <- renderPlot({
    validate(need(GSEmRNA$d,message="Dataframe not found"))
    boxplot(GSEmRNA$d)}
  })
})

I'd prefer you do this:

shinyServer(function(input, output) {
  GSEmRNA <- eventReactive(input$GoButton, {
    data.frame(first= c(1,5,9),second=c(8,5,13), third=c(10,3,17))
  })
  output$BoxplotDataset <- renderPlot({
    boxplot(GSEmRNA())}
  })
})


--
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/13dc194a-28fa-42ef-9bf3-19fd891b49a7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages