hafen / trelliscopejs

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

Saving highcharter objects to a path #101

Open abresler opened 3 years ago

abresler commented 3 years ago

Ryan,

Appears like there are issue creating exportable trelliscopes with highcharter objects.

It works perfectly fine when you aren't exporting the trelliscope.

Here is a quick repro example:

library(trelliscopejs)
library(highcharter)
library(tidyverse)

tbl_mpg <- 
  mpg %>%
  nest(data = !one_of(c("manufacturer", "class"))) %>%
  mutate(cogs = map_cog(
    data,
    ~ tibble(
      mean_city_mpg = cog(mean(.$cty), desc = "Mean city mpg"),
      mean_hwy_mpg = cog(mean(.$hwy), desc = "Mean highway mpg"),
      most_common_drv = cog(tail(names(table(
        .$drv
      )), 1), desc = "Most common drive type")
    )
  ))

df <- tbl_mpg %>%
  mutate(panel = map_plot(data, function(x) {

    hchart(x, type = "scatter", hcaes(x = "cty", y = "hwy", group = "drv"))
  }))

viz_no_path <- df %>%
  trelliscope(
    name = "city_vs_highway_mpg",
    nrow = 1,
    ncol = 2,
    jsonp = T
  )

## Works but can't export
viz_no_path

viz_path_contained <- df %>%
  trelliscope(
    name = "city_vs_highway_mpg",
    nrow = 1,
    ncol = 2,
    path = "Desktop/test/hchart/contained",
    jsonp = T,
    self_contained = T
  )

## Opens but doesn't work
viz_path_contained 

viz_path_not_contained <- df %>%
  trelliscope(
    name = "city_vs_highway_mpg",
    nrow = 1,
    ncol = 2,
    path = "Desktop/test/hchart/uncontained",
    self_contained = F
  )

### Doesn't work
viz_path_not_contained
abresler commented 3 years ago

Been trying to trouble shoot this a bit more.

It looks like even without explicitly setting the path when you search for it in the tmp folders and navigate to the index.html file the results are the same as the raw export mechanism.

However when you use use the path set to the index.html and launch the browseURL it works perfectly by setting up a localhost {same way it works without defining the path in the function where it the output renders but not in an exportable format.

library(trelliscopejs)
library(highcharter)
library(tidyverse)

tbl_mpg <-
  mpg %>%
  nest(data = !one_of(c("manufacturer", "class"))) %>%
  mutate(cogs = map_cog(
    data,
    ~ tibble(
      mean_city_mpg = cog(mean(.$cty), desc = "Mean city mpg"),
      mean_hwy_mpg = cog(mean(.$hwy), desc = "Mean highway mpg"),
      most_common_drv = cog(tail(names(table(
        .$drv
      )), 1), desc = "Most common drive type")
    )
  ))

df <- tbl_mpg %>%
  mutate(panel = map_plot(data, function(x) {

    hchart(x, type = "scatter", hcaes(x = "cty", y = "hwy", group = "drv"))
  }))

viz_no_path <- df %>%
  trelliscope(
    name = "city_vs_highway_mpg",
    nrow = 1,
    ncol = 2,
    jsonp = T
  )

viz_no_path  ## works in viewer

temp_path <- attr(viz_no_path, "trelliscope_pars")$www_dir

browseURL(temp_path) ## opens static trelliscope index.html file and doesn't work
file.path(path$www_dir, "index.html") %>% browseURL() ## does work!

Any idea if there is a way to export the localhost rendered version that does work?

Screen Shot 2020-12-21 at 9 52 22 AM Screen Shot 2020-12-21 at 9 52 13 AM