rstudio / sortable

R htmlwidget for Sortable.js
https://rstudio.github.io/sortable/
Other
130 stars 30 forks source link

silent str_squish in add_rank_list? #62

Closed shannonpileggi closed 3 years ago

shannonpileggi commented 4 years ago

Thanks for this amazing widget! This works perfectly for my ap.

I did run into an issue that took me a while to debug. It appears that there is some str_squish type behavior happening behind the scenes that removes extra white spaces. Did I miss this in the documentation? Is there an argument to turn this off or on? If so, apologies for not seeing it! An example is below. If this isn't yet documented, it would be helpful to make this more obvious to the user.

Thanks again!

library(shiny)
library(sortable)

my_list <- c("   A   A   A   ",
             "  B. B. B.  ",
             " C.  C.  C. ")

shinyApp(
  ui = fluidPage(fluidRow(
    column(
      6,
      sortable::bucket_list(
        header = NULL,
        group_name = "bucket_list_group",
        sortable::add_rank_list(
          text = "Group 1",
          labels = my_list,
          input_id = "rank_list_1"
        ),
        sortable::add_rank_list(
          text = "Group 2",
          labels = NULL,
          input_id = "rank_list_2"
        )
      )
    ),
    column(
      6,
      h2("Original text"),
      verbatimTextOutput("original"),
      br(),
      h2("Group 1"),
      verbatimTextOutput("results_1"),
      br(),
      h2("Group 2"),

      verbatimTextOutput("results_2")
    )
  )),
  server = function(input, output) {
    output$original <-  renderPrint({
      my_list
    })

    output$results_1 <-  renderPrint({
      input$rank_list_1
    })

    output$results_2 <-  renderPrint({
      input$rank_list_2
    })

  }
)
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
Shiny applications not supported in static R Markdown documents

Created on 2020-08-21 by the reprex package (v0.3.0)

andrie commented 4 years ago

This is interesting. Thanks for reporting.

It's not immediately apparent to me where or why this conversion happens, but I think it's the interaction between R, JavaScript and HTML. It may even be a pure function of HTML.

But here is a workaround for you:

Like this:

my_list <-
  c(
    "   A   A   A   ",
    "  B. B. B.  ",
    " C.  C.  C. "
  ) %>%
  purrr::map_chr(~gsub(" ", "&nbsp;", .)) %>%
  purrr::map(HTML)
shannonpileggi commented 4 years ago

Thanks for taking a look and thanks for the workaround! I actually didn't need the white spaces for my purposes, I just wasn't aware that I had them. I fixed my issue by modifying the input:

my_list <- stringr::str_squish(c(" A A A ", " B. B. B. ", " C. C. C. "))