I'm trying to produce an interactive plot, where the x-axis depends on input. Like in this minimal example, R just gives me one boxplot (x-axis labelled: as.factor(cyl)
) instead of three (for cyl == 4,6,8).
How can I include (render/paste) the input variable directly in the arguments of aes so that I get a dynamic axis?
Minimal example: (Rmd Flexdashboard)
---
title: ""
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
Column {.sidebar}
-----------------------------------------------------------------------
### Input
```{r }
radioButtons("xaxis", "x-axis:",
c("Cly" = "cyl",
"VS" = "vs"))
```
Column
-----------------------------------------------------------------------
```{r}
mtcars_r <- reactive({mtcars})
library(ggplot2)
renderPlot({
ggplot(mtcars_r(), aes_string(x = paste("'as.factor(", input$xaxis, ")'", sep = ""), y = 'wt')) + geom_boxplot()
})
```
mtcars_r
object you created is not necessary here, a reactive object is useful when more than one output depends on a transformation and you do not want to do that transformation more than once. In your case, inside therective({})
call you did nothing with the mtcars data frame, when I was expecting you to do something using the reactive input. Take a look over this post to see what I mean. - Johan Rosa