2017-09-05 57 views
0

我在R中創建了一個儀表板。從6周前開始,我對R一無所知/很少,但上週我能夠完成我的版本1.現在的問題是如何在Raspberry Pi上呈現。顯示GGplots的最佳(或好)方法

我希望R腳本每15分鐘運行一次更新。我看到它的方式有兩種。 我可以從cron作業運行R腳本,但除非我從本機R環境運行R腳本,否則它不會在屏幕上顯示GGPlot。

其次,我可以從cron作業運行R腳本,輸出爲pdf,然後運行cron作業以顯示pdf。 Xpdf工作得很好,但這意味着每次迭代我需要終止最後一次迭代,然後重新運行Xpdf以再次打開文件。

第二個選項是一個工作,但不是很漂亮。我現在已經有了所有的代碼,但是我希望任何人都知道哪條路可以走。

回答

1

鑑於您使用Shiny構建儀表板,您可以使用invalidateLater()。退房的默認腳本吧:

if (interactive()) { 
    ui <- fluidPage(
    sliderInput("n", "Number of observations", 2, 1000, 500), 
    plotOutput("plot") 
) 

    server <- function(input, output, session) { 

    observe({ 
     # Re-execute this reactive expression after 1000 milliseconds 
     invalidateLater(1000, session) 

     # Do something each time this is invalidated. 
     # The isolate() makes this observer _not_ get invalidated and re-executed 
     # when input$n changes. 
     print(paste("The value of input$n is", isolate(input$n))) 
    }) 

    # Generate a new histogram at timed intervals, but not when 
    # input$n changes. 
    output$plot <- renderPlot({ 
     # Re-execute this reactive expression after 2000 milliseconds 
     invalidateLater(2000) 
     hist(rnorm(isolate(input$n))) 
    }) 
    } 

    shinyApp(ui, server) 
} 

來源:https://shiny.rstudio.com/reference/shiny/latest/invalidateLater.html

invalidateLater() function's值第一個參數是毫秒。 15分鐘是900000毫秒。

編輯:每內部命令,這的確是可以重複的腳本在一個特定的時間間隔,它可以以更多的方式來完成(link1link2或其他含invalidateLater()閃亮的情況下)。這裏遵循其中之一的適應在一個無限循環的形式地塊GGPLOT2每6秒柱狀圖:

library(ggplot2) 

plot_it <- function(){ 
    a <- rnorm(1000, mean = 50, sd = 10) 
    print(summary(a)) 
    p <- ggplot()+geom_histogram(aes(x=a), bins=40) 
    print(p) 
} 

repeat { 
    startTime <- Sys.time() 
    plot_it() 
    sleepTime <- startTime - Sys.time()+ 6 
    if (sleepTime > 0) 
    Sys.sleep(sleepTime) 
} 
+0

對不起 - 我沒有用閃亮的 - 只是GGPLOT2 –

+0

如果你不介意我問用什麼方式您是否僅使用ggplot2創建儀表板?通常當人們在R中談到儀表板時,使用閃亮的概念就是同義詞。至少對於我來說。對於內部R時間間隔重複,您也可以使用'重複'功能 –

+0

對不起,如果它是TMI,但只是爲了完整性。我連接到MySQL數據庫並提取信息。我使用R來對數字進行一些算術運算。然後,我使用GGPlot創建一些圖,然後將其排列在一個網格中。我有文字和徽標,這些徽標被轉換爲grobs進行排列。 –