rshinyvisualizationtopic-modelingtext2vec

LDA topic model using R text2vec package and LDAvis in shinyApp


Here is the code for LDA topic modelling with R text2vec package:

library(text2vec)

tokens = docs$text %>%  # docs$text: a colection of text documents
  word_tokenizer

it = itoken(tokens, ids = docs$id, progressbar = FALSE)
v = create_vocabulary(it) %>%   
    prune_vocabulary(term_count_min = 10, doc_proportion_max = 0.2)
vectorizer = vocab_vectorizer(v)
dtm = create_dtm(it, vectorizer, type = "dgTMatrix")

lda_model = text2vec::LDA$new(n_topics = 10, doc_topic_prior = 0.1, topic_word_prior = 0.01)

doc_topic_distr = lda_model$fit_transform(x = dtm, n_iter = 1000, 
                          convergence_tol = 0.001, n_check_convergence = 25, 
                          progressbar = FALSE)

As far as I understand there are two sets of variables, named as public and private, please see the below image:

enter image description here

I am wondering how can I have access to private variable of "doc_len". I tried lda_model$doc_len and lda_model$private$doc_len, but they returned "NULL".

The reason I need is that the command "lda_model$plot()" plots LDAvis in R console, but I need to plot it in my own shiny app page. To do this, I want to extract all parameters for the following function as discussed in the below link: "https://github.com/cpsievert/LDAvis/issues/27".

I appreciate your response and help either for extracting private parameters of lda model or how to plot LDAvis with "lda_model$plot()" in own shiny app page.

Thanks, Sam


Solution

  • Private fields are private for a purpose - they are specifically hided for a user and not part of public API (can be easily changed in future or removed). The correct way of embedding LDAvis to a shiny app is to store LDAvis json on disk and then open it in a shiny app. Something like should work:

    lda_model$plot(out.dir = "SOME_DIR", open.browser = FALSE)
    

    And in shiny:

    output$myChart <- renderVis(readLines("SOME_DIR/lda.json"))
    

    This works because ... passed to LDAvis::createJSON and LDAvis::serVis (as documentation says):

    $plot(lambda.step = 0.1, reorder.topics = FALSE, ...)

    plot LDA model using https://cran.r-project.org/package=LDAvis package. ... will be passed to LDAvis::createJSON and LDAvis::serVis functions