hafen / trelliscopejs

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

Embedding multiple trelliscopejs displays in one Rmarkdown file #58

Open lmbramer opened 6 years ago

lmbramer commented 6 years ago

Is there a way to embed multiple trelliscopejs displays in one Rmarkdown file? A reproducible example of two code chunks is below. The second display does not appear in the html file.

Here's one trelliscope display

library(trelliscopejs)
library(ggplot2)

data(iris)

qplot(Sepal.Length, Sepal.Width, data = iris) + facet_trelliscope(~Species, nrow = 1, ncol = 2, self_contained = T)

and another


qplot(Sepal.Length, Petal.Width, data = iris) + facet_trelliscope(~Species, nrow = 1, ncol = 2, self_contained = T)
beansrowning commented 6 years ago

I'm not sure of your use case, but this may not be the best approach.

Instead of markdown, you could simply have trelliscope write out several displays in the same directory and the resulting index page will give you an option to select which set of panels to display.

clarkjoe commented 6 years ago

@lmbramer This is a great issue. I am running into the same problem. My current work around is to independently generate many trelliscopes all into their own directories. Then, using iframes and manually adjusting the sizes, reference all the index.html files for each trelliscope within one Rmarkdown file.

This process works, but it is cumbersome.

balkon16 commented 6 years ago

I'm experiencing the same problem as @lmbramer. Here's my .rmd file:

---
title: "Testy trelliscopejs"
author: "balkon16"
date: "22 lipca 2018"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(message = FALSE, warning = FALSE) #suppress ggplot2 warnings
library(trelliscopejs)
library(ggplot2)
library(dplyr)
library(tidyr)
library(rbokeh)

d <- mpg %>%
  group_by(manufacturer, class) %>%
  nest() %>%
  mutate(panel = map_plot(data,
    ~ figure(xlab = "City mpg", ylab = "Highway mpg") %>%
        ly_points(cty, hwy, data = .x)))
d
d %>%
  trelliscope(name = "city_vs_highway_mpg", self_contained = TRUE)
mpg %>%
  group_by(manufacturer, class) %>%
  summarise(
    mean_city_mpg = cog(mean(cty), desc = "Mean city mpg"),
    mean_hwy_mpg = cog(mean(hwy), desc = "Mean highway mpg"),
    panel = panel(
      figure(xlab = "City mpg", ylab = "Highway mpg",
        xlim = c(7, 37), ylim = c(9, 47)) %>%
        ly_points(cty, hwy,
          hover = data_frame(model = paste(year, model),
            cty = cty, hwy = hwy)))) %>%
  trelliscope(name = "city_vs_highway_mpg", nrow = 1, ncol = 2,
              self_contained = TRUE)
qplot(x = 0, y = cty, data = mpg, geom = c("boxplot", "jitter")) +
  facet_trelliscope(~ class, ncol = 7, height = 800, width = 200,
    state = list(sort = list(sort_spec("cty_mean"))), self_contained = TRUE) +
  ylim(7, 37) + theme_bw()

and my session info:


R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=Polish_Poland.1250  LC_CTYPE=Polish_Poland.1250   
[3] LC_MONETARY=Polish_Poland.1250 LC_NUMERIC=C                  
[5] LC_TIME=Polish_Poland.1250    

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

other attached packages:
[1] Cairo_1.5-9          bindrcpp_0.2.2       dplyr_0.7.6          ggplot2_3.0.0.9000  
[5] trelliscopejs_0.1.13 rbokeh_0.5.0         tidyr_0.8.1         

loaded via a namespace (and not attached):
 [1] progress_1.2.0          gistr_0.4.2             tidyselect_0.2.4       
 [4] purrr_0.2.5             lattice_0.20-35         colorspace_1.3-2       
 [7] htmltools_0.3.6         yaml_2.1.19             base64enc_0.1-3        
[10] utf8_1.1.3              rlang_0.2.1             hexbin_1.27.2          
[13] pillar_1.2.2            glue_1.3.0              withr_2.1.2            
[16] pryr_0.1.4              bindr_0.1.1             plyr_1.8.4             
[19] stringr_1.3.1           munsell_0.4.3           gtable_0.2.0           
[22] devtools_1.13.5         htmlwidgets_1.2         memoise_1.1.0          
[25] codetools_0.2-15        evaluate_0.10.1         labeling_0.3           
[28] knitr_1.20              curl_3.2                Rcpp_0.12.17           
[31] scales_0.5.0            backports_1.1.2         checkmate_1.8.5        
[34] DistributionUtils_0.5-1 webshot_0.5.0           jsonlite_1.5           
[37] hms_0.4.2               digest_0.6.15           stringi_1.1.7          
[40] grid_3.5.1              rprojroot_1.3-2         cli_1.0.0              
[43] tools_3.5.1             magrittr_1.5            maps_3.3.0             
[46] lazyeval_0.2.1.9000     autocogs_0.0.1          tibble_1.4.2           
[49] crayon_1.3.4            pkgconfig_2.0.1         rsconnect_0.8.8        
[52] prettyunits_1.0.2       assertthat_0.2.0        rmarkdown_1.10         
[55] httr_1.3.1              rstudioapi_0.7          R6_2.2.2               
[58] mclust_5.4.1            git2r_0.21.0            compiler_3.5.1
jjfitz commented 4 years ago

@lmbramer One workaround to having both trelliscopes show up is to define a path in lib_dir in your yaml:

---
title: "Test Multiple Trelliscopes"
date: "11/21/2019"
output: 
  html_document:
    code_folding: hide
    lib_dir: trelli
---

and then defining subdirectory paths in facet_trelliscope in place of the self_contained argument. A full example is below:

---
title: "Test Multiple Trelliscopes"
date: "11/21/2019"
output: 
  html_document:
    code_folding: hide
    lib_dir: trelli
---
knitr::opts_chunk$set(echo = TRUE)
library(trelliscopejs)
library(ggplot2)

data(iris)

Trelliscope 1

qplot(Sepal.Length, Sepal.Width, data = iris) + facet_trelliscope(~Species, nrow = 1, ncol = 2, path = "trelli/trelli_one")

Trelliscope 2

qplot(Sepal.Length, Sepal.Width, data = iris) + facet_trelliscope(~Species, nrow = 1, ncol = 2, path = "trelli/trelli_two")