2017-10-19 27 views
0

我有一個閃亮的應用程序,使系統調用啓動第二個進程,在這種情況下,一個c + +程序(由於各種原因Rcpp不是該項目的選項)。 C++程序需要一些時間才能運行,但會給終端提供連續的(如每秒幾次)反饋。連續更新的閃亮系統調用

我可以從閃亮的應用程序執行該程序並檢索系統調用的輸出,但它會等到該過程完成。

現在我的問題是,有沒有辦法不斷更新閃亮的應用程序內的文字?

作爲一個例子,我有以下光澤應用

ui <- bootstrapPage(
    verbatimTextOutput("text") 
) 

server <- function(input, output) { 
    a <- system("./tmp", intern = T, wait = F) 
    output$text <- renderText(paste(a, collapse = "\n")) 
} 

shinyApp(ui = ui, server = server) 

和下面的C++代碼(在tmp.cppg++ tmp.cpp -o tmp編譯)

#include <unistd.h> 
#include <stdio.h> 

int main() { 
    for (int i = 0; i < 10; ++i) { 
     printf("i is now %i\n", i); // print the current state 
     fflush(stdout);    // force the print 
     usleep(1000000);   // sleep for one second 
    } 
    return 0; 
} 

等待十秒鐘後,輸出獲取顯示,而是我想要顯示每一步。

對此的任何想法/解決方案?非常感謝!

+1

您可以將您的C++輸出保存爲文本文件並「反應」地讀取該文件。看到我的答案[這裏](https://stackoverflow.com/questions/46719268/shiny-app-does-not-reflect-changes-in-update-rdata-file/46747407#46747407)(選項4)。 –

+0

看起來很完美,如果你寫一個簡短的答案,我會很樂意接受並接受它。 – David

回答

1

正如評論中所建議的,我主張通過文本文件在C++和閃亮之間建立連接。我的回答here向您展示瞭如何「反應性地」導入文件。以下內容從鏈接答案中的選項4改編而來。

請注意,對於每個連接的用戶,該過程將開始一次,因此您可能需要通過將system("./tmp > mytext.txt", intern = F, wait = FALSE)行移動到global.R來調整此代碼。

ui <- bootstrapPage( 
    verbatimTextOutput("text") 
) 

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

    system("./tmp > mytext.txt", intern = F, wait = FALSE) 

    observe({ 
    ## read the text file once every 50 ms 
    invalidateLater(50, session) 
    req(file.exists("mytext.txt")) 
    txt <- paste(readLines("mytext.txt"), collapse = "\n") 
    output$text <- renderText(txt) 
    }) 
} 

shinyApp(ui = ui, server = server)