crosstalk icon indicating copy to clipboard operation
crosstalk copied to clipboard

SharedData objects do not "select" properly when data has one row

Open yogat3ch opened this issue 2 years ago • 2 comments

Hi @cpsievert & crosstalk devs, I've recently just realized that the selection is not updated properly when the data only has a single row.

A reprex is below:


# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(crosstalk)
library(shiny)


# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("Old Faithful Geyser Data"),
  
  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      actionButton(
        "send",
        label = "Send"
      )
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      DT::DTOutput("distPlot")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  isolate({
    s_data <- crosstalk::SharedData$new(data = mtcars |> head(1))
  })
  output$distPlot <- DT::renderDT({
    # generate bins based on input$bins from ui.R
    DT::datatable(s_data)
  }, server = FALSE)
  observeEvent(input$send,{
    o <- s_data$data(withSelection = TRUE)
    #selected_ should be TRUE
    if (isFALSE(o$selected_))
      browser()
    
  })
}

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

If one selects the row, presses the send button, and checks the o object, the selected_ column has FALSE for the only value even though the row is selected. Add two rows to the data and it works fine. (head(2))

sessionInfo ``` R version 4.1.3 (2022-03-10) Platform: aarch64-apple-darwin20 (64-bit) Running under: macOS Monterey 12.3.1

Matrix products: default LAPACK: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRlapack.dylib

locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages: [1] stats graphics grDevices datasets utils methods base

other attached packages: [1] crosstalk_1.2.0.9000 shiny_1.7.1

loaded via a namespace (and not attached): [1] fs_1.5.2 reactable_0.2.3.9000 usethis_2.1.5 lubridate_1.8.0 devtools_2.4.3
[6] httr_1.4.3 rprojroot_2.0.3 echarty_1.4.5 BOR_0.0.0.9000 tippy_1.0.0
[11] tools_4.1.3 bslib_0.3.1 utf8_1.2.2 R6_2.5.1 DT_0.22.1
[16] DBI_1.1.2 lazyeval_0.2.2 colorspace_2.0-3 withr_2.5.0 tidyselect_1.1.2
[21] prettyunits_1.1.1 shinyvalidate_0.1.2 processx_3.5.2 compiler_4.1.3 english_1.2-6
[26] cli_3.3.0 xml2_1.3.3 shinyjs_2.1.0 desc_1.4.1 plotly_4.10.0
[31] sass_0.4.0 scales_1.1.1 bs4Dash_2.0.3 callr_3.7.0 crssDB_0.0.0.9200
[36] stringr_1.4.0 digest_0.6.29 rmarkdown_2.13 pkgconfig_2.0.3 htmltools_0.5.2
[41] sessioninfo_1.2.2 attempt_0.3.1 learnr_0.10.1 fastmap_1.1.0 htmlwidgets_1.5.4
[46] rlang_1.0.2 rstudioapi_0.13 jquerylib_0.1.4 generics_0.1.2 jsonlite_1.8.0
[51] dplyr_1.0.9 config_0.3.1 magrittr_2.0.3 patchwork_1.1.1 Rcpp_1.0.8.3
[56] munsell_0.5.0 fansi_1.0.3 lifecycle_1.0.1 stringi_1.7.6 yaml_2.3.5
[61] snakecase_0.11.0 brio_1.1.3 pkgbuild_1.3.1 plyr_1.8.7 grid_4.1.3
[66] promises_1.2.0.1 crayon_1.5.1 knitr_1.37 ps_1.6.0 pillar_1.7.0
[71] markdown_1.1 pkgload_1.2.4 glue_1.6.2 evaluate_0.15 cicerone_1.0.4
[76] golem_0.3.1 data.table_1.14.2 remotes_2.4.2 renv_0.15.4 vctrs_0.4.1
[81] httpuv_1.6.5 testthat_3.1.2 virgaUtils_0.0.0.9001 gtable_0.3.0 purrr_0.3.4
[86] tidyr_1.2.0 assertthat_0.2.1 cachem_1.0.6 ggplot2_3.3.5 xfun_0.29
[91] mime_0.12 xtable_1.8-4 UU_0.0.0.9006 roxygen2_7.1.2 later_1.3.0
[96] viridisLite_0.4.0 dockerfiler_0.1.4 tibble_3.1.7 memoise_2.0.1 shinyWidgets_0.6.4
[101] sortable_0.4.5 ellipsis_0.3.2

</details>

yogat3ch avatar May 20 '22 15:05 yogat3ch

I've started exploring what the cause of this issue is since it doesn't look like crosstalk has any activity on it. I'm finding that the selection return here is only the first letter of the key value.

The get method is simple enough so it's probably not that and must be an issue in the javascript that returns the selected input.

It looks like the Javascript get method is also quite simple and doesn't appear to be the culprit.

I'm going to try to debug the set method

As well as this gnarly regex which looks suspect in the $escape method

yogat3ch avatar Aug 18 '22 14:08 yogat3ch

Nevermind, it looks like the debugger statements I add to the javascript are automatically removed. My guess is there's a nodejs linter that's removing them but I don't know enough about nodejs to figure out how to make it keep the debugger statements.

yogat3ch avatar Aug 18 '22 15:08 yogat3ch