使用watchEvent中函数的输出作为闪亮应用程序中global.R的输入?

时间:2019-11-01 19:02:23

标签: r shiny reactive-programming shiny-reactivity

我有一个带有多个标签的闪亮应用。第一个标签从用户那里上传术语,然后在单击搜索时以弹性方式搜索那些关键字,这些都在功能keyword_search中–我的问题是,一旦该功能返回一个数据框,该数据框就是输入到global.R(text_global.R),直到用户单击运行按钮,我才需要运行它,因为否则需要的数据在keyword_search函数完成且应用程序将错误了。因此,另一个问题是global.R卡在eventReactive中,因此我不确定这是否妨碍了在其余renderPlots中使用从该文件创建的所有变量的能力。 / p>

我了解到,text_global.R内包含eventReactive可以将其容器化,而不能在server的其他部分中使用它,但是如果我将其取出并使其实际变为全局,则它可以在会话开始,这只会使我的应用出错,因为它需要的数据在运行keyword_search函数之前不存在。我试图通过在其前面添加eventReactive并将{(1)}用作data() <-的输入,来使text_global.R成为可调用的函数,但这是行不通的。

这是我的闪亮代码以及我现在正在尝试的代码(抱歉,它很长,但想提供上下文):

ui <- navbarPage("App", theme = shinytheme("united"),

          tabPanel("Search Your Terms",
                   titlePanel("Welcome"),
                   br(),
                   br(),
                   mainPanel("Please upload .XLSX file of the terms that you would like searched.",
                             br(),
                             br(),
                    "When you hit the search button, the code will pull the data, then you can navigate to the next tabs to run each type of analysis.",
                             br(),
                             br(),

                      fileInput('file1', 'Choose .xlsx file of terms:',
                                             accept = c(".xlsx")),

                      actionButton("srch", "Search"),

                      tableOutput('contents'))),

          tabPanel("Text Analysis",

                   titlePanel("Word Analysis"),

                   tabsetPanel(


                      tabPanel("Word Cloud", 

                            sidebarPanel(width = 2, 
                                          actionButton("runtext", "Run Text Analysis"),

                            column(width = 10, align='center',
                                   sliderInput("cloudsize", "Choose size (larger size shows more words)", min = .1, max = .9, value = .8, step = .1),
                                   wordcloud2Output('wcplot', height = '500px'))),


                      tabPanel("Word Counts",

                            column( width = 10, 
                                    uiOutput("data1"),
                                   plotOutput('counts'),
                                   dataTableOutput("wordcount_dt"))),

                      tabPanel("Common Word Sequence",

                            column( width = 10,
                                    uiOutput("data2"),
                                   plotOutput('bigrams'),
                                   dataTableOutput("bigram_dt")))

                  )),

          tabPanel("Topic Modeling",

                   titlePanel("Topics"),

                  mainPanel(width = 10,
                            plotOutput("topics"),
                            dataTableOutput("topiccontents"))),

          tabPanel("Social Network Analysis",


                   sidebarPanel(actionButton("netsrch", "Get"),
                                actionButton("runnet", "Create"), 
                                             width = 2),

                   mainPanel(visNetworkOutput("network"),
                             dataTableOutput("netdt")
                  ))
)

server <- function(input, output) {


  output$contents <- renderTable({
    req(input$file1)

    inFile <- input$file1

    read_excel(inFile$datapath, 1)
   })


  source('keyword_search.R')


  observeEvent(input$srch, {
    inFile <- input$file1

    file <- read_excel(inFile$datapath, 1)

    file <- na.omit(file)

    keyword_search(file)
    showModal(modalDialog("Search finished, head to the next tab!"))
  })

#this text_global.R has variables that are used for over half of my app, how #do I make its contents available for everything WITHOUT having it run the #moment the session starts?  

  eventReactive(input$runtext, {
    source('text_global.R')
  })


  output$data1 <- renderUI({
    selectInput("data1", "Choose which you would like to view:", choices = c(tfdf_forplot$report_name))
  })


  output$wcplot <- renderWordcloud2({
    wordcloud2(forwordcloud2, size = (input$cloudsize), color = rep_len( c("orange", "teal", "gray"), nrow(forwordcloud2)))
  })


  output$counts <- renderPlot({
      ggplot(tfdf_forplot[tfdf_forplot$report_name==input$reportchoose,]) + (aes(word, tf_idf)) +
      geom_col(show.legend = FALSE, fill = '#cc3300', alpha = .9) +
      labs(x = NULL, y = "tf-idf") +
      coord_flip() +
      theme_hc()

  })


  output$wordcount_dt <- renderDataTable({
    datatable(tidy_output, colname = c("Report Name", "Word", "Count of Word", "Total Words", "tf", "idf", "tf_idf"))
  })

  output$data2 <- renderUI({
  selectInput("data2", "Choose which device you would like to view:", choices = c(bigram_forplot$report_name))
  })

  output$bigrams <- renderPlot({
    ggplot(bigram_forplot[bigram_forplot$report_name==input$reportchoose2,]) + aes(bigram, tf_idf, fill = report_name) +
      theme(text = element_text(size=6)) +
      geom_col(show.legend = FALSE, fill = '#cc3300', alpha = .9) +
      labs(x = NULL, y = "tf-idf") +
      coord_flip() +
      theme_hc()
  })

  output$bigram_dt <- renderDataTable({
    bigram_tf_idf
  })

 output$topics <- renderPlot({
   ggplot(top_terms) + aes(term, beta, fill = factor(topic)) +
     theme(text = element_text(size=10)) +
     geom_col(show.legend = FALSE, fill = '#cc3300', alpha = .9) +
     facet_wrap(~ topic, scales = "free") +
     coord_flip()
 }) 

 output$topiccontents <- renderDataTable({
   report_contents
 })

 eventReactive(input$runnet, {
   source('sna_global.R')
 })

 output$network <- renderVisNetwork({
   visIgraph(g2, idToLabel = TRUE) %>% 
     visOptions(highlightNearest = list(enabled = TRUE, labelOnly=FALSE, 
                                        degree=list(from = 1, to = 1),                                         algorithm="hierarchical"),nodesIdSelection = TRUE)  
 })

 source('report_search.R')

 observeEvent(input$netsrch, {
   report_search()
   showModal(modalDialog("Network is Ready!"))
 })

 output$netdt <- renderDataTable({
   datatable(nodelist, rownames = FALSE, 
             filter = 'top', extensions = 'Buttons', 
             options = list(columnDefs = list(list(className = 'dt-center', targets = c(0:3))), 
                            pageLength = 10, dom = 'Bfrtip', buttons = c('copy', 'csv', 'excel', 'pdf', 'print')))
 })

}

shinyApp(ui = ui, server = server)

我的{。{1}}应该在global.R文件中的所有内容都不断出现“未找到”错误。如何使用renderPlots的输出并在global.R中使用它,以及使之可以使其余服务器组件均可访问global.R,任何建议都将不胜感激。 t运行,直到单击一个按钮。抱歉,我知道这是一个复杂的问题。我希望我能解释清楚

所有这些都在闪亮的应用程序之外起作用,所以我知道global.R中的代码本身是正确的。

0 个答案:

没有答案