Seems like this should be very simple, but I cannot get it to work. I have a long shell command that I would like to span over multiple lines, but escaping the single/double quotes correctly is giving me trouble. I have searched SO and googled this, but cannot find the solution.
Original, working code:
getVersion <- function() {
version <- shell("powershell -Command $path = 'C:/Windows/notepad.exe'; $versioninfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($path); $itemproperties= get-childitem $path; [pscustomobject]#{'File version' = $versioninfo.FileVersion}", intern = TRUE)
version <- gsub(" ", "", version[4])
return(version)
}
Does not work:
getVersion <- function() {
version <- shell(paste0(\""powershell -Command $path = 'C:/Windows/notepad.exe'; $versioninfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($path);\",
\"$itemproperties= get-childitem $path; [pscustomobject]#{'File version' = $versioninfo.FileVersion}"\"), intern = TRUE)
version <- gsub(" ", "", version[4])
return(version)
}
Also tried this:
getVersion <- function() {
version <- paste0('shell(\"powershell -Command $path = "C:/Windows/notepad.exe"; $versioninfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($path);',
'$itemproperties= get-childitem $path; [pscustomobject]#{"File version" = $versioninfo.FileVersion}\", intern = TRUE)')
version <- gsub(" ", "", version[4])
return(version)
}
Why are you trying to escape? You don't actually have double quotes in the string you need to pass, right? So you can just do a paste() with double quoted strings that happen to contain single quotes. Or am I missing something?
For example, would this work?
getVersion <- function() { version <- shell(paste("powershell -Command $path = 'C:/Windows/notepad.exe';",
"$versioninfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($path);",
"$itemproperties= get-childitem $path;",
"[pscustomobject]#{'File version' = $versioninfo.FileVersion}"),
intern = TRUE)
version <- gsub(" ", "", version[4])
return(version) }
Related
I am trying to populate a table in Microsoft Dataverse using an API Patch call through R. The field in Dataverse is a multiline text field with a limit of 4,000 characters, but using a Patch call I can't seem to get text to break across lines. I have other entries in the table that break across lines where they had been brought in by using Access as the front end, which I'm trying to not have to use.
### This is where I create the multiline text
MPC.Comments$Comment = paste(paste("Meeting Date: ", month(Sys.Date()), "/", year(Sys.Date()), sep = ""),
paste("Plan Cost: $", round(MPC.Comments$plantot, 0), sep = ""),
paste("%PC Remain: ", round((round(MPC.Comments$plantot, 0) - round(MPC.Comments$acttot, 0) - round(MPC.Comments$comsub, 0))/round(MPC.Comments$plantot, 0)*100,1), "%", sep = ""),
paste("Date Next PM Gate: ", MPC.Comments$eventdate, sep = ""),
paste("Est. Monthly Spend: $", MPC.Comments$esttot, sep = ""),
paste("Status Update: ", MPC.Comments$MPCStatus, sep = ""),
paste("Action Items: ", MPC.Comments$MPCAction, sep = ""),
sep = " <\\r\\n>")
### This is where I try to push it to the database
if (nrow(MPC.Comments) > 0) {
for (i in 1:nrow(MPC.Comments)) {
### Set POST parameters
MPC.Comments.Temp = list(
crfd0_projno = MPC.Comments$ProjNo[i],
crfd0_fyp = MPC.Comments$FYP[i],
crfd0_comment = MPC.Comments$Comment[i],
statuscode = 1)
POST("https://[REDACTED].crm4.dynamics.com/api/data/v9.2/crfd0_mpccommentses",
add_headers(Authorization = paste("Bearer", API.AuthKey, sep = " ")),
body = MPC.Comments.Temp,
encode = "json",
verbose())}}
No matter what special character I put in as my separator, it either comes through as nothing (if not escaped like \n) or as the physical string (if escaped like \\n). I have tried \n, \r\n, , <br>, and \u000a. Clearly there is a piece missing here that the data needs to be sent as something other than a string with a carriage return character. Does anyone know how to do this?
I am trying to call a very simple script in Matlab from RStudio. However, whenever I run the following code, without getting any error, it will return 0 to me. Would you please let me know how I can call Matlab scripts in R and get their outputs?
run_matlab_script("C:/Users/XXX/Desktop/Sum.m", verbose = TRUE, desktop = FALSE, splash = FALSE,
display = TRUE, wait = TRUE, single_thread = FALSE)
Note that to use the above function, I am using "matlabr" package in r. Moreover, my simple script in Matlab includes the below code:
b=1+2
Thanks in advance!
matlab::run_matlab_script uses system to call matlab. As of today, that function (current commit is c01d310) looks like:
run_matlab_script = function(
fname,
verbose = TRUE,
desktop = FALSE,
splash = FALSE,
display = FALSE,
wait = TRUE,
single_thread = FALSE,
...){
stopifnot(file.exists(fname))
matcmd = get_matlab(
desktop = desktop,
splash = splash,
display = display,
wait = wait,
single_thread = single_thread)
cmd = paste0(' "', "try, run('", fname, "'); ",
"catch err, disp(err.message); ",
"exit(1); end; exit(0);", '"')
cmd = paste0(matcmd, cmd)
if (verbose) {
message("Command run is:")
message(cmd)
}
x <- system(cmd, wait = wait, ...)
return(x)
}
Noteworthy (to me) is that run_matlab_script includes ... in its formals, and passes that unchanged to system. In fact, its help documentation specifically says that is what it does:
#' #param ... Options passed to \code{\link{system}}
Because of this, we can try to capture its output by looking at system. From ?system,
intern: a logical (not 'NA') which indicates whether to capture the
output of the command as an R character vector.
which suggests that if you change your call to
ret <- run_matlab_script("C:/Users/XXX/Desktop/Sum.m", verbose = TRUE, desktop = FALSE,
splash = FALSE, display = TRUE, wait = TRUE, single_thread = FALSE,
intern = TRUE)
you will get its output in out.
My R code is trying to open a RDS file in a for loop as follows:
for(i in 1:run_loops){
source("./scripts/load_data.R")
model <- readRDS(file=paste(model_directory,"/",modelname,".Rds", sep="")) #STOPS-HERE!!!
source("./scripts/prediction.R")
}
R stops when there is no model file.
How do I get it to move to the next iteration instead of stopping?
P.S. modelname variable changes each time load_data.R is sourced.
This should do the trick:
for(i in 1:run_loops) {
tryCatch(
expr = {
source("./scripts/load_data.R")
model <-
readRDS(file = paste(model_directory, "/", modelname, ".Rds", sep = "")) #STOPS-HERE!!!
source("./scripts/prediction.R")
},
error = function(e) {
print(paste0(i, ' not done'))
}
)
}
You can use file.exists
file_name <- paste0(model_directory,"/",modelname,".Rds")
if(file.exists(file_name)) {
#do something
} else {
#do something else
}
I am trying to make an R scripts generator. The problem is that the new files are presented as text files and not as R scripts. Is there any way to present them as R scripts?
Expected icon:
Result icon:
require("here", character.only = TRUE)
files.to.create <- c("main.R",
"functions.R",
"explore.R",
"initialize.R",
"load_data.R",
"build.R",
"analyze.R",
"build_ppt.R",
"prepare_markdown.R",
"markdown_report.Rmd")
try.to.make.files <- function(file.name, path){
if( .Platform$OS.type == "unix" ) {
new.file.name <- paste(path,"/", file.name, sep = "")
}
else {
new.file.name <- paste(path,"\\", file.name, sep = "")
}
cat(new.file.name,"\n")
unlink(new.file.name)
success <- file.create(new.file.name)
print(success)
return (TRUE)
}
invisible( lapply( files.to.create,
try.to.make.files,
here("src")))
UPDATE
Well it seems that if the file is empty Ubuntu handles it as empty text file, forcing it to show the txt icon and not the R icon. Filling the file solves the problem.
I am trying to create a web application using R and Rook.
I saw this example http://www.road2stat.com/cn/r/rook.html and I have been able to replicate it in my command line (Calling it from inside R). But I want multiple users to be able to connect to the app at the same-time so I want to deploy it like Jeff said here - http://jeffreyhorner.tumblr.com
I have been able to replicate most of the examples that came with Rook package like Jeff stated on his blog. These include:
summary.r
rnorm
RookTestApp
The problem I am having now is that I cant make the former example work (That is -www.road2stat) It gives me an error message every time I try to upload a file to it. This does not happen when I use the command line.
My error message is like so:
Internal Server Error The server encountered an internal error or
misconfiguration and was unable to complete your request.
This is my path to deploy my app
<Location /test/Visbin>
SetHandler r-handler
RFileEval /usr/lib/R/library/Rook/exampleApps/visbin.R:Rook::Server$call(newapp)
</Location>
This is the app I am trying to replicate
newapp = function(env) {
req = Rook::Request$new(env)
res = Rook::Response$new()
res$write('Choose a Binary file to Train:\n')
res$write('<form method="POST" enctype="multipart/form-data">\n')
res$write('<input type="file" name="data">\n')
res$write('xdim:\n')
res$write('<form method="POST">\n')
res$write('<input type="text" name="xdim" value="12">\n')
res$write('ydim:\n')
res$write('<form method="POST">\n')
res$write('<input type="text" name="ydim" value="25">\n')
res$write('ncolors:\n')
res$write('<form method="POST">\n')
res$write('<input type="text" name="ncolors" value="8">\n')
res$write('<input type="submit" name="Go!">\n</form>\n<br>')
myNormalize = function (target) {
return((target - min(target))/(max(target) - min(target)))
}
if (!is.null(req$POST())) {
data = req$POST()[["data"]]
hash = digest(data$tempfile, algo = "md5", file = TRUE)
destFile = file(data$tempfile, "rb")
k = floor((file.info(data$tempfile)$size/16)) - 2
doneFile = readBin(con = destFile, what = "raw", n = 2 * 8 * k)
close(destFile)
tmpFile0 = rbind(doneFile[seq(1, (2 * 8 * k) - 1, 2)], doneFile[seq(2, (2 * 8 * k), 2)])
tmpFile1 = paste(tmpFile0[1, ], tmpFile0[2, ], sep = "")
initMat = matrix(strtoi(tmpFile1, 16L), ncol = 8, byrow = TRUE)
normMat = myNormalize(initMat)
trainedSOM = kohonen::som(normMat, grid = somgrid(xdim = req$POST()[["xdim"]], ydim = req$POST()[["ydim"]], "hexagonal"))
png(paste("/tmp/", hash, ".png", sep = ""))
plot(trainedSOM, type = "dist.neighbours", palette.name = rainbow, ncolors = as.numeric(req$POST()[["ncolors"]]), main = "")
dev.off()
res$write(paste("<img src='", s$full_url("pic"), "/", hash, ".png'", " />", sep = ""))
}
res$finish()
}
And this is my command line instruction:
s = Rhttpd$new()
s$add(app = newapp, name = "visbin")
s$add(app = File$new("/tmp"), name = "pic")
s$start()
s$browse("visbin")
Can anyone please point me in the right direction or direct me to resources that will assist me in doing it.
P.S. I am using a fedora 15 and I have Rapache-1.2.0 installed.