1

I am trying to make a Shiny app which uses this function (made in R markdown):

ls_vs <- function(variable) {
dataset %>%
  filter({{variable}} != 254.948530) %>% 
  filter({{variable}} != 121.738080) %>%
  ggplot(mapping = aes(y = life_satisfaction, x = {{variable}})) +
  geom_point(aes(color = region, shape = freedom_status), size = 2) +
  geom_smooth(color = "cyan") +
  labs(y = "Life Satisfaction", shape = "Freedom Status", color = "Continent")
}

ls_vs(economic_freedom)

I am getting the desired results with this function:

Desired Plot

Now Here's my attempt of integrating that into a shiny app:

The UI section:

tabPanel("Factor Comparision", 
               
               sidebarPanel(
                 varSelectInput(inputId = "variable", label = "Select Factor to Compare", data = dataset),
                 
                mainPanel(                                   
                  h1("Test"),           
                  plotOutput("ls_vs"),
                 )   
               )),
      

And here is the server section:

#Factor Comparisons
    output$ls_vs <- renderPlot({
      dataset%>%
        filter({{input$variable}} != 254.948530) %>% 
        filter({{input$variable}} != 121.738080) %>%
        ggplot(mapping = aes(y = life_satisfaction, x = {{input$variable}})) +
        geom_point(aes(color = region, shape = freedom_status), size = 2) +
        geom_smooth(color = "cyan") +
        labs(y = "Life Satisfaction", shape = "Freedom Status", color = "Continent")
    })

Upon trying to run the app, I'm getting the error:

error

Error: arg must a symbol

Have I done something incorrectly in the UI area or in the sever area? How does one correctly use varSelectInput in Shiny to make an interactive ggplot where one can change the variable to change the plot?

Thank you very much!

2 Answers 2

1

The main difference between the code in markdown vs that in Shiny is Shiny passes string value of the variable ("economic_freedom") whereas the function in markdown has been written to work with bare column names (economic_freedom).

Change the function to work with strings that can be done using .data instead of {{}}.

library(dplyr)
library(ggplot2)

output$ls_vs <- renderPlot({
  dataset%>%
    filter(.data[[input$variable]] != 254.948530) %>% 
    filter(.data[[input$variable]] != 121.738080) %>%
    ggplot(mapping = aes(y = life_satisfaction, x = .data[[input$variable]])) +
    geom_point(aes(color = region, shape = freedom_status), size = 2) +
    geom_smooth(color = "cyan") +
    labs(y = "Life Satisfaction", shape = "Freedom Status", color = "Continent")
})
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much, it worked well. I have a quick follow up question, would you be able to offer some guidance on removing certain options from varSelectInput. My app shows the drop down menu with ALL variables, but I want to select only a specific set of variables to be shown in the drop down menu and hopefully rename them so they don't show up with underscores if possible. Thank you again.
You can pass subset of dataframe in varSelectInput i.e instead of passing data only pass dataset[1:5] if you want to include first 5 columns. You may also use selectInput instead of varSelectInput. In selectInput you can pass the values manually like c('A', 'B', 'C')` or subset of column names like names(dataset)[1:5].
0

Using "bang bang" operator with dplyr::sym() should also work.

#Factor Comparisons
output$ls_vs <- renderPlot({
  dataset%>%
    filter(!!sym(input$variable) != 254.948530) %>% 
    filter(!!sym(input$variable) != 121.738080) %>%
    ggplot(mapping = aes(y = life_satisfaction, x = !!sym(input$variable))) +
    geom_point(aes(color = region, shape = freedom_status), size = 2) +
    geom_smooth(color = "cyan") +
    labs(y = "Life Satisfaction", shape = "Freedom Status", color = "Continent")
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.