rshinyldatext2vec

Return several objects from a shiny server function in R for plotting an LDAvis plot first


The code below is the one I am using for plotting an LDA plot using text2vec inside topic_model function in a shiny app. input$date is a checkboxGroupInput selection, input$data works perfectly fine for a DT::renderDataTable output & topic_model runs well outside the app. Here I found how to get an LDA plot in a shiny app, but I didn't really get it so copied as it was. input$go is a simple actionButton.

  raw_data <- eventReactive(input$data,{
    inFile1 <- input$data
    if (is.null(inFile1)) {
      return(NULL)
    } else {
      return(read.csv(inFile1$datapath, sep = input$sep1, header = T, stringsAsFactors = F)) 
    }
  }) # eventReactive - raw_data

  stopwords <- eventReactive(input$stopwords,{ #needed for topic_model
    inFile2 <- input$stopwords
    if (is.null(inFile2)) {
      return(NULL)
    } else {
      return(read.csv(inFile2$datapath, sep = input$sep2, header = F, stringsAsFactors = F)) 
    }
  }) # eventReactive - stopwords

  data <- eventReactive(input$date, {                   #raw_data turns into data            
    if(!exists(input$data)) return()                    # if no upload

    switch (input$date,
            "ydm" = ydm(raw_data()$File.Date),
            "mdy" = mdy(raw_data()$File.Date),
            "dmy" = dmy(raw_data()$File.Date),
            "ydm" = ydm(raw_data()$File.Date)
            ) 
    return(raw_data())
  }) # reactive - date switch

  plot_data <- eventReactive(input$go, {
    stopwords <- stopwords()
    dats <- data()
    topic_model(dats)
    return(list(lda_model, dtm, doc_topic_distr,k))
  }) # reactive - modeling 


   output$LDAplot <- renderVis({ 
     plot_data()[[1]]$plot(out.dir = "SOME_DIR", open.browser = FALSE)
     readLines("SOME_DIR/lda.json") 
     })

This is the Error I am getting: Listening on http://127.0.0.1:3363 Warning: Error in exists: invalid first argument [No stack trace available]


Solution

  • Use: req(input$data) instead of if(!exists(input$data)) return(). When input$data hasn't been filled out it == "". exists("") will throw that error.

    Also eventReactive looks wrong. It's not doing anything. Did you mean:

      data <- eventReactive(input$date, {                   #raw_data turns into data            
        if(!exists(input$data)) return()                    # if no upload
        df <- raw_data()
        df$File.Date <- switch (input$date,
                "ydm" = ymd(raw_data()$File.Date),
                "mdy" = mdy(raw_data()$File.Date),
                "dmy" = dmy(raw_data()$File.Date),
                "ydm" = ydm(raw_data()$File.Date)
                ) 
        return(df)
      })