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

JQuery error when column name contains brackets in select_group #88

Closed marcin-karlinski closed 9 months ago

marcin-karlinski commented 10 months ago

If a column in dataset contains brackets in its name, console returns syntax error containing column name when using select_group_ui() and select_group_server(). This happens even if the column is not used in filtering.

The error:

shinyapp.ts:442  Error: Syntax error, unrecognized expression: #my-filters-container-Weight (pounds)
    at se.error (jquery.js:1681:8)
    at se.tokenize (jquery.js:2381:11)
    at se.select (jquery.js:2842:20)
    at Function.se [as find] (jquery.js:898:9)
    at S.fn.init.find (jquery.js:3099:11)
    at new S.fn.init (jquery.js:3209:32)
    at S (jquery.js:161:10)
    at e.<anonymous> (datamods.js:38:5)
    at e.<anonymous> (shinyapp.ts:866:40)
    at m (shinyapp.ts:5:1357)

This may not be a critical issue, (especially that it's probably not a good practice to use brackets in column names) but think it could potentially reveal columns which may not be intended to see by the user?

Slightly modified example from documentation (datamods v. 1.4.2):

# Default -----------------------------------------------------------------

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

cars <- MASS::Cars93 %>% 
  mutate(`Weight (pounds)` = Weight)

ui <- fluidPage(
  # theme = bslib::bs_theme(version = 5L),
  fluidRow(
    column(
      width = 10, offset = 1,
      tags$h3("Filter data with select group module"),
      shinyWidgets::panel(
        select_group_ui(
          id = "my-filters",
          params = list(
            list(inputId = "Manufacturer", label = "Manufacturer:"),
            list(inputId = "Type", label = "Type:"),
            list(inputId = "AirBags", label = "AirBags:"),
            list(inputId = "DriveTrain", label = "DriveTrain:")
          )
        ),
        status = "primary"
      ),
      reactable::reactableOutput(outputId = "table"),
      tags$b("Inputs values:"),
      verbatimTextOutput("inputs")
    )
  )
)

server <- function(input, output, session) {
  res_mod <- select_group_server(
    id = "my-filters",
    data = reactive(cars),
    vars = reactive(c("Manufacturer", "Type", "AirBags", "DriveTrain"))
  )

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

  output$inputs <- renderPrint({
    attr(res_mod(), "inputs")
  })
}

if (interactive())
  shinyApp(ui, server)

ps. thanks for a great package!

edit: Of course I meant normal brackets, not square brackets. Sorry for that.

pvictor commented 9 months ago

Thanks for reporting this, it's fixed in GitHub version.

Victor