R built in Web server

蹲街弑〆低调 提交于 2020-07-05 03:03:42

问题


Since R 2.13, R comes with a built-in web server. Is there a simple way to create a local web server using R with a custom port number?

In python it will be (to use "http://localhost:8080") :

python -m SimpleHTTPServer 8080

I'm aware of Rook but I look for a simple solution.


回答1:


Take a look at Rook - it's on GitHub. About setting a port number, see ?tools::help.ports it accepts a vector of numbers (integers) and it will choose the first available one. There's also sinartra (CRAN, GitHub), but I'd rather go with Rook.




回答2:


This is not an answer, but some places to get started.

If we run R from terminal (by typing R), then run help.start(), it will start a local host and open it in the browser. On mine, it opens http://127.0.0.1:16371/doc/html/index.html. This at least proves that it's possible to open a localhost in R without any external packages.

I have found some of the code that makes this happen in the R Source Code.




回答3:


It looks like servr might be what you're looking for these days.

From github:

To some degree, this package is like python -m SimpleHTTPServer or python -m http.server.

https://github.com/yihui/servr

https://cran.rstudio.com/web/packages/servr/index.html

Example code

writeLines("<h1>Hi</H1>", "index.html")

# install.packages("servr")
library(servr)
servr::httd()

# createTcpServer: address already in use
# To stop the server, run servr::daemon_stop(2) or restart your R session
# Serving the directory /Users/st/R/localhost at http://127.0.0.1:7826

Place the address in the browser: http://127.0.0.1:7826



来源:https://stackoverflow.com/questions/12636764/r-built-in-web-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!