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.