I'm trying to put together my first flexdashboard. (See here for an outline of the data I'm using)
I'd like the dashboard to be able to present either aggregate data for each facility, or break it to individual drugs.
The code I have so far is:
Inputs {.sidebar}
-----------------------------------------------------------------------
```{r}
selectInput("hosp",
label = h4("Select hospital:"),
choices = list("Hospital 1" = "hosp1",
"Hospital 2" = "hosp2"), selected = "hosp1")
sliderInput("dates",
label = h4("Select dates:"),
min = as.Date("2006-01-01"), max = as.Date("2015-12-01"),
value = c(as.Date("2006-01-01"), as.Date("2015-12-01")),
timeFormat = "%b-%Y")
````
Row
-----------------------------------------------------------------------
### Usage Graph
```{r}
renderPlot({
p <- ggplot(data = summarise(group_by(filter(usage,
hospital == input$hosp, date > as.Date(input$dates[1])
& date < as.Date(input$dates[2])), date),
total = round(sum(usage))), aes(x = date, y = total))
+ geom_line()
p
})
```
This works fine - I have a dropdown for the hospital and can select a date range using the slider.
What I'd like to do is add two different drop-downs; one to select the type of graph (total usage, individual drug, class of drugs)
How I've tried to do this is:
selectInput("gtype",
label = h4("Select graph type:"),
choices = list("Total Use" = "abxa",
"By Class" = "abxc",
"By Agent" = "abxd"), selected = "abxa")
conditionalPanel("input.gtype == 'abxd'",
selectInput("abagent",
label = h4("Select Agent:"),
choices = list("Amoxycillin" = "amoxycillin",
"Co-amoxyclav" = "amoxicillin-clavulanate",
"Cephazolin" = "cefazolin",
"Gentamicin" = "gentamicin"), selected = "cefazolin"
This works well in the sidebar - selecting "By Agent" pops up the next list box with the choice of drugs, and lets me pick one.
But how do change the output in the graph panel?
I need to change the ggplot call slightly for each use case; is there a way of rendering a different plot depending on the results of the dropdown?
I've tried using:
conditionalPanel("input.type" == "abxa",
renderPlot({ plot 1 call })
)
conditionalPanel("input.type" == "abxd",
renderPlot({ plot 2 call })
)
but this results in neither plot appearing regardless of the sidebar settings (I've confirmed that the two renderPlot calls work if you use them individually)
Thanks in advance.