Nevedha Ayyanar
Nevedha Ayyanar

Reputation: 865

Filtering the data using pickerInput() and plotting based on the filtered data in R

I am currently developing shiny application and I need to filter the data based on the user selection. The input control used is pickerInput(). I tried the following code but it doesn't work.

library("shiny")
library("dplyr")
library("shinyWidgets")

mychoices <- c("A", "B", "C", "D", "E","F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
               "U", "V", "W", "X", "Y", "Z")
df <- data.frame("ID" = mychoices, "invoice" = runif(26, 100, 200))

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      uiOutput("id1")
      )
    ),
    mainPanel(
      plotOutput("test")        
    )
  )
)



server <- function(input, output) {

  output$id1 <- renderUI({
    pickerInput(
      inputId = "id", label = "Choices :",
      choices = mychoices,
      options = list('actions-box' = TRUE),
      multiple = TRUE
    )

  })

  filter1 <-  reactive({

    df %>% filter(ID %in% input$id)
})


  output$test <- renderPlot({

    ggplot(data = filter1, aes(invoice)) + geom_histogram(binwidth=30, alpha=0.6)
  })
}

shinyApp(ui = ui, server = server)

Can anyone help me with this issue?

Upvotes: 1

Views: 1395

Answers (2)

ismirsehregal
ismirsehregal

Reputation: 33417

When calling your reactive filter1 you'll need brackets (). Please see the following:

library("shiny")
library("dplyr")
library("shinyWidgets")

mychoices <- c("A", "B", "C", "D", "E","F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
               "U", "V", "W", "X", "Y", "Z")
df <- data.frame("ID" = mychoices, "invoice" = runif(26, 100, 200))

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      uiOutput("id1")
    )
    ,
    mainPanel(
      plotOutput("test")        
    ))
)

server <- function(input, output) {

  output$id1 <- renderUI({
    pickerInput(
      inputId = "id", label = "Choices :",
      choices = mychoices,
      options = list('actions-box' = TRUE),
      multiple = TRUE
    )
  })

  filter1 <-  reactive({
    df %>% filter(ID %in% input$id)
  })

  output$test <- renderPlot({
    myFilteredDf <- filter1()
    ggplot(data = myFilteredDf, aes(invoice)) + geom_histogram(binwidth=30, alpha=0.6)
  })
}

shinyApp(ui = ui, server = server)

Upvotes: 1

Ouss Gh
Ouss Gh

Reputation: 53

This is a classical error that we encounter when starting with R Shiny. As said above, you'll need to add parentheses whenever you call your reactive data object filter1. this code works for me :

library("shiny")
library("dplyr")
library("shinyWidgets")
library("ggplot2")

mychoices <- c("A", "B", "C", "D", "E","F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
               "U", "V", "W", "X", "Y", "Z")
df <- data.frame("ID" = mychoices, "invoice" = runif(26, 100, 200))

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      uiOutput("id1")
    )
  ,
  mainPanel(
    plotOutput("test")        
  )
)
)




server <- function(input, output) {

  output$id1 <- renderUI({
    pickerInput(
      inputId = "id", label = "Choices :",
      choices = mychoices,
      options = list('actions-box' = TRUE),
      multiple = TRUE
    )

  })

  filter1 <-  reactive({

    df %>% filter(ID %in% input$id)
  })


  output$test <- renderPlot({
    filter1 = filter1()
    ggplot(data = filter1, aes(invoice)) + geom_histogram(binwidth=30, alpha=0.6)
  })
}

shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions