Search code examples
rshinyradar-chart

Filtering a radar chart by column names in Shiny


Thanks to the code below I can display the characteristics of 7 cars (A to G) on a radarchart on which I can choose the number of cars to be displayed by clicking on the checkboxes located on the left part of the chart :

enter image description here

   if (interactive()) {
  library(shiny)
  library(ECharts2Shiny)
  
  
  dat <- data.frame(Car.A = c(4300, 10000, 25000, 35000, 50000),
                    Car.B = c(5000, 14000, 28000, 31000, 42000),
                    Car.C = c(2400, 5000, 9020, 9200, 26000),
                    Car.D = c(8300, 11000, 15000,25000, 40000),
                    Car.E = c(23000, 14000, 28000, 31000, 42000),
                    Car.F = c(4000, 2000, 9000, 29000, 35000),
                    Car.G = c(4800, 1000, 5000, 21000, 15000))
  row.names(dat) <- c("Feture 1", "Feature 2", "Feature 3", "Feature 4", "Feature 5")
  
  
  # Server function -------------------------------------------
  server <- function(input, output) {
    renderRadarChart(div_id = "test",
                     data = dat)
  }
  
  # UI layout -------------------------------------------------




  ui <- 
    fluidRow(
      column(width = 3,
             
             box(width = NULL,  selectInput("variable", "choose any of the following", names(dat), multiple=TRUE)
             )),
  
  
  
  column(width = 5,
         box(width = NULL,
             loadEChartsLibrary(),
             
             tags$div(id="test", style="width:100%;height:400px;"),
             deliverChart(div_id = "test")
         ))
  
  )
   
  
  # Run the application --------------------------------------
  shinyApp(ui = ui, server = server)
}
# }

But this option doesn't suit me and I would rather use a select list input control instead:

 # Server function -------------------------------------------
  server <- function(input, output) {
    
    
    
    renderRadarChart(div_id = "test",
                     dat %>% dplyr::select(!!!input$variable)
    )
    
  }


 # UI layout -------------------------------------------------
    ui <- 
        fluidRow(
            column(width = 3,
                   
                   box(width = NULL,  selectInput("variable", "choose any of the following", names(dat), multiple=TRUE)
                   )),
            
            
            
            column(width = 5,
                   box(width = NULL,
                       loadEChartsLibrary(),
                       
                       tags$div(id="test", style="width:100%;height:400px;"),
                       deliverChart(div_id = "test")
                   ))
            
        )
    

enter image description here

But unfortunately this filter option doesn't work and I only manage to get this error message:

Error: only defined on a data frame with all numeric variables

What am I doing wrong ?


Solution

  • Minor adjustment to your server code should resolve the error. Try this

    # Server function -------------------------------------------
    server <- function(input, output) {
      observe({
        req(input$variable)
      
        data <- dat %>% dplyr::select(!!!input$variable)
        renderRadarChart(div_id = "test",data)
                       
      })
      
    }
    

    output