Incompatibility between graph NLP and leaflet

42 Views Asked by At

I have a shiny R application in which I have a leaflet card and also an NLP object from LDAvis. There seems to be an incompatibility between the LDAvis graph object and leaflet, as the geometries on the map are not displayed.

To display the LDAvis graph, execute the commented code and generate the objects (json, js, ...) in the line lda_model$plot(...) and then load the generated json file into the readlines function

library(shiny)
library(LDAvis)
library(leaflet)

ui <- fluidPage(
  leaflet::leafletOutput("mymap"),
  tags$br(),
  tags$br(),
  
  LDAvis::visOutput('ldavis_plot')
)





server <- function(input, output) {
  

  output$mymap <- leaflet::renderLeaflet({
    leaflet::leaflet() %>% leaflet::addTiles() %>%
      leaflet::addRectangles(
        lng1=-118.456554, lat1=34.078039,
        lng2=-118.436383, lat2=34.062717,
        fillColor = "transparent"
      )
  })
  
  
  
  
  output$ldavis_plot <- LDAvis::renderVis({
    # tokens = tolower(movie_review$review[1:1000])
    # tokens = word_tokenizer(tokens)
    # it = itoken(tokens, ids = movie_review$id[1:1000], progressbar = FALSE)
    # v = create_vocabulary(it)
    # v = prune_vocabulary(v, 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 = 5, doc_topic_prior = 0.1, topic_word_prior = 0.01)
    # doc_topic_distr = 
    #   lda_model$fit_transform(x = dtm, n_iter = 100, 
    #                           convergence_tol = 0.001, n_check_convergence = 25, 
    #                           progressbar = FALSE)
    # 
    # lda_model$plot(out.dir = "PATH_TO", open.browser = FALSE)
    
    
    readLines("PATH_TO/lda.json")
  })
  
}

shinyApp(ui, server)

Does anyone have any ideas on how to get around this incompatibility?

0

There are 0 best solutions below