In R-Shiny. Trying to break up a really long reactive function (thousands of lines!). Hypothetically, is it possible to nest conditional reactive functions, something similar to:
STATE_filter <- reactive({
if(input$selectcounty ends with "-AL") {
run AL_filter()
}
else if (input$selectstate ends with "-AR"){
run AR_filter()
}
else {
return("ERROR")
}
})
EDIT
Non-hypothetically, I'm trying to create a nested reactive filtering function based on user select inputs of U.S. counties. Upon their selection of county, a circlepackeR graph should pop up in a modal dialog box. This is the data I am using:
dput(head(demographics))
structure(list(NAME = c("Autauga-AL", "Baldwin-AL", "Barbour-AL",
"Bibb-AL", "Blount-AL", "Bullock-AL"), STATE_NAME = c("AL", "AL",
"AL", "AL", "AL", "AL"), gender = structure(c(2L, 2L, 2L, 2L,
2L, 2L), .Label = c("female", "male"), class = "factor"), hispanic = structure(c(2L,
2L, 2L, 2L, 2L, 2L), .Label = c("hispanic", "nonhispanic"), class = "factor"),
race = structure(c(6L, 6L, 6L, 6L, 6L, 6L), .Label = c("asian",
"black", "islander", "native", "two or more", "white"), class = "factor"),
makeup = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("in combination",
"one race", "two or more"), class = "factor"), r_count = c(456L,
1741L, 114L, 96L, 320L, 44L), pathString = c("world/male/nonhispanic/white/one race",
"world/male/nonhispanic/white/one race", "world/male/nonhispanic/white/one race",
"world/male/nonhispanic/white/one race", "world/male/nonhispanic/white/one race",
"world/male/nonhispanic/white/one race")), row.names = c(NA,
6L), class = "data.frame")
Here's an example of the reactive function I'm using below. It's a small subset of 10,000 + lines, and I want to "nest" it by splitting the lines by state (AL for Alabama, AR for Arkansas) first so it's a cleaner piece of code.
demographics_filter <- reactive({
if(input$selectcounty == "Autauga-AL") {
race_autauga <- subset.data.frame(demographics, NAME=="Autauga-AL")
nodes_autauga <- as.Node(race_autauga)
}
else if(input$selectcounty== "Baldwin-AL") {
race_baldwinAL <-subset.data.frame(demographics, NAME=="Baldwin-AL")
nodes_baldwinAL<- as.Node(race_baldwinAL)
}
else if(input$selectcounty== "Ashley-AR") {
race_AshleyAR <-subset.data.frame(race, NAME=="Ashley-AR")
nodes_AshleyAR<- as.Node(race_AshleyAR)
}
else {
return("ERROR!")
}
})
And finally, here's the graph in my server that's utilizing this function:
output$circle_graph_of_demographics <- renderCirclepackeR({
circlepackeR(demographics_filter(), size = "r_count"
})