2016-08-17 71 views
1

我有一個簡單的python程序,它應該將其數據推送到R Shiny應用程序中。在閃亮的這些線,解析「GET」輸入:在R中接收推送數據Shiny應用程序

# Parse the GET query string 
    output$queryText <- renderText({ 
    query <- parseQueryString(session$clientData$url_search) 
    eventList[query$eventid] <<- query$event 
    }) 

這正常使用瀏覽器調用「http://127.0.0.1:5923/?eventid=1223&event=somestring」。如果我嘗試在python中調用URL,我會在R中獲得「通過對等方重置連接」,並且沒有任何內容添加到列表中。到目前爲止,我的Python代碼:

request = urllib2.Request("http://127.0.0.1:5923/?eventid=1223&event=somestring") 
test = urllib2.urlopen(request) 

有誰知道如何得到這個工作還是有更好的解決方案,以推動從外部數據到一個R閃亮的應用程序?

感謝您的幫助!

+0

客戶端與閃亮的應用程序進行交互需要處理JavaScript /的WebSockets其中據我所知,'urllib2'庫做不。 – jdharrison

+0

閃亮的應用程序是客戶端 - 服務器應用程序,所以你需要運行Javascript的東西。潛在的候選人是https://github.com/niklasb/dryscrape另一個潛在的解決方案http://www.seleniumhq.org/ –

+0

感謝您的意見。我通過使用httpuv和創建websocket服務器解決了我的問題。例子在這裏找到:https://github.com/rstudio/httpuv/blob/master/demo/daemon-echo.R – tanktoo

回答

2

我使用的WebSockets與httpuv完整的解決方案:

library(httpuv) 
startWSServer <- function(){ 
    if(exists('server')){ 
     stopDaemonizedServer(server) 
    } 
    app <- list(
     onWSOpen = function(ws) { 
     ws$onMessage(function(binary, message) { 
      #handle your message, for example save it somewhere 
      #accessible by Shiny application, here it is just printed 
      print(message) 
      ws$send("message received") 
     }) 
     } 
    ) 
    server <<- startDaemonizedServer("0.0.0.0", 9454, app) 
} 

stopWSServer <- function(){ 
    stopDaemonizedServer(server) 
    server <<- NULL 
} 

希望這有助於;)

相關問題