From cf1750ccd07a511ffa04c22004d57044d0741aef Mon Sep 17 00:00:00 2001 From: prashantsinalkar Date: Fri, 28 Jun 2019 21:33:27 +0530 Subject: added plumber file --- plumber.R | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 plumber.R (limited to 'plumber.R') diff --git a/plumber.R b/plumber.R new file mode 100644 index 0000000..580c018 --- /dev/null +++ b/plumber.R @@ -0,0 +1,37 @@ +# plumber.R +library(plumber) +library(jsonlite) +library(readr) +library(futile.logger) +library(tryCatchLog) + +#* Echo back the input +#* @param msg The message to echo +#* @get /echo +function(msg=""){ + list(msg = paste0("The message is: '", msg, "'")) +} + +#* Plot a histogram +#* @png +#* @get /plot +function(){ + rand <- rnorm(100) + hist(rand) +} + +#* @serializer unboxedJSON +#* @post /rscript +function(code=""){ + fileConn<-file("input.R") + writeLines(code, fileConn) + close(fileConn) + ro <- system("Rscript input.R", intern = TRUE) + fileConn<-file("output.txt") + writeLines(ro, fileConn) + close(fileConn) + ro <- read_file("output.txt") + r<- list(status = "SUCCESS", code = "200", output = ro) + return (r) +} + -- cgit From 58f7c7719aa8c64ecf84998207756af6b6a4a7c2 Mon Sep 17 00:00:00 2001 From: prashantsinalkar Date: Sat, 29 Jun 2019 15:42:49 +0530 Subject: added dynamic file names and formated the code --- plumber.R | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) (limited to 'plumber.R') diff --git a/plumber.R b/plumber.R index 580c018..de033b7 100644 --- a/plumber.R +++ b/plumber.R @@ -4,34 +4,41 @@ library(jsonlite) library(readr) library(futile.logger) library(tryCatchLog) +library(ggplot2) #* Echo back the input #* @param msg The message to echo #* @get /echo -function(msg=""){ - list(msg = paste0("The message is: '", msg, "'")) +function(msg="") +{ + list(msg = paste0("The message is: '", msg, "'")) } #* Plot a histogram #* @png #* @get /plot -function(){ - rand <- rnorm(100) - hist(rand) +function() +{ + rand <- rnorm(100) + hist(rand) } #* @serializer unboxedJSON #* @post /rscript -function(code=""){ - fileConn<-file("input.R") - writeLines(code, fileConn) - close(fileConn) - ro <- system("Rscript input.R", intern = TRUE) - fileConn<-file("output.txt") - writeLines(ro, fileConn) - close(fileConn) - ro <- read_file("output.txt") - r<- list(status = "SUCCESS", code = "200", output = ro) - return (r) +function(code="", user_id="") +{ + InputFile <- paste("/tmp/",user_id,"/",user_id,".R", sep="") + OutputFile <- paste("/tmp/",user_id,"/",user_id,".txt", sep="") + RunInputFile <- paste("Rscript", InputFile, sep=" ") + fileConn<-file(InputFile) + writeLines(code, fileConn) + close(fileConn) + ro <- system(RunInputFile, intern = TRUE) + fileConn<-file(OutputFile) + writeLines(ro, fileConn) + close(fileConn) + ro <- read_file(OutputFile) + r<- list(status = "SUCCESS", code = "200", output = ro) + return (r) } -- cgit From 0c28dadc43107d3b06c1dcb2b7a8121520d64f8a Mon Sep 17 00:00:00 2001 From: prashantsinalkar Date: Wed, 3 Jul 2019 17:55:31 +0530 Subject: added session for user --- plumber.R | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'plumber.R') diff --git a/plumber.R b/plumber.R index de033b7..e5d47b3 100644 --- a/plumber.R +++ b/plumber.R @@ -25,10 +25,11 @@ function() #* @serializer unboxedJSON #* @post /rscript -function(code="", user_id="") +function(code="", session_id="") { - InputFile <- paste("/tmp/",user_id,"/",user_id,".R", sep="") - OutputFile <- paste("/tmp/",user_id,"/",user_id,".txt", sep="") + dir.create(file.path("/tmp/", session_id), showWarnings = FALSE) + InputFile <- paste("/tmp/",session_id,"/",session_id,".R", sep="") + OutputFile <- paste("/tmp/",session_id,"/",session_id,".txt", sep="") RunInputFile <- paste("Rscript", InputFile, sep=" ") fileConn<-file(InputFile) writeLines(code, fileConn) -- cgit From 70b9ea3f66a7256695ba669cac8d59b515915832 Mon Sep 17 00:00:00 2001 From: prashantsinalkar Date: Thu, 4 Jul 2019 16:43:43 +0530 Subject: added function for system call execution --- plumber.R | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'plumber.R') diff --git a/plumber.R b/plumber.R index e5d47b3..0226024 100644 --- a/plumber.R +++ b/plumber.R @@ -34,12 +34,27 @@ function(code="", session_id="") fileConn<-file(InputFile) writeLines(code, fileConn) close(fileConn) - ro <- system(RunInputFile, intern = TRUE) + #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(ro, fileConn) + writeLines(paste0(ro), fileConn) close(fileConn) ro <- read_file(OutputFile) r<- list(status = "SUCCESS", code = "200", output = ro) return (r) } + +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) +} -- cgit From cf4f98aa562af951f539a74df47ef332c9186744 Mon Sep 17 00:00:00 2001 From: prashantsinalkar Date: Fri, 5 Jul 2019 18:11:09 +0530 Subject: added save path for plot --- plumber.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'plumber.R') diff --git a/plumber.R b/plumber.R index 0226024..39c6ca4 100644 --- a/plumber.R +++ b/plumber.R @@ -32,7 +32,10 @@ function(code="", session_id="") OutputFile <- paste("/tmp/",session_id,"/",session_id,".txt", sep="") RunInputFile <- paste("Rscript", InputFile, sep=" ") fileConn<-file(InputFile) - writeLines(code, fileConn) + Line1 = paste("png('/tmp/",session_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) -- cgit From a810ada66224c4634e551dc189b18a1c35ccf724 Mon Sep 17 00:00:00 2001 From: prashantsinalkar Date: Mon, 8 Jul 2019 17:38:51 +0530 Subject: added plot/graph feature --- plumber.R | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) (limited to 'plumber.R') diff --git a/plumber.R b/plumber.R index 39c6ca4..0f091c8 100644 --- a/plumber.R +++ b/plumber.R @@ -6,6 +6,9 @@ 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 @@ -14,25 +17,17 @@ function(msg="") list(msg = paste0("The message is: '", msg, "'")) } -#* Plot a histogram -#* @png -#* @get /plot -function() -{ - rand <- rnorm(100) - hist(rand) -} - #* @serializer unboxedJSON #* @post /rscript -function(code="", session_id="") +function(code="", session_id="", R_file_id="") { - dir.create(file.path("/tmp/", session_id), showWarnings = FALSE) - InputFile <- paste("/tmp/",session_id,"/",session_id,".R", sep="") - OutputFile <- paste("/tmp/",session_id,"/",session_id,".txt", sep="") + # 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/",session_id,".png')\n", sep="") + 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) @@ -44,11 +39,23 @@ function(code="", session_id="") writeLines(paste0(ro), fileConn) close(fileConn) ro <- read_file(OutputFile) - r<- list(status = "SUCCESS", code = "200", output = ro) + 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())) -- cgit