dreamRs / datamods

Shiny modules to import and manipulate data into an application or addin
https://dreamrs.github.io/datamods/
GNU General Public License v3.0
138 stars 35 forks source link

Default selection for factors is always all choices #76

Closed PhilipVonschallen closed 11 months ago

PhilipVonschallen commented 1 year ago

Hello. I was wondering if in the UI there is a possibility to have no choices selected per default for factors when I start the application. I'm aware that there is the possibility to deselect all choices with the X on the right side, but it would be nice to be able to select that in the code. As far as I know the application always selects all choices for factors.

I have used the example under the "filter-data " section from the CRAN documentation (https://cran.r-roject.org/web/packages/datamods/datamods.pdf).

I have attached here the script I was using: datamods_filter-data.txt

Thanks :)

pvictor commented 12 months ago

Hello,

Indeed the default behavior is to select all choices for factors (same for characters btw). It's surely possible to have no choice selected by default, do you think it'll be better default for the module in general or you want the ability to choose on or the other ?

Victor

PhilipVonschallen commented 12 months ago

Hi Victor, Thanks for the reply! Well ideally it would be nice to choose either none, all, or a specific selection of choices. If that is too tedious to implement I would prefer to have none as the default (looks a bit cleaner when you have a lot of choices), but this is just my preference. Best, Philip

pvictor commented 12 months ago

Actually you can pre-select choices using defaults argument, the problem is that if you don't select any choice for a character/factor variable you will be filtering out all rows. See this example :

library(shiny)
library(shinyWidgets)
library(datamods)

ui <- fluidPage(
  tags$h2("Filter data.frame"),
  fluidRow(
    column(
      width = 3,
      filter_data_ui("filtering", max_height = "500px")
    ),
    column(
      width = 9,
      reactable::reactableOutput(outputId = "table"),
      tags$b("Code dplyr:"),
      verbatimTextOutput(outputId = "code_dplyr"),
      tags$b("Expression:"),
      verbatimTextOutput(outputId = "code"),
      tags$b("Filtered data:"),
      verbatimTextOutput(outputId = "res_str")
    )
  )
)

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

  res_filter <- filter_data_server(
    id = "filtering",
    data = reactive(iris), 
    defaults = reactive(
      list(Species = character(0))
    )
  )

  output$table <- reactable::renderReactable({
    reactable::reactable(res_filter$filtered())
  })

  output$code_dplyr <- renderPrint({
    res_filter$code()
  })
  output$code <- renderPrint({
    res_filter$expr()
  })

  output$res_str <- renderPrint({
    str(res_filter$filtered())
  })

}

shinyApp(ui, server)
PhilipVonschallen commented 12 months ago

Cool that works, thanks a lot! I was wondering if there is a similar functionality for the select_group_ui and select_group_server functions? Meaning, that when I select choices from one group I could select all remaining available choices from other groups. So for example in this script datamods_select-group.txt I would select for manufacturer "Audi" and then I would be able to choose all Types (here would be "Compact" and "Midsize") with one click.

PhilipVonschallen commented 11 months ago

Ok I found it myself, it was the disableSelectAll = TRUE part in select_group_ui that I changed to FALSE.