This is my very first attempt at using Shiny, and I'm quite new to all of this, so I imagine my problem is quite straightforward to someone with more experience.
I have data associated with a shp file that I am trying to map. I want the user to be able to select which data (which disease) to display on the map. There will be a number of other parameters that the user will be able to select (age group affected, per capita statistics, etc), so I am trying to put together a means of narrowing down which data to actually plot. There will be over 500 combinations, so assigning one by one would be tedious. So far I have the following:
(Preceding the code, there's lots of data cleaning and getting things into the right format. Don't think any of that is relevant, but I can include if necessary)
ui <- fluidPage(
titlePanel("Health in Tanzania"),
DiseaseList <- c(NameDiagList[5:60],"All Disease"),
sidebarLayout(
sidebarPanel(
#Select disease
selectInput("DiseaseSelect",
label = "Disease",
choices = DiseaseList,
selected = "All Diseases"),
),
mainPanel(
leafletOutput("TZHealthMap")
)
)
server <- function(input, output) {
AllNames <- colnames(Map@data) #make a list of all the column names where health data is stored
DiseaseChoice <- subset(AllNames, grepl(input$DiseaseSelect, AllNames)) #select only the columns pertaining to the disease selected by user input
SelectedData <- Map@data[[DiseaseChoice[1]]] #take the first of the columns selected by user input
#this will eventually not just be the first item in DiseaseChoice, based on more selection
LeafletPalette <- colorNumeric(palette = rev(brewer.pal(5, "RdYlGn")), domain = SelectedData)
output$TZHealthMap <- renderLeaflet({
leaflet(Map) %>%
addProviderTiles("OpenStreetMap.Mapnik") %>%
addPolygons(color = ~LeafletPalette(SelectedData))
})
}
shinyApp(ui = ui, server = server)
When I execute this code, I get the following error message: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
If I replace the line DiseaseChoice <- subset(AllNames, grepl(input$DiseaseSelect, AllNames))
with DiseaseChoice <- subset(AllNames, grepl(DiseaseList[1], AllNames))
, I get a couple of warnings, but the map is generated, so I suspect there's something wonky with input$DiseaseSelect
or using it in the grepl()
call or something.
Input appreciated!!