Filter is not getting executed in r shiny

I have plotted in r shiny for multiple variables. I have also applied a filtering for that as per below code but I am getting an error. Once I click on Area and Street filter the ggplot should change accordingly.

library(shiny)
library(ggplot2)

ui <- fluidPage(
  tabsetPanel(tabPanel("Week",titlePanel(h4("Trend of Consumption",align="center")),
                       sidebarLayout(
                         sidebarPanel(
                           selectInput("trend",h6("trend"),choices = c("","Plot"),width = 120),br(),
                           selectInput("area",h6("Area"),choices = c("",levels(df$Area)),width = 
                                         150,multiple = TRUE),
                           selectInput("street",h6("Street"),choices = 
                                         c("",levels(df$Street)),width = 
                                         150,multiple = TRUE),width = 2,height = 1000),
                         mainPanel(plotOutput("graph"))
                       )
  ))
)

server <- function(input, output, session) {
  selectedData <- reactive({
    df[df %in% input$trend | df$Area %in% input$area | df$Street %in% input$street, ]
    
  })
  #graph <- eventReactive(
  #  input$trend,{
  graph <- reactive({
    ggplot(data = selectedData(), aes(Col, Wed, col = Area, shape = Street))+
      geom_point()
    # geom_line(aes(y=Mon, colour = "Mon"))+
    # geom_line(aes(y=Tue, colour = "Tue"))+
    # geom_line(aes(y=Wed, colour = "Wed"))+
    # ylab("hj")
  }
  )
  output$graph <- renderPlot(
    {
      graph()
    }
  )
}

shinyApp(ui, server)

Can we solve this problem?

andresrcs Could you please guide me in this.

Could you check on your reproducible example again? It is not filtering anything, because you haven't defined a UI element for input$day and you are not aplying any filtering for area and street, so I don't understand what part of your code is "not getting executed" as stated by you.

Also, please read our policy about @name mentioning

Apologies and I have edited my code. Please check. What I need is once you open the app, the plot should be empty and once you click plot trend button, the plot should get displayed. Then we need to play with the filters. For example if i filter on Area filter, the plot should get changes accordingly. In my code right now, plot button is not working since I have tagged it(#). Hope my point is clear

input$trend is a button, you can't filter based on it, if I remove df %in% input$trend from your code your app works, as long as both filters are populated, if this is not what you want, then check your filtering logic.

Hi andresrcs,

Thanks for your time, This is not what I wanted. I have edited my code for you. So right now what is happening is that once I click Plot trend button, the graph is getting displayed right? I am fine with that. But after this, when I filter values in Area and Street, the plot should get changed(but without pressing the button). Am I clear to you now?

Hi Hope my points are clear?

Does that work for you? I removed the button as now the plot just shows by default with all areas and streets selected. If you change the input in the dropdown lists the graph adjusts correspondingly. You could also make the Street input update depending on your Area input as for some areas (e.g. C) you don't have all streets.

df <- structure(list(Col = 1:11, Mon = c(47L, 110L, 31L, 72L, 129L, 
																				 41L, 85L, 123L, 14L, 152L, 118L), Tue = c(164L, 168L, 146L, 140L, 
																				 																					185L, 77L, 26L, 15L, 23L, 116L, 101L), Wed = c(163L, 5L, 109L, 
																				 																																												 170L, 37L, 96L, 41L, 188L, 163L, 82L, 5L), Area = structure(c(1L, 
																				 																																												 																															1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L), .Label = c("A", "B", 
																				 																																												 																																																									"C", "D"), class = "factor"), Street = structure(c(1L, 1L, 1L, 
																				 																																												 																																																																																		 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("Ste 1", "Ste 2"), class = 
																				 																																												 																																																																																	 	"factor")), class = "data.frame", row.names = c(NA, 
																				 																																												 																																																																																	 																									-11L))

library(shiny)
library(ggplot2)
library(dplyr)

ui <- fluidPage(
	tabsetPanel(tabPanel("Week",titlePanel(h4("Trend of 
                                            Consumption",align="center")),
											 sidebarLayout(
											 	sidebarPanel(#actionButton("trend", h6("Plot Trend"),width = 120),
											 							 #br(),
											 							 selectInput("area", h6("Area"),choices = c("All",levels(df$Area)),
											 							 						width = 150, multiple = TRUE, selected = "All"),
											 							 selectInput("street", h6("Street"),choices = 
											 							 							c("All",levels(df$Street)),width = 
											 							 							150,multiple = TRUE, selected = "All"),
											 							 							width = 2,height = 1000),
											 	mainPanel(plotOutput("graph"))
											 )
	)))

server <- function(input, output, session) {
	# graph <- eventReactive(
	# 	input$trend,{
	# 		ggplot(data=df,aes(Col))+geom_line(aes(y=Mon, colour = 
	# 																					 	"Mon"))+geom_line(aes(y=Tue, colour = "Tue"))+geom_line(aes(y=Wed, 
	# 																					 																															colour = "Wed"))+ylab("hj")
	# 	}
	# )
	output$graph <- renderPlot(
		{
		req(input$area, input$street)	
			if (input$area == "All") {
				area_filter <-  TRUE
			} else {
				area_filter <- df$Area %in% input$area
			}
			
			if (input$street == "All") {
				street_filter <-  TRUE
			} else {
				street_filter <- df$Street %in% input$street
			}
			
			df_to_plot <- df %>% 
				filter(area_filter, street_filter)
			
			
			
			ggplot(data=df_to_plot, aes(Col))+geom_line(aes(y=Mon, colour = 
																												"Mon"))+geom_line(aes(y=Tue, colour = "Tue"))+geom_line(aes(y=Wed, 
																																																										colour = "Wed"))+ylab("hj")
		}
	)
}

shinyApp(ui, server)

Thanks But filter is not working. If we select values under Area or street the graph is not getting changed

Make sure you remove "All" from the selection when you want to select specific areas and/or streets. And in fact since you allow multiple selections, the filters need to be modified a bit as follows:

			if ("All" %in% input$area) {
				area_filter <-  TRUE
			} else {
				area_filter <- df$Area %in% input$area
			}
			
			if ("All" %in% input$street) {
				street_filter <-  TRUE
			} else {
				street_filter <- df$Street %in% input$street
			}

OK go it. Is it not possible even I select some values in Area the plot should come (street remaining blank) . Also actually I wanted that plot button. Seeing this logic can we not make another filter(Select Input) with value "PLot" so I click on this the plot should be displayed and subsequently, we can play with the other filters? Can we do this?

I have edited my question for you. Now it will be easy for you get it

Were you able to get it?

Can you be please a bit more patient and wait for others to respond accordingly.
They might not sit in front of this thread 24/7.

1 Like

@vinayprakash808 you are being rude and demanding. If you want someone to do urgent work for you, you should hire a consultant.

Apologies both. I am new to this site and sure it wont be repeated

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