Return 来自 R 中闪亮服务器函数的几个对象,用于首先绘制 LDAvis 图

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

下面的代码是我在闪亮的应用程序中使用 topic_model 函数中的 text2vec 绘制 LDA 图的代码。 input$date 是一个 checkboxGroupInput 选择,input$data 非常适合 DT::renderDataTable 输出并且 topic_model 在应用程序外运行良好。 我找到了如何在闪亮的应用程序中获取 LDA 图,但我并没有真正按照原样复制它。 input$go 是一个简单的操作按钮。

  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") 
     })

这是我收到的错误:Listening on http://127.0.0.1:3363 Warning: Error in exists: invalid first argument [No stack trace available]

使用:req(input$data) 而不是 if(!exists(input$data)) return()input$data 未填写时 == ""exists("") 将抛出该错误。

还有eventReactive看起来不对。它没有做任何事情。您的意思是:

  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)
  })