Uruchom R / Rook jako serwer WWW przy starcie
Stworzyłem serwer za pomocą Rook in R -http://cran.r-project.org/web/packages/Rook Kod jest następujący
#!/usr/bin/Rscript
library(Rook)
s <- Rhttpd$new()
s$add(
name="pingpong",
app=Rook::URLMap$new(
'/ping' = function(env){
req <- Rook::Request$new(env)
res <- Rook::Response$new()
res$write(sprintf('<h1><a href="%s">Pong</a></h1>',req$to_url("/pong")))
res$finish()
},
'/pong' = function(env){
req <- Rook::Request$new(env)
res <- Rook::Response$new()
res$write(sprintf('<h1><a href="%s">Ping</a></h1>',req$to_url("/ping")))
res$finish()
},
'/?' = function(env){
req <- Rook::Request$new(env)
res <- Rook::Response$new()
res$redirect(req$to_url('/pong'))
res$finish()
}
)
)
## Not run:
s$start(port=9000)
$ ./Rook.r
Loading required package: tools
Loading required package: methods
Loading required package: brew
starting httpd help server ... done
Server started on host 127.0.0.1 and port 9000 . App urls are:
http://127.0.0.1:9000/custom/pingpong
Server started on 127.0.0.1:9000
[1] pingpong http://127.0.0.1:9000/custom/pingpong
Call browse() with an index number or name to run an application.
$
A proces kończy się tutaj.
Działa dobrze w powłoce R, ale chcę uruchomić go jako serwer podczas uruchamiania systemu. Tak więc po wywołaniu startu R nie powinien wyjść, ale czekać na żądania na porcie. Jak przekonam R, żeby po prostu czekał lub spał, a nie wychodził? Mogę użyć funkcji wait lub sleep w R, aby poczekać kilka N sekund, ale to nie pasuje idealnie do rachunku