内置Web服务器的R

问题描述 投票:3回答:3

自R 2.13起,R带有内置的Web服务器。有没有一种简单的方法可以使用带有自定义端口号的R创建本地Web服务器?

在python中将是(使用“ http:// localhost:8080”:

python -m SimpleHTTPServer 8080

我知道Rook,但我正在寻找一个简单的解决方案。

谢谢

r webserver
3个回答
1
投票

看看Rook-它在GitHub上。关于设置端口号,请参见?tools::help.ports,它接受数字矢量(整数),并且它将选择第一个可用的数字。还有sinartraCRANGitHub),但我宁愿选择Rook


1
投票

这不是答案,而是一些入门的地方。

如果我们从终端运行R(通过键入R),然后运行help.start(),它将启动本地主机并在浏览器中将其打开。在我的机器上,它打开http://127.0.0.1:16371/doc/html/index.html。这至少证明,可以在R中打开localhost,而无需任何外部软件包。

我已经在R Source Code中找到了一些实现此目的的代码。


1
投票

看起来servr可能就是您最近要寻找的东西。

来自github

在某种程度上,此软件包类似于python -m SimpleHTTPServer或python -m http.server。

https://github.com/yihui/servr

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

示例代码

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

将地址放置在浏览器中:http://127.0.0.1:7826

© www.soinside.com 2019 - 2024. All rights reserved.