Shiny apply selected filter to ggplot, field colour

Hello everyone.

I have the following problem.
I want to do a simple plot, where x is Date, Y is some numeric and the lines should represent the values for given variables. So if I pick a gender variable, and it would return values for genders, or if I pick up cities it would return the values for cities.

I do believe that my problem is somewhere here :
"ggplot( aes(x = Date, y = avg_rating)) + geom_line(aes(colour = input$variable))"
Where the aes line does not recognize the input$variable. Kindly see the code bellow for more details
I tried to solve it with "aes_string" but it didn't work out.

Here is my code:

Dataset:

branch <- c("A","C","B")
city <- c("Yangon", "Naypyitaw","Yangon")
gender <- c("Male", "Female", "Male")
Date <- c("01/05/2019", "03/08/2019", "03/03/2019")
Rating <- c("9.1","9.6","9.7")

df <- data.frame(branch, city, gender, date, rating)

df1 <- df %>% mutate( Date = mdy(Date),
                      Rating = as.numeric(Rating),
               across(where(is.character),as.factor))%>% arrange(date)

Shiny Code:

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Shiny Scripts"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            selectInput(inputId = "variable",
                        label = "Type of Plot",
                        choices = df1 %>% select(where(is.factor)) %>% colnames() %>% as.factor,
                        selected = "City",
                        multiple = FALSE)
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("plot1")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    output$plot1 <- renderPlot({
         df1 %>% 
            group_by(Date, input$variable) %>%
            summarise( avg_rating = mean(Rating)) %>%
        ggplot( aes(x = Date, y = avg_rating)) + geom_line(aes(colour = input$variable))
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

Kindly let me know if you have any ideas.

library(shiny)
library(tidyverse)

df1 <- structure(list(branch = structure(1:3, .Label = c("A", "B", "C"
), class = "factor"), city = structure(c(2L, 2L, 1L), .Label = c("Naypyitaw", 
                                                                 "Yangon"), class = "factor"), gender = structure(c(2L, 2L, 1L
                                                                 ), .Label = c("Female", "Male"), class = "factor"), date = structure(c(17901, 
                                                                                                                                        17958, 17963), class = "Date"), rating = c(9.1, 9.7, 9.6)), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                                                        -3L))

# Define UI for application that draws a histogram
ui <- fluidPage(
    
    # Application title
    titlePanel("Shiny Scripts"),
    
    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            selectInput(inputId = "variable",
                        label = "Type of Plot",
                        choices = df1 %>% select(where(is.factor)) %>% colnames() %>% as.factor,
                        selected = "City",
                        multiple = FALSE)
        ),
        
        # Show a plot of the generated distribution
        mainPanel(
            plotOutput("plot1")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
    
    output$plot1 <- renderPlot({
        df1 %>% 
            group_by_("date", input$variable) %>%
            summarise(avg_rating = mean(rating)) %>%
            ggplot(aes(x = date, y = avg_rating)) +
            geom_line(aes_string(colour = input$variable))
    })
}

shinyApp(ui = ui, server = server)
1 Like

I mean, it is right. But it didn't work because I did not have "" on Date?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.