Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify_tree() (or map_vec()?) chokes on S3 inside shiny::reactive() #1155

Open
maxheld83 opened this issue Nov 14, 2024 · 0 comments
Open

modify_tree() (or map_vec()?) chokes on S3 inside shiny::reactive() #1155

maxheld83 opened this issue Nov 14, 2024 · 0 comments

Comments

@maxheld83
Copy link

I ran into a very weird and subtle behavior involving modify_tree() on a list which includes

  1. shiny::reactive()s
  2. around some S3 class

take any of them away (unclass() the S3 class) or don't use a reactive, and the problem goes away.

Here's the reprex:

shiny::testServer(
  app = function(input, output, session) {
    # fails with this object
    obj <- reactive(htmltools::HTML("foo"))
    # works without a reactive
    # obj <- htmltools::HTML("foo")
    # works with class removed
    # obj <- reactive(unclass(htmltools::HTML("foo")))
    purrr::modify_tree(
      x = obj,
      is_node = function(x) is.reactive(x) || is.list(x),
      pre = function(x) {
        if (is.reactive(x)) {
          x <- rlang::exec(x)
        }
        x
      }
    )
  },
  # no actual tests here
  expr = stopifnot(TRUE)
)

As is, this fails with:

Error in `map_vec()`:
! Can't convert `<list>[[1]]` <character> to <html>.

This makes sense to me: you can't convert a list to html.
(html is the S3 class assigned to returns by htmltools::HTML()).

What I don't understand is:

  • why modify_tree() (via map_vec()) would try to do this in the first place
  • why this wouldn't also affect the other examples

I'm not sure this is a bug, but it sure struck me as unexpected behavior.
(Or maybe I'm just being boneheaded and missing something).

session_info():

─ Session info ────────────────────────────
 setting  value
 version  R version 4.4.2 (2024-10-31)
 os       macOS Sequoia 15.1
 system   aarch64, darwin20
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       Europe/Berlin
 date     2024-11-14
 pandoc   3.5 @ /opt/homebrew/bin/pandoc

─ Packages ────────────────────────────────
 package     * version date (UTC) lib source
 backports     1.5.0   2024-05-23 [1] CRAN (R 4.4.0)
 brio          1.1.5   2024-04-24 [1] RSPM
 bslib         0.8.0   2024-07-29 [1] RSPM
 cachem        1.1.0   2024-05-16 [1] RSPM
 checkmate     2.3.2   2024-07-29 [1] CRAN (R 4.4.0)
 cli           3.6.3   2024-06-21 [1] CRAN (R 4.4.0)
 desc          1.4.3   2023-12-10 [1] RSPM
 devtools    * 2.4.5   2022-10-11 [1] RSPM
 digest        0.6.37  2024-08-19 [1] CRAN (R 4.4.1)
 ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.4.0)
 fansi         1.0.6   2023-12-08 [1] CRAN (R 4.4.0)
 fastmap       1.2.0   2024-05-15 [1] CRAN (R 4.4.0)
 fs            1.6.5   2024-10-30 [1] CRAN (R 4.4.1)
 glue          1.8.0   2024-09-30 [1] CRAN (R 4.4.1)
 htmltools     0.5.8.1 2024-04-04 [1] CRAN (R 4.4.0)
 htmlwidgets   1.6.4   2023-12-06 [1] CRAN (R 4.4.0)
 httpuv        1.6.15  2024-03-26 [1] RSPM
 jquerylib     0.1.4   2021-04-26 [1] CRAN (R 4.4.0)
 jsonlite      1.8.9   2024-09-20 [1] CRAN (R 4.4.1)
 later         1.3.2   2023-12-06 [1] RSPM
 lifecycle     1.0.4   2023-11-07 [1] RSPM
 magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.4.0)
 memoise       2.0.1   2021-11-26 [1] RSPM
 mime          0.12    2021-09-28 [1] CRAN (R 4.4.0)
 miniUI        0.1.1.1 2018-05-18 [1] RSPM
 pak         * 0.8.0   2024-08-26 [1] CRAN (R 4.4.1)
 pillar        1.9.0   2023-03-22 [1] CRAN (R 4.4.0)
 pkgbuild      1.4.5   2024-10-28 [1] CRAN (R 4.4.1)
 pkgload       1.4.0   2024-06-28 [1] CRAN (R 4.4.0)
 profvis       0.4.0   2024-09-20 [1] CRAN (R 4.4.1)
 promises      1.3.0   2024-04-05 [1] CRAN (R 4.4.0)
 purrr         1.0.2   2023-08-10 [1] CRAN (R 4.4.0)
 R6            2.5.1   2021-08-19 [1] CRAN (R 4.4.0)
 Rcpp          1.0.13  2024-07-17 [1] RSPM
 remotes       2.5.0   2024-03-17 [1] CRAN (R 4.4.0)
 reprex      * 2.1.1   2024-07-06 [1] RSPM
 rlang         1.1.4   2024-06-04 [1] CRAN (R 4.4.0)
 rprojroot   * 2.0.4   2023-11-05 [1] CRAN (R 4.4.0)
 sass          0.4.9   2024-03-15 [1] CRAN (R 4.4.0)
 sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.4.0)
 shiny       * 1.9.1   2024-08-01 [1] RSPM
 testthat      3.2.1.1 2024-04-14 [1] RSPM
 urlchecker    1.0.1   2021-11-30 [1] RSPM
 usethis     * 3.0.0   2024-07-29 [1] CRAN (R 4.4.0)
 utf8          1.2.4   2023-10-22 [1] RSPM
 vctrs         0.6.5   2023-12-01 [1] CRAN (R 4.4.0)
 withr         3.0.2   2024-10-28 [1] CRAN (R 4.4.1)
 xtable        1.8-4   2019-04-21 [1] RSPM

 [1] /Users/max/Library/R/arm64/4.4/library
 [2] /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library

───────────────────────────────────────────

Why though?

Fair question.

My shiny module deals with inputs which can be a varying, nested list of reactives and non-reactives.
To deal with this list (show them) I first need to make all these leafs the same (i.e. rlang::exec() them and some other stuff).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant