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

Embed LDAvis output to Rmarkdown #90

Open
him4u324 opened this issue Jul 19, 2018 · 8 comments
Open

Embed LDAvis output to Rmarkdown #90

him4u324 opened this issue Jul 19, 2018 · 8 comments

Comments

@him4u324
Copy link

I am trying to embed LDAvis output to Rmarkdown and share the html output just like any other rmarkdown output with intact interactivity.

I have tried 2 approaches in rmarkdown

  1. This does not produce anything in html output
        ```{r}
        data2 <- with(TwentyNewsgroups, createJSON(phi, theta, doc.length, vocab, 
        term.frequency))
        serVis(with(data2, createJSON(phi, theta, doc.length, vocab, 
        term.frequency,R = 25)))
        ```
  1. with this, LDA interactive output appeared in Rstudio Viewer but does not show anything in browser
        ```{r}
        data2 <- with(TwentyNewsgroups, createJSON(phi, theta, doc.length,
        vocab, term.frequency))
        serVis(with(data2, createJSON(phi, theta, doc.length, vocab, 
        term.frequency, R = 25)), out.dir = "a")
        ```
        <iframe width="1200" height="1500" src="a/index.html" frameborder="0"></iframe>

I have tried open.browser = TRUE as well as FALSE but it didn't solve the problem.

Note: Shiny is not an option for me as I want to share the interactivity as an attachment.

I have also logged this issue on stackoverflow but no help so far.
https://stackoverflow.com/questions/51285861/embed-ldavis-output-to-rmarkdown

@ZHuang4270
Copy link

Did you check the Rmd file referenced in this thread?

#28

@him4u324
Copy link
Author

Hi ZHuang, Thanks for your response. I checked the thread but still I am not able to figure out how to embed it on RMD without Shiny.

@o0oBluePhoenixo0o
Copy link

o0oBluePhoenixo0o commented Dec 11, 2018

Put these line before your <iframe></iframe>

Those files you can find in the LDAVis folder (out.dir)

<link rel="stylesheet" type="text/css" href="lda.css">
<script src="d3.v3.js"></script>
<script src="ldavis.js"></script>

@him4u324
Copy link
Author

him4u324 commented Jul 26, 2019

Hello Trung Nguyen, I tried your suggestion but still no avail.

library(lda)
library(LDAvis)
 # data2 <- with(TwentyNewsgroups, createJSON(phi, theta, doc.length,
 #                                           vocab, term.frequency))
serVis(with(TwentyNewsgroups, createJSON(phi, theta, doc.length, vocab, 
                              term.frequency, R = 25)), out.dir = "a")
<link rel="stylesheet" type="text/css" href="a/lda.css">
<script src="a/d3.v3.js"></script>
<script src="a/ldavis.js"></script>

<iframe width="1200" height="1500" src="a/index.html" frameborder="0"></iframe>

@BlakeList
Copy link

@dselivanov has a great way of embedding them similar to what is described above.

See the code in his text2vec topic modelling tutorial here.

The graph can be saved then embedded after the code chunk using

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>LDAvis</title>
<script src="topic_modeling_files/ldavis/d3.v3.js"></script>
<script src="topic_modeling_files/ldavis/ldavis.js"></script>
<link rel="stylesheet" type="text/css" href="topic_modeling_files/ldavis/lda.css">
</head>
<body>
<div id = "lda"></div>
<script>
var vis = new LDAvis("#lda", "topic_modeling_files/ldavis/lda.json");
</script>
</body>

@valentinitnelav
Copy link

I am curious if a solution was found as none of the proposed ones above worked in my case. As @him4u324 initially posted, I get the interactive visualization in the Rstudio Viewer, but still not in the html document.

@BlakeList
Copy link

You should be able to embed the dynamic graph using the html code above. Do you have a reproducible case?

@valentinitnelav
Copy link

valentinitnelav commented Jun 30, 2020

Here is a Rmd simplified example and 2 imgur links showing what I see in the Rstudio Viewer and what in the browser:

      ---
      output: html_document
      ---

      ```{r global-options, include=FALSE}
      knitr::opts_chunk$set(echo=FALSE, warning=FALSE, message=FALSE, cache=TRUE)
      ```

      ```{r load-packages}
      library(stm)
      library(LDAvis)
      ```

      ```{r}
      mod <- stm(documents = poliblog5k.docs,
                 vocab= poliblog5k.voc,
                 K = 10,
                 prevalence = ~ rating,
                 max.em.its = 2, # set very low to gain speed for this example
                 data = poliblog5k.meta) 

      stm::toLDAvis(mod = mod, docs = poliblog5k.docs, out.dir = "ldavis")
      ```

      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>LDAvis</title>
        <script src="ldavis/d3.v3.js"></script>
        <script src="ldavis/ldavis.js"></script>
        <link rel="stylesheet" type="text/css" href="ldavis/lda.css">
      </head>

      <body>
        <div id = "lda"></div>
        <script>
         var vis = new LDAvis("#lda", "ldavis/lda.json");
        </script>
      </body>

What I see in the Rstudio Viewer (LDAvis works)

What I see in the browser - tested on Chrome, Firefox, Brave and Internet explorer (no LDAvis output)

sessionInfo()

## R version 4.0.0 (2020-04-24)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18362)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] ggcorrplot_0.1.3  plotly_4.9.2.1    ggdendro_0.1-20   ggplot2_3.3.2    
##  [5] tidyr_1.1.0       magrittr_1.5      dplyr_1.0.0       data.table_1.12.8
##  [9] LDAvis_0.3.2      textmineR_3.0.4   Matrix_1.2-18     stm_1.3.5        
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.4.6       compiler_4.0.0     pillar_1.4.4       tools_4.0.0       
##  [5] stopwords_2.0      digest_0.6.25      viridisLite_0.3.0  jsonlite_1.7.0    
##  [9] evaluate_0.14      lifecycle_0.2.0    tibble_3.0.1       gtable_0.3.0      
## [13] lattice_0.20-41    pkgconfig_2.0.3    rlang_0.4.6        rstudioapi_0.11   
## [17] yaml_2.2.1         xfun_0.15          httr_1.4.1         withr_2.2.0       
## [21] stringr_1.4.0      knitr_1.29         htmlwidgets_1.5.1  generics_0.0.2    
## [25] fs_1.4.1           vctrs_0.3.1        grid_4.0.0         tidyselect_1.1.0  
## [29] glue_1.4.1         R6_2.4.1           rmarkdown_2.3      purrr_0.3.4       
## [33] MASS_7.3-51.5      scales_1.1.1       htmltools_0.5.0    ellipsis_0.3.1    
## [37] RcppProgress_0.4.2 usethis_1.6.1      colorspace_1.4-1   stringi_1.4.6     
## [41] lazyeval_0.2.2     munsell_0.5.0      crayon_1.3.4

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

5 participants