'Combining shiny with facet_grid
I'm working on a school project where I have a few dyplr queries where I need to make them interactive using the Shiny library. Now I have a query in dyplr where i can see the Revenue for every country from a Indian takeaway restaurant. I have the following query:
```
df %>%
group_by(Origin, Jaar = year(Order.Date), Maand = month(Order.Date, label = TRUE), Kwartaal = quarter(Order.Date)) %>%
summarize(omzet = sum(regelomzet)) %>%
ggplot(aes(reorder(Origin, omzet), omzet)) +
facet_grid(~Jaar) +
geom_col() +
ggtitle("Omzet per land van herkomst") +
coord_flip() +
theme(axis.text.x = element_text(angle = 90))
```
This gives me the following output:
Now if I change the facet_grid to for example kwartaal (quarters in a year) I get the following output:
Now I would like to apply this plot in a interactive Shiny app. i use the following code for this:
```
```{r}
library(shiny)
ui <- fluidPage(
titlePanel("Indian takeaway"),
navlistPanel("Kies een plot",
tabPanel("Omzet per land van herkomst",
selectInput(inputId = "Select_unit",
label = "Selecteer op basis van wat je de grafiek wilt zien",
choices = c("Jaar", "Kwartaal", "Maand"),
selected = "Jaar"
),
plotOutput(outputId = "plot3")
)
)
)
server <- function(input, output, session) {
output$plot3 <- renderPlot({
df %>%
group_by(Origin, Jaar = year(Order.Date), Maand = month(Order.Date, label = TRUE), Kwartaal = quarter(Order.Date)) %>%
summarize(omzet = sum(regelomzet)) %>%
ggplot(aes(reorder(Origin, omzet), omzet)) +
facet_grid(~input$Select_unit) +
geom_col() +
ggtitle("Omzet per land van herkomst") +
coord_flip() +
theme(axis.text.x = element_text(angle = 90))
})
}
shinyApp(ui, server)
```
Here I make a slicer and show the plot on the Shiny app. To change the facet_wrap I use the input function that is linked to the input box now if I change the variable in the input box I would expect it to show the graph like in the above two pictures.
Now if I start the shiny app you can see that there isn't any facet_grid in the first place not even on the variable that is standard. My question is why does it do this because I did include it in my query. Because I have no clue I don't know how I could fix this and already did google this but couldn't find anything helpfull

Solution 1:[1]
I found the following information on another stack overflow post: R Shiny: Issue with facet_wrap in ggplot
I use facet_grid(~input$Select_unit), using the following line of code the problem will be sovled
facet_grid(~get(input$Select_unit))
Solution 2:[2]
With the introduction of the .data pronoun, the best way to turn a string into a variable for ggplot would be with .data[[variable]]. Also we can use the non-formula syntax for facet_grid which allows specifying rows= and cols= as vars() lists. For example
facet_grid(cols=vars(.data[[input$Select_unit]]))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jdiehl |
| Solution 2 |
