簡體   English   中英

在 Shiny R 中繪制圖形; 使用重復循環繪制數據以進行自動分析

[英]Plotting graphs in Shiny R; using a repeat loop to plot data for automated analysis

編輯

我正在構建一個閃亮的應用程序,其中一個組件將包括自動數據分析。 我對它的主要用途是訪問 API 以收集數據並對其進行后續分析。 此操作發生在具有 5 分鍾延遲的repeat循環內(盡管這可以由應用程序的用戶指定)。 一旦延遲結束,API 將再次被訪問,整個過程重新開始。 這對我來說很有效,因為我在主控制台中將我的繪圖/表格作為列表返回。

但是,我無法在 Shiny 應用程序中執行它。 我無法提供 API 信息,但出於所有意圖和目的,這里是一個使用mpg數據集的可復制示例。

下面是使用操作按鈕生成的繪圖示例,而不使用重復循環。 每次單擊操作按鈕時,圖表都會隨當前系統時間更新:

在此處輸入圖片說明

以及生成此代碼的代碼:-

library(shiny)
library(ggplot2)

ui<-fluidPage(
  titlePanel('Minimal example'),
  tabsetPanel(
    
    tabPanel("Example",
             
             
             #summary
             sidebarPanel(width = 4, 
                          h5("The default interval for the analysis refresh is 5 minutes. If you wish to change this, please do so in the box below:"),
                          numericInput("intervaltime","Input refresh interval:",5),
                          br(),
                          h5("Press 'Run Analysis' button below to start automated analysis"),
                          actionButton("automatedanalysis", "Run Analysis")),
             mainPanel(
               h4("An example plot"),
               plotOutput("example_plot", width = "100%"),
               h4("Some text with updated system time"),
               textOutput("example_text")
             )
             
             
    )))



server<-function(input,output,session){
  
  
  observeEvent(input$automatedanalysis,{
    
    #interval=input$intervaltime*60
    #repeat{
    
      currenttime<-Sys.time()
      
      p<-ggplot(mpg, aes(displ, hwy, colour = class)) + 
        geom_point()+ggtitle(paste0("graph made at: ",currenttime))# adds on the current time
      
    output$example_plot<-renderPlot({
      return(p)
    })
    
    
    output$example_text<-renderText({
      
      print(paste0("The current system time is: ", Sys.time())) #a check to know that it is working
      
    })

    #Sys.sleep(interval)
        
    #}
  })
  
  
}



shinyApp(ui, server)

但是,當我使重復循環生效時,可以使用 UI 中的數字輸入切換間隔計時器,它不再起作用。 這是非工作代碼:-


ui<-fluidPage(
  titlePanel('Minimal example'),
  tabsetPanel(
    
    tabPanel("Example",
             
             
             #summary
             sidebarPanel(width = 4, 
                          h5("The default interval for the analysis refresh is 5 minutes. If you wish to change this, please do so in the box below:"),
                          numericInput("intervaltime","Input refresh interval:",5),
                          br(),
                          h5("Press 'Run Analysis' button below to start automated analysis"),
                          actionButton("automatedanalysis", "Run Analysis")),
             mainPanel(
               h4("An example plot"),
               plotOutput("example_plot", width = "100%"),
               h4("Some text with updated system time"),
               textOutput("example_text")
             )
             
             
    )))



server<-function(input,output,session){
  
  
  observeEvent(input$automatedanalysis,{
    
    interval=input$intervaltime*60
    repeat{
    
      currenttime<-Sys.time()
      
      p<-ggplot(mpg, aes(displ, hwy, colour = class)) + 
        geom_point()+ggtitle(paste0("graph made at: ",currenttime))# adds on the current time
      
    output$example_plot<-renderPlot({
      return(p)
    })
    
    
    output$example_text<-renderText({
      
      print(paste0("The current system time is: ", Sys.time())) #a check to know that it is working
      
    })

    Sys.sleep(interval)
        
    }
  })
  
  
}



shinyApp(ui, server)

沒有圖形或文本輸出出現。

再一次,這在 Shiny 應用程序之外工作正常,但我需要它作為我正在開發的 Shiny 應用程序中的一個功能。

總之,我怎樣才能讓它工作,以便在單擊操作按鈕時,分析​​在間隔期結束后刷新?

您應該查看invalidateLaterreactiveTimer函數。

我在以下示例中使用invalidateLater注意:該函數以毫秒為第一個參數)。

您也不應該在觀察者中放置任何輸出,創建一個reactiveVal / reactiveValues對象並在每個新間隔填充它。 然后您可以在應用程序中的任何位置使用該對象。

我還將observeEvent更改為普通的observe ,否則它只會在單擊 Button 時觸發。 現在,觀察者觸發,當按鈕被點擊時,間隔滑塊會發生變化,並且間隔已經過去。

library(shiny)
library(ggplot2)
ui<-fluidPage(
  titlePanel('Minimal example'),
  tabsetPanel(
    tabPanel("Example",
             sidebarPanel(width = 4, 
                          h5("The default interval for the analysis refresh is 5 minutes. If you wish to change this, please do so in the box below:"),
                          numericInput("intervaltime","Input refresh interval:",5),
                          br(),
                          h5("Press 'Run Analysis' button below to start automated analysis"),
                          actionButton("automatedanalysis", "Run Analysis")),
             mainPanel(
               h4("An example plot"),
               plotOutput("example_plot", width = "100%"),
               h4("Some text with updated system time"),
               textOutput("example_text")
             )
    )))

server<-function(input,output,session){
  rv <- reactiveVal(NULL)
  observe({
    interval = input$intervaltime*1000
    invalidateLater(interval, session)
    req(input$automatedanalysis)
    print("Fill ReactiveVal")
    mpg$hwy <- mpg$hwy * runif(nrow(mpg))
    rv(mpg)
  })
  output$example_plot<-renderPlot({
    req(rv())
    currenttime<-Sys.time()
    print("Plot Code")
    ggplot(rv(), aes(displ, hwy, colour = class)) + 
      geom_point()+ggtitle(paste0("graph made at: ",currenttime))
  })
  output$example_text<-renderText({
    print(paste0("The current system time is: ", Sys.time())) #a check to know that it is working
  })
}

shinyApp(ui, server)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM