简体   繁体   English

如何在R中提供html页面?

[英]How can I serve html pages in R?

I have a folder with html files and I want to start a simple HTTP server that serves the requested file. 我有一个包含html文件的文件夹,我想启动一个提供所请求文件的简单HTTP服务器。

I have been trying to use Rook , but it asks for an app function that generates the HTML response. 我一直在尝试使用Rook ,但它要求生成HTML响应的应用程序功能。

library(Rook)
server <- Rhttpd$new()
server$start(quiet=TRUE)
server$add(name="my_app", app="path/to/app.R")

I guess I could somehow tell the app function to read the contents of the requested HTML file and use that as the response, but there should be an easier way. 我想我可以以某种方式告诉应用程序函数读取所请求的HTML文件的内容并将其用作响应,但应该有一种更简单的方法。

I know this post is a little old but I faced a similar problem and so thought I would post my solution here. 我知道这篇文章有点旧,但我遇到了类似的问题,所以我想在这里发布我的解决方案。

[Aside: I wanted to serve regular html/js/css via rook in conjuction with json responses to ajax queries with statistical solution - hence wanting to use R] [旁白:我想通过车辆提供常规的html / js / css与json对统计解决方案的ajax查询的响应 - 因此想要使用R]

R.server <- Rhttpd$new(); # Create server

# Use a Builder to add a 
staticApp <- Builder$new( 
  Static$new(
      urls=c('/www/css',
             '/www/js/libs',
             '/www/js',
             '/www/img',
             '/www'),
  root=getwd()
))
R.server$add(app=staticApp, name="static")
R.server$start()

Essentially my working directory contained contains a folder called www which contains all my static resources (in subfolders css, js, etc). 基本上我的工作目录包含一个名为www的文件夹,其中包含我的所有静态资源(在子文件夹css,js等中)。 In particular if the folder www contains a file index.html then this can be accessed via localhost:23702/custom/static/www/index.html 特别是如果文件夹www包含一个文件index.html,那么这可以通过localhost访问:23702 / custom / static / www / index.html

Other apps for more R-focused operations can be easily included in the builders construction. 其他用于更多R-focused操作的应用程序可以轻松地包含在构建器构造中。

Not probably would be cleaner with www moved to root = file.path(getwd(),'www') and a recursive search for all subfolders. www移动到root = file.path(getwd(),'www')并对所有子文件夹进行递归搜索可能不会更干净。

Hope this helps! 希望这可以帮助!

You should have a look at the example at ?Builder . 你应该看一下?Builder的例子。

You right, Rook expects you to provide an "app". 你是对的, Rook希望你提供一个“app”。 An "app" however can be built almost as you want. 然而,“app”几乎可以根据需要构建。 And Builder comes into this. Builder如此。

The typical usage of Builder is providing: Builder的典型用法是提供:

  1. the urls to the static pages (js, css, images, etc) 静态页面的URL(js,css,图像等)
  2. the urls to the dynamic pages that will be brew()ed via brew function, these can be .html or .Rhtml pages 将通过brew函数brew()编辑的动态页面的URL,这些可以是.html.Rhtml页面
  3. the url to redirect your web application (the default, or index, page) 用于重定向Web应用程序的URL(默认或索引页面)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM