Home > OS >  How to filter a column in shiny whose name is an output from a slider
How to filter a column in shiny whose name is an output from a slider

Time:12-03

I have a dashboard where slider is getting updated based on a dropdown widget. My issue is that dropdown selects the name of the column, and slider filters the selected column. The issue is when i create reactive filtered dataset: specifically this line: filter(input$selectx > input$my_slider[1]. i understand that it does not work cause the input$selectx is a character name of the column (eg "mean_radius", and I need a name without quotations (eg mean_radius). I tried quote(), {{}} and other functions but could not sort it out

#loading packages 
library(shiny) 
library(tidyverse) 
library(datateachr) #cancer_sample dataset was used from this data package
library(rstatix) 
library(shinythemes)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Cancer", titleWidth = 300),
  dashboardSidebar(
    width = 300,
    selectInput("selectx", label = h3("Select X Variable"), 
                choices = list("radius_mean", "texture_mean", "perimeter_mean", "area_mean"), 
                selected = "area_mean"),
    tags$br(),
    
    sliderInput("my_slider",
                label = h3("Range of X Variable"), 
                min = min(cancer_sample$area_mean, na.rm = TRUE),
                max = max(cancer_sample$area_mean, na.rm = TRUE),
                value = c(143.5,2501))
  ),
  dashboardBody(
    
    #makes the place holder for the plot
    box(title = "Scatter Plot", solidHeader = TRUE, collapsible = TRUE, width = 12, plotOutput("my_plot", click = "plot_click")),
    box(title = "Data Table", solidHeader = TRUE, collapsible = TRUE, width = 12, tableOutput("my_data"))
  )
  
  
)

server <- function(input, output, session) {
  
  #makes a reactive function to minimize repeated code
  filtered <- reactive({
    #the dataset that is being used
    cancer_sample %>%
      #filters the data set based on the area mean range from the slider, and the check boxes that are selected
      filter(input$selectx > input$my_slider[1],
             input$selectx < input$my_slider[2])
  })
  
  observe({
    col <- cancer_sample %>% select(input$selectx)
    
    #makes a slider that you can manipulate to show only data points that has an area mean that falls in the certain range
    updateSliderInput(session, "my_slider",
                      value = col,
                      min = min(col, na.rm = TRUE),
                      max = max(col, na.rm = TRUE))
  })
  
  output$my_plot <- renderPlot({
    filtered() %>%
      #produces a graph with area_mean on the x-axis and perimeter_mean on the y-axis.        
      ggplot(aes_string(x = input$selectx, y = perimeter_mean))  
      geom_point(aes(colour = diagnosis)) 
  })
  

  output$my_data <- renderTable(
    filtered() %>%
      select(ID:area_mean)
    
  )
}

# Run the application 
shinyApp(ui = ui, server = server)

CodePudding user response:

Your problem is not shiny connected, so the question could be easily simplified.

Unfortunately you do not provide the dataset here. So I could not provide a working example.

quote will always return what is inside quote(input$selectx) -> input$selectx so this for sure not a solution.

Please use the e.g. .data solution here.

airquality %>% filter(.data[[input$selectx]] > input$my_slider[1],
.data[[input$selectx]] < input$my_slider[2])
  • Related