refresh the plot after adjusting radio button and slider

ggplot2
shiny
rstudio
#1

Hello
For the shiny app I'm working on, i'm expecting the histogram will refresh after the user adjust the slider as well as the radio button.I tried many ways to update the plot but haven't been successful.
The plot do refresh while adjusting the slider when the radio button stays at
"ALL".But when the radio button changes to others, it appears ERROR:operations are possible only for numeric, logical or complex types.
Is there anyway to solve the problem?

Below is the server.r code I've used:

shinyServer<-function(input, output) {
  output$histogramplot<- renderPlot({
    filtered1<-reactive({
      dataset %>%
        filter(dataset$INVEST<=input$INVEST[2],
               dataset$INVEST>=input$INVEST[1],
               dataset$Age<=input$Age[2],
               dataset$Age>=input$Age[1],
               dataset$DayTrade == dataset$DayTrade)}
    )
    filtered2<-reactive({
      dataset %>%
        filter(dataset$INVEST<=input$INVEST[2],
               dataset$INVEST>=input$INVEST[1],
               dataset$Age<=input$Age[2],
               dataset$Age>=input$Age[1],
               dataset$DayTrade<-input$DayTrade)
    })
    theme_set(theme_classic())
    if (input$DayTrade == "All"){ 
     ggplot(filtered1(), aes(Age)) +
      geom_bar(aes(fill=SEX),bins = 25)}
    else{ 
      ggplot(filtered2(), aes(Age)) +
        geom_bar(aes(fill=SEX),bins=25)}
  })

Below is the ui.r code I've used:

    sidebarPanel(
      
      sliderInput("INVEST",min = 0,max = 5000,value = c(100,300),pre="$",width = "75%"),
      sliderInput("Age",min = 0,max = 100,value = c(20,30),width = "75%"),
      radioButtons("DayTrade", 
                   choices = c("Yes", "No","All"),
                   selected = "All",width = "75%")
    
    ),

Any help is appreciated

0 Likes

#2

This could be the reason, I think this should be <= instead of <-

0 Likes

#3

Thanks for finding out the mistake.After changing it the plot still doesn't appear when the radio button adjust to "yes" and "no" though.

0 Likes

#4

I didn't noticed that input$DayTrade comes from a radiobutton control, you can't compare text strings this way dataset$DayTrade <= "No", it's hard to help you witout a REPRoducible EXample (reprex), could you make one for your issue?
Here is a very useful guide about how to make a reprex for a shiny app

0 Likes