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

[This article was first published on Rronan » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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

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

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() }
Output

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

Testing

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.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)