使用R text2vec包的LDA主题模型和在shinyApp中使用LDAvis

问题描述 投票:1回答:1

以下是使用R text2vec包进行LDA主题建模的代码:

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)

据我所知,有两组变量,分别为public和private,请看下图:

enter image description here

我想知道如何访问“doc_len”的私有变量。我尝试了lda_model $ doc_len和lda_model $ private $ doc_len,但是他们返回了“NULL”。

我需要的原因是命令“lda_model $ plot()”在R控制台中绘制LDAvis,但我需要在我自己的闪亮应用页面中绘制它。为此,我想提取以下函数的所有参数,如下面的链接所述:“https://github.com/cpsievert/LDAvis/issues/27”。

我感谢您的回复,并帮助提取lda模型的私有参数或如何在自己的闪亮应用页面中使用“lda_model $ plot()”绘制LDAvis。

谢谢,山姆

r shiny visualization topic-modeling text2vec
1个回答
1
投票

私有字段是专用的 - 它们专门为用户隐藏,而不是公共API的一部分(将来可以轻松更改或删除)。将LDAvis嵌入到闪亮的应用程序中的正确方法是将LDAvis json存储在磁盘上,然后在闪亮的应用程序中打开它。应该工作的东西:

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

有光泽的:

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

这是因为...传递给LDAvis::createJSONLDAvis::serVis(正如文档所说):

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

使用https://cran.r-project.org/package=LDAvis包绘制LDA模型。 ...将传递给LDAvis :: createJSON和LDAvis :: serVis函数

© www.soinside.com 2019 - 2024. All rights reserved.