Home > front end >  Filter by distinct column values from selectInput
Filter by distinct column values from selectInput

Time:09-30

Trying to get a value box output where it is only summing a column (Sold) based on the selections I would make to 2 other columns. My idea was to filter the data down based on my selection, then summarize the remaining rows by the Sold column. Code here:

library(tidyverse)
library(shiny)
library(shinydashboard)

df <- 
  data.frame(Car_Col = c("Red","Blue","Red","Black","White","Black","Black","White","Red","Blue"),
             Car_Type = c("E","G","D","G","G","D","G","D","E","G"),
             Sold = (c(1,0,1,0,0,0,1,1,0,0)))

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      selectInput(
      inputId = "x",
      label = "Select variable",
      choices = c(unique(df$Car_Col)),
    )),
    selectInput(
      inputId = "x2",
      label = "Select variable",
      choices = c(unique(df$Car_Type))
    ),
    valueBoxOutput("box1"),
    
  )
)

server <- function(input, output) {
  
    output$box1 <- renderValueBox({
    df %>% 
        filter(Car_Col == .data[[input$x]]) %>% 
        filter(Car_Type == .data[[input$x2]]) %>% 
        summarise(Number_Sold = sum(Sold)) %>% 
        pull(Number_Sold) %>% 
        valueBox(subtitle = "Number of sales")
        })
}

shinyApp(ui, server)

This spits back the error;

Warning: Error in filter: Problem while
computing `..1 = Car_Col ==
.data[["Red"]]`.
Caused by error in `.data[["Red"]]`:
! Column `Red` not
  found in `.data`.

Now I believe that the way I've done it above is essentially telling the filter to look for a column called whatever I've selected as an input, which are the distinct values so column name is coming up empty. I understand why I'm wrong but I'm not sure how to do it correctly.

End goal would be, for instance from the code above, to be able to select say

Car_Col = Red
Car_Type = E

valueBoxOutput would then give 1

CodePudding user response:

I did very little to your code, but this for me works, is this what you were looking for? One, I removed the c() from the choices = c(unique(...)) since for me at least, it was coercing them into numbers instead of characters. And two, in the filter I removed the .data[[]]. Hopefully this helps!

library(tidyverse)
library(shiny)
library(shinydashboard)

df <- 
  data.frame(Car_Col = c("Red","Blue","Red","Black","White","Black","Black","White","Red","Blue"),
             Car_Type = c("E","G","D","G","G","D","G","D","E","G"),
             Sold = (c(1,0,1,0,0,0,1,1,0,0)))

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      selectInput(
        inputId = "x",
        label = "Select variable",
        choices = unique(df$Car_Col),
      )),
    selectInput(
      inputId = "x2",
      label = "Select variable",
      choices = unique(df$Car_Type)
    ),
    valueBoxOutput("box1"),
    
  )
)

server <- function(input, output) {
  
  output$box1 <- renderValueBox({
    df %>% 
      filter(Car_Col == input$x) %>% 
      filter(Car_Type == input$x2) %>% 
      summarise(Number_Sold = sum(Sold)) %>% 
      pull(Number_Sold) %>% 
      valueBox(subtitle = "Number of sales")
  })
}

shinyApp(ui, server)
  • Related