reactive conditional statements?

2,889 views
Skip to first unread message

Loren Santana

unread,
Dec 8, 2013, 5:24:41 PM12/8/13
to shiny-...@googlegroups.com
This is my first time creating a Shiny App so this might not make any sense. 

In the server file, I have if statements with reactive functions within. However, I want one of the functions within to be the output. Below, I've included what I have so far.
I highlighted the portion in the last output that requires the defined functions within the if statement. When I run the app, R can't find the function color() because it's nested inside. Is there another way of doing this? 

Thanks!


shinyServer(function(input, output) {

observe({
  if(input$graph == "advisor"){
  
  s <- reactive({
    as.numeric(unlist(neighborhood(g,1,input$advisor)))
  })
  
  color = reactive({
    ifelse(V(g)$name %in% c(V(g)$name[ s()]), 'red','lightgray')
  })
  
  size = reactive({
    ifelse(V(g)$name %in% c(V(g)$name[s()]), 3,2)
  })
}
})


observe({
  if(input$graph == "school"){
  
  index = reactive({
    which(SmithDataSet$school_id == input$schools)
  })
  
  vertices = reactive({
    SmithDataSet$peopleID[index()]
  })
  
  color = reactive({
    ifelse(V(g)$name %in% vertices(), 'blue','lightgray')
  })
  
  size = reactive({
    ifelse(V(g)$name %in% vertices(), 3,2)
  })
  
}
})

observe({
  if(input$graph == "subject"){
  index = reactive({
    which(SmithDataSet$code == input$subj)
  })
  
  vertices = reactive({
    SmithDataSet$peopleID[index()]
  })
  
  color = reactive({
    ifelse(V(g)$name %in% vertices(), 'green','lightgray')
  })
  
  size = reactive({
    ifelse(V(g)$name %in% vertices(), 3,2)
  })
  
  
}
})



output$network = renderPlot({plot(g, layout = my.layout, edge.color = "lightgray", vertex.color = color(), vertex.size = size(),edge.arrow.size = .002, vertex.label=NA)
 })
 
})

ZJ

unread,
Dec 9, 2013, 12:45:58 AM12/9/13
to shiny-...@googlegroups.com
Is there a need to put all the functions inside an observe({ ... })?

Why don't you restructure you code to something like below.

observe({
  if(input$graph == "subject"){
 })

 index = reactive({
    if(input$graph == "subject") {
      which(SmithDataSet$code == input$subj)
    }
  })
 
  vertices = reactive({
    if(input$graph == "subject") {
      SmithDataSet$peopleID[index()]
   }
  })
  
  color = reactive({
    if(input$graph == "subject") {
    ifelse(V(g)$name %in% vertices(), 'green','lightgray')
  }
  })
  
  size = reactive({
    if(input$graph == "subject") {
    ifelse(V(g)$name %in% vertices(), 3,2)
    }
  })
  
It possible to change the assignment of reactives inside function using <<- e.g.

a <- reactive({ ...some code.. }}

observe({

})

ZJ

unread,
Dec 9, 2013, 12:48:40 AM12/9/13
to shiny-...@googlegroups.com
Accidentally pressed posted...

It possible to change the assignment of reactives inside function using <<- e.g.

a <- reactive({ ...some code.. }}

observe({
  input$something
  a <<- reactive({...some other code ...})
})

but you need some tricky code to make the other components that react to a react as per usual after re-assignment. Also for me it makes the code confusing so it's best avoided.

On Monday, December 9, 2013 6:24:41 AM UTC+8, Loren Santana wrote:

Patrick Toche

unread,
Dec 9, 2013, 1:45:19 AM12/9/13
to shiny-...@googlegroups.com
It's difficult (for me, but I'm a newbie too) to say from what you've posted. I suggest you create a testable app.

Below I created a simple one for you:


You can create another one in a very intuitive manner from gist.github.com

I showed you one way to return the colors from reactive, but there may other ways, and it may depend on what your V(g) and all that are...

Stéphane Laurent

unread,
Dec 9, 2013, 3:25:32 AM12/9/13
to shiny-...@googlegroups.com
First of all, I would use reactiveValues() instead of reactive({}).

Loren Santana

unread,
Dec 9, 2013, 5:21:44 PM12/9/13
to shiny-...@googlegroups.com
Thanks for all the input! I actually just solved the problem. I made it return the plot right away. I've included the changes below. Again, thanks!

shinyServer(function(input, output) {
# Panel 1 ---------------------------------------------------------------------------------------------------------
eas = .4;
 
# plot graph using color() and size ()
output$network = renderPlot({
  if(input$graph == "advisor"){
    s <- reactive({
      as.numeric(unlist(neighborhood(g,1,input$advisor)))
    })
    color = reactive({ifelse(V(g)$name %in% c(V(g)$name[ s()]), 'red','lightgray')})
    size = reactive({ifelse(V(g)$name %in% c(V(g)$name[ s()]), 3,2)})
    
  plot(g, layout = my.layout, edge.color = "lightgray", vertex.color = color(), vertex.size = size(),edge.arrow.size = eas, vertex.label=NA)
}
  
  if(input$graph == "school"){
    index = reactive({
      which(SmithDataSet$school_id == input$schools)})
    
    vertices = reactive({
      SmithDataSet$peopleID[index()]})
    
    color = reactive({
      ifelse(V(g)$name %in% vertices(), 'blue','lightgray')})
    
    size = reactive({
      ifelse(V(g)$name %in% vertices(), 3,2)})
    plot(g, layout = my.layout, edge.color = "lightgray", vertex.color = color(), vertex.size = size(),edge.arrow.size = eas, vertex.label=NA)
    
  }
  
  if(input$graph == "subject"){
    index = reactive({
      which(SmithDataSet$code == input$subj)})
    
    vertices = reactive({
      SmithDataSet$peopleID[index()]})

    color = reactive({
      ifelse(V(g)$name %in% vertices(), 'orange','lightgray')})
    
    size = reactive({
      ifelse(V(g)$name %in% vertices(), 3,2)}) 
    
    plot(g, layout = my.layout, edge.color = "lightgray", vertex.color = color(), vertex.size = size(),edge.arrow.size = eas, vertex.label=NA)
    
  }
  
  
  })

Joe Cheng

unread,
Dec 10, 2013, 2:43:54 PM12/10/13
to Loren Santana, shiny-...@googlegroups.com
Ack, no!! Any time you call reactive() or observe() from INSIDE a renderXXX() or observe(), it's almost guaranteed that you're doing something wrong (or at least that a simpler, less confusing alternative exists).

In the example below, all the reactives could be replaced with regular assignment. Like so:

shinyServer(function(input, output) {
# Panel 1 ---------------------------------------------------------------------------------------------------------
eas = .4;
 
# plot graph using color() and size ()
output$network = renderPlot({
  if(input$graph == "advisor"){
    s <- as.numeric(unlist(neighborhood(g,1,input$advisor)))
    color = ifelse(V(g)$name %in% c(V(g)$name[ s]), 'red','lightgray')
    size = ifelse(V(g)$name %in% c(V(g)$name[ s]), 3,2)
    
  plot(g, layout = my.layout, edge.color = "lightgray", vertex.color = color, vertex.size = size,edge.arrow.size = eas, vertex.label=NA)
}
  
  if(input$graph == "school"){
    index = which(SmithDataSet$school_id == input$schools)
    
    vertices = SmithDataSet$peopleID[index]
    
    color = ifelse(V(g)$name %in% vertices, 'blue','lightgray')
    
    size = ifelse(V(g)$name %in% vertices, 3,2)
    plot(g, layout = my.layout, edge.color = "lightgray", vertex.color = color, vertex.size = size,edge.arrow.size = eas, vertex.label=NA)
    
  }
  
  if(input$graph == "subject"){
    index = which(SmithDataSet$code == input$subj)
    
    vertices = SmithDataSet$peopleID[index]

    color = ifelse(V(g)$name %in% vertices, 'orange','lightgray')
    
    size = ifelse(V(g)$name %in% vertices, 3,2)
    
    plot(g, layout = my.layout, edge.color = "lightgray", vertex.color = color, vertex.size = size,edge.arrow.size = eas, vertex.label=NA)
    
  }
  
  
  })


--
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/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages