Chris Chris - 11 months ago 83
R Question

Integrating LDAvis with shiny

I'm trying to take advantage of the shiny integration that came with LDAvis version 0.3.1. The example given on github (https://github.com/cpsievert/LDAvis/tree/master/inst/examples/shiny) is:

library(LDAvis)
library(shiny)

server <- shinyServer(function(input, output, session) {
output$myChart <- renderVis({
with(TwentyNewsgroups,
createJSON(phi, theta, doc.length, vocab, term.frequency,
R = input$nTerms))})
})

ui <- shinyUI(
fluidPage(
sliderInput("nTerms", "Number of terms to display", min = 20, max = 40, value = 30),
visOutput('myChart')
)
)

shinyApp(ui = ui, server = server)


This app doesn't render any outputs, though, so I'm a bit confused. Can anyone help with a minimal example of how to integrate LDAvis into a shiny app?

Answer Source

This piece of code worked on my machine, still the javascript takes some time to laod (about 20 seconds). Does this help you?

library(shiny)
library(LDAvis)

data(TwentyNewsgroups, package = "LDAvis")
ui <- shinyUI(
  fluidPage(
    sliderInput("nTerms", "Number of terms to display", min = 20, max = 40, value = 30),
    visOutput('myChart')
  )
)

server <- shinyServer(function(input, output, session) {
  output$myChart <- renderVis({
    if(!is.null(input$nTerms)){
      with(TwentyNewsgroups, 
           createJSON(phi, theta, doc.length, vocab, term.frequency, 
                      R = input$nTerms))


    } 
           })
})

shinyApp(ui = ui, server = server)

enter image description here