Dynamic filters and reactive plot in Shiny

Pryore

Issues between inputs and plot output

Hi,

I'm testing out a basic ShinyApp where I can generate a plot of commercial services broken down by geography and service type.

The idea is I want the user to use three drop-down menu inputs, each dependent upon the previous selection, to subset the data, which then gets output in a ggplot.

However, I'm having issues connecting the inputs to the plot output (see below). The inputs are working fine and reactive when selected, but I can't work out how to link that to the plot, I get the feeling I'm not using the right data source (but have no idea how to ensure it is). Furthermore, I'm not familiar with how I would go about adding a third filter (for "service") seeing as I don't know how to link my data source in the first place.

Sorry this is probably simple, but some help would be really appreciated.

UI

 #Data
 Test <- dataframe(
           Geography1 = c("Region","Local Authority","County"...),
           Geography2 = c("North West","Aldershot","Cheshire"...),
           Service = c("Shop","Cafe","Library"...),
           Overall_rating = c("Awesome","Good","Fantatstic"...),
           Locations = c(4000, 1300, 1700...)
  )

 #SHINY APP
 ui <- fluidPage(
  titlePanel("Tool"),
   sidebarLayout(
    sidebarPanel(
     uiOutput("geography1"),
     uiOutput("geography2"),
     uiOutput("service")),

  mainPanel(
     plotOutput("plot", height = "400px"))
     )
  )

Server

   server <- function(input, output) {

 output$geography1 = renderUI({
    selectInput(inputId = "geog1",
              label = "Geography 1:", 
              choices = as.character(unique(Test$Geography1)),
              selected = "Region")
  })

 output$geography2 = renderUI({

   datasub <- Test[Test$Geography1 == input$geog1, "Name"]

     selectInput(inputId = "geog2", 
                 label = "Geography2:", 
                 choices = unique(datasub),
                 selected = unique(datasub)[1])
  })

  output$service = renderUI({

     datasub2 <- unique(datasub)

     selectInput(inputId = "service",
                 label = "Service type:",
                 choices = unique(...),
                 selected = unique(...)[1])
   })

  output$plot = renderPlot({

    ggplot(datasub2(),aes(x = Overall_rating, y = Locations, fill= Overall_rating))+
     geom_bar(stat = "identity")

   })
 }

  shinyApp(ui, server)

enter image description here

Juergen

It's hard to tell how the provided data is supposed to be filtered in the app but this code will at least run and be interactive. Hopefully from there you can figure out how to adjust the dataset.

As BigDataScientist said one fault is that you're not using a reactive dataset.

#Data
Test <- data.frame(
  Geography1 = c("Region","Local Authority","County"),
  Geography2 = c("North West","Aldershot","Cheshire"),
  Service = c("Shop","Cafe","Library"),
  Overall_rating = c("Awesome","Good","Fantatstic"),
  Locations = c(4000, 1300, 1700)
)

#SHINY APP
ui <- fluidPage(
  titlePanel("Tool"),
  sidebarLayout(
    sidebarPanel(
      uiOutput("geography1"),
      uiOutput("geography2"),
      uiOutput("service")),

    mainPanel(
      plotOutput("plot", height = "400px"))
  )
)

server <- function(input, output) {

  output$geography1 = renderUI({
    selectInput(inputId = "geog1",
                label = "Geography 1:", 
                choices = as.character(unique(Test$Geography1)),
                selected = "Region")
  })

  datasub <- reactive({
    Test[Test$Geography1 == input$geog1,]
  })

  output$geography2 = renderUI({
    selectInput(inputId = "geog2", 
                label = "Geography2:", 
                choices = unique(datasub()[,"Geography2"]),
                selected = unique(datasub()[,"Geography2"])[1])
  })

  datasub2 <- reactive({
    datasub()[Test$Geography2 == input$geog2, ]
  })

  output$service = renderUI({
    selectInput(inputId = "service",
                label = "Service type:",
                choices = unique(datasub2()[,"Service"]),
                selected = unique(datasub2()[,"Service"])[1])
  })

  datasub3 <- reactive({
    datasub()[Test$Service == input$service, ]
  })

  output$plot = renderPlot({
    ggplot(datasub3(),aes(x = Overall_rating, y = Locations, fill= Overall_rating))+
      geom_bar(stat = "identity")

  })
}


shinyApp(ui, server)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

dynamic UI that allows filters to be updated and plot made in shiny R

R Shiny dplyr reactive filters

Shiny ggvis reactive plot

questions in shiny with selectinput and the reactive plot

Code for a reactive table to plot in Shiny

Dynamic plot height in Shiny

R shiny editable table with reactive filters - update filters with table edits

R shiny error when using reactive to plot

Reactive bar plot with different datasets in Shiny

Dynamic filters shiny app with equal/common levels

Using dynamic filters to aggregate data in R Shiny

Dynamic height of shiny ggplotly plot

Shiny: Dynamic height adjustment of plot

Reactive Shiny Plot doesn't display any plot.

Shiny dashboard - reactive bar plot with values from checkbox

Preventing Shiny selectInput from evaluating prematurely when updating a reactive plot

Render a plot generated from a fitted model in reactive for shiny

How is data passed from reactive Shiny expression to ggvis plot?

Shiny: Reactive slider produces error in plot: "invalid 'xlim' value"

Shiny reactive XY Plot: object of type 'closure' is not subsettable

In R shiny, how do you render a plot for a reactive object?

Return input as list in reactive shiny - dynamic UI in insert UI

R Shiny - How to update a dependent reactive selectInput before updating dependent reactive plot

How to pass the Drop down value as an input to a function to generate dataset within Shiny Dashboard? The dataset uses reactive filters

R [Shiny]: How to make reactive shiny apps which display dynamic systems models?

How do I control bar colors in plot_ly with reactive data in shiny

How do I make sure that a shiny reactive plot only changes once all other reactives finish changing?

Plot different reactive graph using different dataset and columns based on reacitve inputs in shiny

Shiny Interdependent Filters values

TOP Ranking

HotTag

Archive