summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPrashant S2019-07-08 17:40:53 +0530
committerGitHub2019-07-08 17:40:53 +0530
commitcf25d4362119b7a2811f696553048d077ed2350f (patch)
tree113e295a0197b228945865ba4ed0314e8298b858
parentdc21381ddc2de7f13b2ed702b55bf70869f80080 (diff)
parenta810ada66224c4634e551dc189b18a1c35ccf724 (diff)
downloadR_on_Cloud_Web_API-cf25d4362119b7a2811f696553048d077ed2350f.tar.gz
R_on_Cloud_Web_API-cf25d4362119b7a2811f696553048d077ed2350f.tar.bz2
R_on_Cloud_Web_API-cf25d4362119b7a2811f696553048d077ed2350f.zip
Merge pull request #1 from prashantsinalkar/master
created api for R
-rw-r--r--README.md30
-rw-r--r--plumber.R70
2 files changed, 99 insertions, 1 deletions
diff --git a/README.md b/README.md
index 6d4af5d..d431377 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,29 @@
-# R_on_Cloud_Web_API \ No newline at end of file
+
+# R_on_Cloud_Web_API
+
+
+**Run on local R console:**
+R version 3.4.4
+
+ **Requirements preinstalled R packages:**
+
+ install.packages("plumber")
+ install.packages("jsonlite")
+ install.packages("readr")
+ install.packages("futile.logger")
+ install.packages("tryCatchLog")
+ ------------------------------------------
+ > library(plumber)
+ > r <- plumb("plumber.R") # Where 'plumber.R' is the location of the file shown above
+ > r$run(port=8001)
+
+
+**Developer:**
+
+ Prashant Sinalkar,
+ FOSSEE, IIT Bombay
+
+
+
+
+
diff --git a/plumber.R b/plumber.R
new file mode 100644
index 0000000..0f091c8
--- /dev/null
+++ b/plumber.R
@@ -0,0 +1,70 @@
+# plumber.R
+library(plumber)
+library(jsonlite)
+library(readr)
+library(futile.logger)
+library(tryCatchLog)
+library(ggplot2)
+
+# creare R directory
+dir.create(file.path("/tmp/R"), showWarnings = FALSE)
+
+#* Echo back the input
+#* @param msg The message to echo
+#* @get /echo
+function(msg="")
+{
+ list(msg = paste0("The message is: '", msg, "'"))
+}
+
+#* @serializer unboxedJSON
+#* @post /rscript
+function(code="", session_id="", R_file_id="")
+{
+ # create session directory for user
+ dir.create(file.path("/tmp/R/", session_id), showWarnings = FALSE)
+ InputFile <- paste("/tmp/R/",session_id,"/", R_file_id,".R", sep="")
+ OutputFile <- paste("/tmp/R/",session_id,"/", R_file_id,".txt", sep="")
+ RunInputFile <- paste("Rscript", InputFile, sep=" ")
+ fileConn<-file(InputFile)
+ Line1 = paste("png('/tmp/R/",session_id,"/", R_file_id,".png')\n", sep="")
+ Line2 = code
+ Line3 = "while (!is.null(dev.list())) dev.off()"
+ writeLines(c(Line1, Line2, Line3), fileConn)
+ close(fileConn)
+ #ro <- system(RunInputFile, intern = TRUE)
+ ro <-robust.system(RunInputFile)
+ ro <- unlist(lapply(ro,function(x) if(identical(x,character(0))) ' ' else x))
+ fileConn<-file(OutputFile)
+ writeLines(paste0(ro), fileConn)
+ close(fileConn)
+ ro <- read_file(OutputFile)
+ if (file.exists(paste("/tmp/R/",session_id,"/",R_file_id,".png", sep="")) == TRUE) {
+ graph_exist <- TRUE
+ } else {
+ graph_exist <- FALSE
+ }
+ r<- list(status = "SUCCESS", code = "200", output = ro, graph_exist = graph_exist)
+ return (r)
+}
+
+#* @serializer contentType list(type='image/png')
+#* @get /file
+function(req, res, session_id="", R_file_id=""){
+ file = paste("/tmp/R/",session_id,"/",R_file_id,".png", sep="")
+ readBin(file,'raw',n = file.info(file)$size)
+}
+
+# function to run R script on system
+robust.system <- function (cmd) {
+ stderrFile = tempfile(pattern="R_robust.system_stderr", fileext=as.character(Sys.getpid()))
+ stdoutFile = tempfile(pattern="R_robust.system_stdout", fileext=as.character(Sys.getpid()))
+
+ retval = list()
+ retval$exitStatus = system(paste0(cmd, " 2> ", shQuote(stderrFile), " > ", shQuote(stdoutFile)), intern = TRUE )
+ retval$stdout = readLines(stdoutFile)
+ retval$stderr = readLines(stderrFile)
+
+ unlink(c(stdoutFile, stderrFile))
+ return(retval)
+}