hafen / trelliscopejs

TrelliscopeJS R Package
https://hafen.github.io/trelliscopejs
Other
262 stars 36 forks source link

Creating panels with various widgets, sometimes not all of them render #131

Open struckma opened 5 months ago

struckma commented 5 months ago

Dear Ryan, dear Barret, dear developers of trelliscopejs,

great package, your trelliscopejs, thank you for the great idea. I've played around with it a bit, and I found

https://github.com/hafen/trelliscopejs/blob/e0805c98db6b9af913e49fa264b4ebb943e430c0/R/trelliscope.R#L198

where you exemplarily choose the first panel to define the html dependencies for a trelliscope page. However, if I use other widgets on the other panels, they won't show up, because they are missing their dependencies.

As a work-around, I could add a first dummy panel with all possible widgets on it, then everything works. I would try to fix the problem soundly by including all dependencies instead of only those for the first panel's widget, if you are interested -- I have already some prototyping work-around, but I would prefer to provide a sound fix.

Thank you,

Stephan

Code to reproduce using (trelliscopejs 0.2.6 with R version 4.3.2 (2023-10-31)):

library(tibble)
library(plotly)
library(dygraphs)
library(trelliscopejs)
library(htmltools)

tib <- tibble(.rows = 3)
tib <- tibble::add_column(tib, group = as.character(1:3))
tib <- tibble::add_column(tib,
                          result =
                            map_plot(1:3, function(i) {
                                if (i %in% c(1, 3)) {
                                  dyRangeSelector(
                                    dygraph(nhtemp, main = "New Haven Temperatures"),
                                    dateWindow = c("1920-01-01", "1960-01-01"))
                                } else {
                                  plotly::plot_ly()
                                }
                            })
)

trelliscope(tib, name = "wont render the plotly")

tib <- tibble(.rows = 3)
tib <- tibble::add_column(tib, group = as.character(1:3))
tib <- tibble::add_column(tib,
                          result =
                            map_plot(1:3, function(i) {
                              if (i %in% c(2)) {
                                dyRangeSelector(
                                  dygraph(nhtemp, main = "New Haven Temperatures"),
                                  dateWindow = c("1920-01-01", "1960-01-01"))
                              } else {
                                plotly::plot_ly()
                              }
                            })
)

trelliscope(tib, name = "wont render the dygraph")

tib <- tibble(.rows = 3)
tib <- tibble::add_column(tib, group = as.character(1:3))
tib <- tibble::add_column(tib,
                          result =
                            map_plot(1:3, function(i) {
                              dygraph(nhtemp, main = "New Haven Temperatures")
                            })
)

trelliscope(tib, name = "renders as expected")

tib <- tibble(.rows = 3)
tib <- tibble::add_column(tib, group = as.character(1:3))
tib <- tibble::add_column(tib,
                          result =
                            map_plot(1:3, function(i) {
                              plotly::plot_ly()
                            })
)

trelliscope(tib, name = "renders as expected")

tib <- tibble(.rows = 3)
tib <- tibble::add_column(tib, group = as.character(1:3))
tib <- tibble::add_column(tib,
                          result =
                            map_plot(1:3, function(i) {
                              if (i %in% c(1, 3)) {
                                dygraph(nhtemp, main = "New Haven Temperatures")
                              } else {
                                plotly::plot_ly()
                              }
                            })
)
hafen commented 5 months ago

@struckma thanks for this report. There is a new rewrite of Trelliscope available here which is where I am focusing my efforts. You can also visit trelliscope.org.

Things are a little different on the R side but I think for the best. I just added the capability you have requested into this package. It now detects the type of each panel and writes widget dependencies for any that have not been written yet. You can find a reproducible example below. You will need to install the two packages commented out.

# remotes::install_github("trelliscope/trelliscope")
library(trelliscope)
library(dplyr)
# remotes::install_github("hafen/mustachewidget")
library(mustachewidget)
library(plotly)

plot_fn <- function(country) {
  if (country == "New Zealand") {
    mustache("<div style='width: 100%; height: 100%; border: 4px solid red; text-align: center; box-sizing: border-box;'>{{country}}</div>", list(country = country))
  } else {
    data <- filter(gap, country == {{ country }})
    plot_ly(data) %>%
      add_trace(x = ~ year, y = ~ life_exp, type = "scatter", mode = "lines+markers")
  }
}

d <- gap |>
  filter(continent == "Oceania") |>
  summarise(mean_life_exp = mean(life_exp), .by = "country") |>
  mutate(
    test_panel = panel_lazy(plot_fn)
  ) |>
  as_trelliscope_df(
    name = "gapminder_multiwidget",
    path = "_public/gapminder_multiwidget"
  )

view_trelliscope(d)
struckma commented 5 months ago

@hafen, thank you for your quick reply, that solves my issue, thanks. Can you tell, if and when you roughly plan a CRAN release of the new trelliscope package? Also, I'm interested in your mustachewidget, since I had a similar idea implemented but far less mature than yours... are you planning to publish that on CRAN too?

I could also help with the CRAN maintenance.

hafen commented 5 months ago

@struckma good question. Since I greatly dislike maintaining packages on CRAN (ever-changing persnickety rules that can result in entire days of lost productivity trying to keep them happy - I may take you up on your offer!), I have been putting it off. My plan has been to get some more community feedback before starting to get locked in on CRAN. The package has been developed with a lot of input from a few people but it's great to get a feel for issues a broader user base might encounter. With that said, I do think the package is very mature and stable at this point and I don't foresee any major changes, so I should probably consider doing that soon.

Regarding mustachewidget, I have been using that in a few internal Trelliscope projects so I hadn't really considered publishing it, but I think it would be a great thing to do if it would be useful for others. If you are interested in joining forces with what you have been working on, let me know. Or if you have something you'd like to publish on CRAN, that is not a problem. In that case I would just continue using this for these projects.

struckma commented 5 months ago

@hafen great, then I'll talk back with my team and come back to you in about two weeks (winter vacation time starts today in German).

struckma commented 4 months ago

@hafen I'm back from vacation and we have discussed about some joined effort, here. Most importantly, the head of our group supports the idea, so I can offer to start some kind of collaboration on maintaining the two packages for CRAN. Maybe, we should prepare a short online meeting for that. I'ld like to use email for further communication, since it is not really about my issue any more: stephan.struckmann@uni-greifswald.de Maybe, you can answer there, if you like.