在shinyApp中使用R text2vec包和LDAvis的LDA主题模型 [英] LDA topic model using R text2vec package and LDAvis in shinyApp

查看:21
本文介绍了在shinyApp中使用R text2vec包和LDAvis的LDA主题模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

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

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

我想知道如何才能访问doc_len"的私有变量.我尝试了 lda_model$doc_len 和 lda_model$private$doc_len,但它们返回了NULL".

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".

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

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".

感谢您在提取 lda 模型的私有参数或如何在自己闪亮的应用页面中使用lda_model$plot()"绘制 LDAvis 的响应和帮助.

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.

谢谢,山姆

推荐答案

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

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)

闪亮:

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

这是可行的,因为 ... 传递给 LDAvis::createJSONLDAvis::serVis(如文档所述):

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

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

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

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

这篇关于在shinyApp中使用R text2vec包和LDAvis的LDA主题模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆