使用LDAvis和R Shinyapp和参数设置进行主题建模可视化

时间:2019-05-02 04:01:40

标签: r shiny visualization topic-modeling

我在R Shiny应用程序中使用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(请参见下图和黄色高亮显示)?

enter image description here

0 个答案:

没有答案