0

Here is the Code I copied it from another stack question and want to integrate multiple tabs with the same LDAvis but a different dataset.

The Visualistion in the first tab (Topic_Subset_0) works fine, but in the second tab, the values are not correct and the lamda bar from the second tab is displayed in the first tab. So there are multiple lamda bars in the first tab.

When I create just one tab, everything works fine, but with mulitple tabs the lambdabars, multiple are all visible in the first tab. Does someone know how to fix tis issue.

I would like to create 3 or 4 tabs, all with the LDAvis and different datasets so I can test the topic model parameters on different datasets in one shiny app.

here the code:

the data ist a text col from a df with multiple cols and the text col comes with individual comments data <- data$text which is tokeneized in the topic_subset function.

UI

create two individual tabpanels

´´´

ui <- fluidPage(

  tabsetPanel(id = "tabs",
              tabPanel("Alle", 
                       fluidPage(
                         
                         headerPanel(""),
                         titlePanel(p(h2("Topic Modelling_0",style = "color:#4d3a7d"))),
                         
                         #sidebarPanel(
                         wellPanel(tags$style(type="text/css", '#leftPanel { width:200px; float:left;}'), style = "background: lightgrey",
                                   id = "leftPanel",
                                   sliderInput("nTopics_0", "Number of topics to display_0", min = 5, max = 50, value = 10, step=5),
                                   sliderInput("nTerms_0", "#top terms per topic_0", min = 10, max = 50, value = 20, step=5),
                                   tags$hr(),
                                   actionButton(inputId = "GoButton_0", label = "Go_0",  icon("refresh"))
                         ),
                         mainPanel( 
                           tabPanel("Topic Visualisation_0", hr(),helpText(h2("Please select a topic!_0")),  visOutput('visChart_0')))
                       )
              ),
              tabPanel("ursächlich", 
                       fluidPage(
                         
                         headerPanel(""),
                         titlePanel(p(h2("Topic Modelling_1",style = "color:#4d3a7d"))),
                         
                         #sidebarPanel(
                         wellPanel(tags$style(type="text/css", '#leftpanel { width:200px; float:left;}'), style = "background: lightgrey",
                                   id = "leftpanel",
                                   sliderInput("nTopics_1", "Number of topics to display_1", min = 5, max = 50, value = 10, step=5),
                                   sliderInput("nTerms_1", "#top terms per topic_1", min = 10, max = 50, value = 20, step=5),
                                   tags$hr(),
                                   actionButton(inputId = "GoButton_1", label = "Go_1",  icon("refresh"))
                         ),
                         mainPanel( 
                           tabPanel("Topic Visualisation_1", hr(),helpText(h2("Please select a topic!_1")),  visOutput('visChart_1')))
                       )
              )
  ))

´´´´

server

2 topic_Subsets for each tab an individual LDA Visualisation

´´´´

server <- function(input, output, session) {
  
  Topic_Subset_0 <- reactive({
    
    docs_0 <- data$Bemerkung
    nTopics_0 <- input$nTopics_0
    stopwords_1 <- tm::stopwords("german")
    stopwords_2 <- c("wurde", "dass", "schon", "mehr", "dadurch", "oft", "hätte", "viele", "statt", "daher", "immer")
    
    
    # topic model using text2vec package
    tokens_0 = docs_0 %>%
      tolower %>%
      word_tokenizer
    
    
    it_0 = itoken(tokens_0, progressbar = FALSE)
    v_0 = create_vocabulary(it_0,stopwords=c(stopwords_1,stopwords_2))
    vectorizer_0 = vocab_vectorizer(v_0)
    dtm_0 = create_dtm(it_0, vectorizer_0, type = "dgTMatrix")
    
    lda_model_0 = text2vec::LDA$new(n_topics = nTopics_0, doc_topic_prior = 0.1, topic_word_prior = 0.01)
    lda_model_0$fit_transform(x = dtm_0, n_iter = 1000,
                              convergence_tol = 0.001, n_check_convergence = 25,
                              progressbar = FALSE)
    
    return(lda_model_0) #
  })
  
  output$visChart_0 <- renderVis({
    
    input$GoButton_0
    
    isolate({
      nterms_0    <- input$nTerms_0
      lda_model_0 <- Topic_Subset_0()
    })
    
    lda_model_0$plot(out.dir = "./results_0", R = nterms_0, open.browser = FALSE)
    
    readLines("./results_0/lda.json")
    
  })
  
  
  
  Topic_Subset_1 <- reactive({
    
    
    docs_1 <- data_1$Bemerkung 
    nTopics_1 <- input$nTopics_1
    stopwords_1 <- tm::stopwords("german")
    stopwords_2 <- c("wurde", "dass", "schon", "mehr", "dadurch", "oft", "hätte", "viele", "statt", "daher", "immer")
    
    
    # topic model using text2vec package
    tokens_1 = docs_1 %>% 
      tolower %>% 
      word_tokenizer
    
    it_1 = itoken(tokens_1, progressbar = FALSE)
    v_1 = create_vocabulary(it_1,stopwords=c(stopwords_1,stopwords_2)) 
    vectorizer_1 = vocab_vectorizer(v_1)
    dtm_1 = create_dtm(it_1, vectorizer_1, type = "dgTMatrix")
    
    lda_model_1 = text2vec::LDA$new(n_topics = nTopics_1, doc_topic_prior = 0.1, topic_word_prior = 0.01)
    lda_model_1$fit_transform(x = dtm_1, n_iter = 1000, 
                              convergence_tol = 0.001, n_check_convergence = 25, 
                              progressbar = FALSE)
    
    return(lda_model_1) # 
  })
  
  output$visChart_1 <- renderVis({
    
    input$GoButton_1
    
    isolate({
      nterms_1    <- input$nTerms_1
      lda_model_1 <- Topic_Subset_1()
    })
    
    lda_model_1$plot(out.dir = "./results_1", R = nterms_1, open.browser = FALSE)
    
    readLines("./results_1/lda.json")
    
  })
  
}

shinyApp(ui = ui, server = server)

´´´´

eager_DS
  • 1
  • 2
  • Welcome to SO. You maximize your chance of getting a useful answer if you provide a minimal reproducible example. This [post](https://stackoverflow.com/help/minimal-reproducible-example)  may help. – yuliaUU Apr 07 '23 at 18:53
  • Hi, thanks a lot for your comment. I tried to structure more visible. It is the minimal code, the problem appears only with two tabs, but I can´t share the data due to privacy reasons, but it is a col in a df with individual textcomments per row – eager_DS Apr 12 '23 at 12:12

0 Answers0