0
votes

Hello everybody,

I need your help please : I have a dataframe named data_test, you can reproduce it with these lines :

 PC<-c('001','002','003','004','005','006','007','008','009','010')
 A<-c('2','2','1','1','2','0','2','0','1','0')
 B<-c('0','2','0','0','1','0','0','0','1','0')
 C<-c('1','0','0','0','2','2','0','0','0','1')
 NB_CARS<-c('1','3','1','1','0','2','1','3','2','2')
 data_test <- data.frame(PC, A, B, C, NB_CARS)

And I have three linked chekboxGroupInputs named "variable1", "variable2" and "variable3". When I check some of these chekboxGroupInputs, the number of matching rows is updated. But I would like to insert a reactive SelectInput to show the resulting number of cars based on the "NB_CARS" variable, and I don't manage to do that. Could you please help me ? Thanks much.

This is my code :

 ui <- fluidPage(
   fluidRow(
     column(3,
            checkboxGroupInput("variable1", "Occurences of column A :",
                               c("Yes (1)" = "1",
                                 "No (2)" = "2",
                                 "Perhaps (0)" = "0")),
            checkboxGroupInput("variable2", "Occurences of column B :",
                               c("Yes (1)" = "1",
                                 "No (2)" = "2",
                                 "Perhaps (0)" = "0")),
            checkboxGroupInput("variable3", "Occurences of column C :",
                               c("Yes (1)" = "1",
                                 "No (2)" = "2",
                                 "Perhaps (0)" = "0"))),

     column(3,
            tableOutput("data")),

     column(3,
            textOutput("result"))
   )
 )

 server <- function(input, output, session) {

   result <- 0

   df1 <- reactive({
     filter(data_test,
            (is.null(input$variable1) | A %in% input$variable1),
            (is.null(input$variable2) | B %in% input$variable2),
            (is.null(input$variable3) | C %in% input$variable3))
   })

   output$result <- renderText(paste("Number of occurences = ", nrow(df1())))


   output$data <- renderTable({df1()})

 }

 shinyApp(ui, server)
1

1 Answers

0
votes

I'm not sure this is what you have in mind, but maybe this will be helpful (if not, let me know).

You can add a selectInput in your ui that will be updated by updateSelectInput in your server.

Edit 1: The output$result is modified to show the number of rows that match input$numcars. In other words, if you select from the new input$numcars selectInput, you will see the number of times NB_CARS matches the selection made.

Edit 2: If you wish to add All as a choice, be aware that NB_CARS is a factor. So you were getting levels of the factor with unexpected results (such as a 4). You can use as.character before combining with All to get desired result.

Also, when you render the text, just check if the input is All - if it is, show number of rows. If not, show NB_CARS matches. See edited server.

ui <- fluidPage(
  fluidRow(
    column(3,
           checkboxGroupInput("variable1", "Occurences of column A :",
                              c("Yes (1)" = "1",
                                "No (2)" = "2",
                                "Perhaps (0)" = "0")),
           checkboxGroupInput("variable2", "Occurences of column B :",
                              c("Yes (1)" = "1",
                                "No (2)" = "2",
                                "Perhaps (0)" = "0")),
           checkboxGroupInput("variable3", "Occurences of column C :",
                              c("Yes (1)" = "1",
                                "No (2)" = "2",
                                "Perhaps (0)" = "0")),
           selectInput("numcars", "Number of Cars", choices = NULL)),
    column(3,
           tableOutput("data")),

    column(3,
           textOutput("result"))
  )
)

server <- function(input, output, session) {

  result <- 0

  df1 <- reactive({
    filter(data_test,
           (is.null(input$variable1) | A %in% input$variable1),
           (is.null(input$variable2) | B %in% input$variable2),
           (is.null(input$variable3) | C %in% input$variable3))
  })

  observe({
    updateSelectInput(session, "numcars", choices = c("All", unique(as.character(df1()[["NB_CARS"]]))))
  })

  output$result <- renderText({
    if (input$numcars == "All") {
      num_text <- nrow(df1())
    } else {
      num_text <- sum(df1()[["NB_CARS"]] == input$numcars)
    }
    paste("Number of occurences = ", num_text)
  })

  output$data <- renderTable({df1()})

}