2

I am building an application with RApache and my code in R is receiving POST data. One of the post datas is POST$f which is a string- say "sin(x)". Is there any way to put this into the plot function successfully?

Thanks!

Nick Trileski
  • 151
  • 1
  • 2
  • 8

1 Answers1

2
fun <- "sin(x)"
plot(function(x) eval(parse(text=fun)))

But it is not something I would recommend. eval(parse(...)) is already dangerous, and then to do it with arbitrary user input from a web site is just a gaping security hole.

# PLOTTING THIS FUNCTION AS ABOVE WILL DELETE EVERYTHING IN YOUR GLOBAL WORKSPACE
fun <- "{rm(list=ls(pos=1),pos=1); x}"
# DON'T SAY I DIDN'T WARN YOU!

Or even using system() to do even more bad things.

Brian Diggs
  • 57,757
  • 13
  • 166
  • 188
  • 1
    For security issues: [sandboxR](https://github.com/daroczig/sandboxR) might be handy. E.g. your function above would not harm the system on my demo page: http://sandboxr.no-ip.org/ – daroczig Jun 05 '12 at 21:04