How to create a data frame from text submitted in a textarea with FastRWeb

March 8, 2012

(This article was first published on Rronan » R, and kindly contributed to R-bloggers)

In this article, I show you how to create a data.frame from a text submitted in a textarea field with FastRWeb.

  • FastRWeb installed
  • Knowledge of webforms
  • ?read.table
  • Experience in HTML5

This example needs two scripts. The first one contains the webform. I wrote a FastRWeb script in order to work in /var/FastRWeb/web.R/. This form could have been a static HTML page. Paste the code below in /var/FastRWeb/web.R/tablesubmit.R.

run <- function() {
  out("Submit a Table",

Submit a Table

", "

This is an example showing how to read a text posted in a ", "textarea and create a data frame from it.

", sep = "", eol = "") out("
", sep = "", eol = "") out("

Paste the table content in the text area below.

") out("") out("
") out("") done() }

To retrieve the the submitted data, read them and create your data.frame, you’ll respectively use the functions parse.multipart(), textConnection() and read.table(). These last two are installed with R base. parse.multipart() is available if you have installed la version 1.1 de FastRWeb.

Paste the text below in /var/FastRWeb/web.R/textareatodf.R.

run <- function(MyTable) {
  # HTML
  out("Data Frame",

textarea ", "to data.frame

", sep = "", eol = "") # Retrieve the data in a 'multipart/form-data' form if (grep("^multipart", request$c.type)) { pars <- parse.multipart() } else { } # Create the data frame try(MyDF <- read.table(file = textConnection(pars$MyTable), header = FALSE, sep = "\t")) # HTML output if (!exists("MyDF")) { out("

Something went wrong…

", "

Make sure your table has the same number of elements for each ", "row.

", sep = "", eol = "") } else { out("

Your data frame

This is your data frame.

") oprint(MyDF) oprint(sapply(MyDF, class)) } out("") done() }

To run this script, go here : http://localhost/cgi-bin/R/tablesubmit


Copy and paste the data from à spreadsheet (either LibreOffice Calc or Excel). The non-ASCII characters are handled correctly if the users did not change their browser encoding. You may want to use the options read.table() such as sep, col.names or colClasses.

Creative Commons License

To leave a comment for the author, please follow the link and comment on their blog: Rronan » R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , , , ,

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)