1

I have a folder with html files and I want to start a simple HTTP server that serves the requested file.

I have been trying to use Rook, but it asks for an app function that generates the HTML response.

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.

nachocab
  • 13,328
  • 21
  • 91
  • 149
  • Why R to server HTML pages? – CHP Feb 28 '13 at 01:05
  • because I'm generating HTML pages in R and I wanted it to be an automatic process and cross-platform, but I guess I'll have to manually run something like `python -m SimpleHTTPServer` – nachocab Feb 28 '13 at 15:48
  • you may want to look at shiny http://www.rstudio.com/shiny/ – CHP Feb 28 '13 at 16:03

2 Answers2

2

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]

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). In particular if the folder www contains a file index.html then this can be accessed via localhost:23702/custom/static/www/index.html

Other apps for more R-focused operations can be easily included in the builders construction.

Not probably would be cleaner with www moved to root = file.path(getwd(),'www') and a recursive search for all subfolders.

Hope this helps!

Joe
  • 1,455
  • 2
  • 19
  • 36
  • That's great, and I can get a stack HTML page to show using this method. But then how to process GET/POST requests from the client? I am trying something like the code below, but then how to know what URL for the GET/POST requests?# Use a Builder to add a staticApp <- Builder$new( Static$new( urls=c('/app', '/app/library'), root = file.path(getwd(), "..", "..") ), App$new(function(env) { req <- Request$new(env) res <- Response$new() getstr <<- paste(capture.output(str(req$GET()),file=NULL),collapse='\n') res$finish() }) ) – user1905004 Aug 03 '14 at 19:48
1

You should have a look at the example at ?Builder.

You right, Rook expects you to provide an "app". An "app" however can be built almost as you want. And Builder comes into this.

The typical usage of Builder is providing:

  1. the urls to the static pages (js, css, images, etc)
  2. the urls to the dynamic pages that will be brew()ed via brew function, these can be .html or .Rhtml pages
  3. the url to redirect your web application (the default, or index, page)
Michele
  • 8,563
  • 6
  • 45
  • 72