Hi,
The tidyverse evaluation is tricky to understand and I'm still new to it myself, but I found a way to fix it by using the parse_quo() from the 'rlang' package.
library("shiny")
library("ggplot2")
library("rlang")
ui <- fluidPage(
selectInput("data_input", "data", choices = 1:4),
selectInput("X_ax", "X_ax", choices = "mpg"),
selectInput("Y_ax", "y_ax", choices = "mpg"),
plotOutput("myPlot")
)
server <- function(input, output, session) {
rf <- reactive({
# req(input$data_input)
if(input$data_input ==1) {daten <- mtcars}
else if (input$data_input ==2) {daten <- iris}
else if (input$data_input ==3) {daten <- ToothGrowth}
else if (input$data_input ==4) {daten <- USArrests}
updateSelectInput(session, "X_ax", choices = colnames(daten))
updateSelectInput(session, "Y_ax", choices = colnames(daten))
daten
})
output$myPlot = renderPlot({
x = parse_quo(input$X_ax, env = caller_env())
y = parse_quo(input$Y_ax, env = caller_env())
ggplot(data = rf(), aes(x = !!x, y = !!y)) + geom_point()
})
}
shinyApp(ui, server)
Once implemented, the code ran, but there were some issues with updating the plots given the inputs for the axes depend on the dataset chosen. If you change the dataset, it would trigger the plot refresh, but then the axes selection hadn't updated yet and you'd get a brief error message before the axes refreshed based on the new dataset. I found a work-around, but I'm still not very happy with is as it looks a bit weird in the code. I had to isolate the dataset in ggplot to prevent triggering update before the axes were set. Unfortunately, this was the only place in the code where the dataset was called, and if isolated, Shiny will ignore the reactive function because there's no code to use it, even if the triggers in the function change. I solved this my adding an empty observeEvent using the reactive function, and now all works well:
library("shiny")
library("ggplot2")
library("rlang")
ui <- fluidPage(
selectInput("data_input", "data", choices = 1:4),
selectInput("X_ax", "X_ax", choices = "mpg"),
selectInput("Y_ax", "y_ax", choices = "mpg"),
plotOutput("myPlot")
)
server <- function(input, output, session) {
rf <- reactive({
# req(input$data_input)
if(input$data_input ==1) {daten <- mtcars}
else if (input$data_input ==2) {daten <- iris}
else if (input$data_input ==3) {daten <- ToothGrowth}
else if (input$data_input ==4) {daten <- USArrests}
updateSelectInput(session, "X_ax", choices = colnames(daten))
updateSelectInput(session, "Y_ax", choices = colnames(daten))
daten
})
observeEvent(rf(),{
})
output$myPlot = renderPlot({
x = parse_quo(input$X_ax, env = caller_env())
y = parse_quo(input$Y_ax, env = caller_env())
ggplot(data = isolate(rf()), aes(x = !!x, y = !!y)) + geom_point()
})
}
shinyApp(ui, server)
Let me know if you know a nice way to prevent that refresh issue, because I've been tinkering with it for 2 hours and although it works, I think it's not the right way of coding it. At least I was able to solve your original issue
Grtz,
PJ