首页 > 解决方案 > 使用 LDAvis 和 R shinyapp 和参数设置进行主题建模可视化

问题描述

我在 R 闪亮的应用程序中使用 LDAvis。

这是代码,它可以正常工作。

# docs is a csv file with a "text" column, e.g.   
# docs <- read.csv("docs.csv",sep=",",header=TRUE)

ui <- navbarPage(
  title = "NLP app",

  tabPanel("Topic Model",icon = icon("group"), 
           fluidPage(

             headerPanel(""),
             titlePanel(p(h2("Topic Modelling example",style = "color:#4d3a7d"))),

             #sidebarPanel(
             wellPanel(tags$style(type="text/css", '#leftPanel { width:200px; float:left;}'), style = "background: lightgrey",
                       id = "leftPanel",
                       sliderInput("nTopics", "Number of topics to display", min = 5, max = 50, value = 10, step=5),
                       sliderInput("nTerms", "#top terms per topic", min = 10, max = 50, value = 20, step=5),
                       tags$hr(),
                       actionButton(inputId = "GoButton", label = "Go",  icon("refresh"))
         ),
             mainPanel( 
               tabPanel("Topic Visualisation", hr(),helpText(h2("Please select a topic!")),  visOutput('visChart')))
           )
  )
)

# server
server <- function(input, output, session) {

  Topic_Subset <- reactive({

    docs <- docs$text  
    nTopics <- input$nTopics

    # topic model using text2vec package
    tokens = docs %>% 
      tolower %>% 
      word_tokenizer

    it = itoken(tokens, progressbar = FALSE)
    v = create_vocabulary(it,stopwords=tm::stopwords("en")) 
    vectorizer = vocab_vectorizer(v)
    dtm = create_dtm(it, vectorizer, type = "dgTMatrix")

    lda_model = text2vec::LDA$new(n_topics = nTopics, doc_topic_prior = 0.1, topic_word_prior = 0.01)
    lda_model$fit_transform(x = dtm, n_iter = 1000, 
                        convergence_tol = 0.001, n_check_convergence = 25, 
                        progressbar = FALSE)

    return(lda_model) # 
  })

  output$visChart <- renderVis({

    input$GoButton

    isolate({
      nterms    <- input$nTerms
      lda_model <- Topic_Subset()
    })

    lda_model$plot(out.dir = "./results", R = nterms, open.browser = FALSE)

   readLines("./results/lda.json")

  })
}


shinyApp(ui = ui, server = server)

我想看看是否可以在 visOutput('visChart') 中使用 width = "80%" ,例如 wordcloud2Output("a_name",width = "80%"); 或任何使可视化尺寸更小的替代方法。特别是,当我最小化闪亮的应用程序窗口时,该图不适合页面。我的第二个问题是:如何用另一个数字(如 0.6(不是 1))初始化参数 lambda(请参见下图和黄色高亮部分)?

在此处输入图像描述

标签: rshinyvisualizationtopic-modeling

解决方案


推荐阅读