I'm running the GCMS software AMDIS inside an R script to analyze GCMS data. The basic syntax is:
system("AMDIS_PATH GCMS_FILE_PATH /S /GC /GE /E"
With "/S /GC /GE /E" referring to functionalities within the AMDIS program.
I want to make the code 'fool-proof', so I'm allowing paths with "/" instead of "\\". And I'm also allowing spaces in the paths, escaping them with shQuote(..., type = "cmd")
Which means the full code looks like
system(paste(shQuote(gsub("/", "\\", AMDIS_PATH, fixed = T), type = 'cmd'), shQuote(gsub("/", "\\", GCMS_FILE_PATH, fixed = T), type = 'cmd'), "/S /GD /GS /E"))
This code does not work (output = 65535), but there's a catch:
If I remove the shQuotes from my GCMS_FILE_PATH. Then everything works like it should (so long as the FILE_PATH does not contain any spaces).
So executing the code like this does work:
system(paste(shQuote(gsub("/", "\\", AMDIS_PATH, fixed = T), type = 'cmd'), gsub("/", "\\", GCMS_FILE_PATH, fixed = T), "/S /GD /GS /E"))
Any idea why shQuote works fine for the AMDIS_PATH, but not for the GCMS_FILE_PATH? And how I can run my code and still escape spaces in the GCMS_FILE_PATH?
Related
I need to dynamically make a call to an API using the following format:
auth_secret <- paste0("Bearer ", secret)
headers = c(
`Authorization` = auth_secret,
`Notion-Version` = '2022-02-22',
`Content-Type` = 'application/json' )
res <- httr::PATCH(url = paste0('https://api.notion.com/v1/pages/', id),
httr::add_headers(.headers = headers),
body = payload,
encode = "json")
d <- httr::content(res)
This payload works:
payload <- "{\"properties\":{\"Project\":{\"relation\":[{\"id\":\"1d148a9e-783d-47a7-b3e8-2d9c34210355\"}]}}}"
But if I want to create it dynamically, using a paste0 (so it is inside of a function), I get some backslashes added before and after:
payload <- paste0('"{\"properties\":{\"',property_name,'\":{\"relation\":[{\"id\":\"',value,'\"}]}}}"')
print(payload)
"\"{\"properties\":{\"%7CAK%5E\":{\"relation\":[{\"id\":\"8cb9519e72ca4bbe9e0448807acb8e10\"}]}}}\""
I presume this is due to some weird escape character being added but have run out of ideas. I've added two \ and have gotten same issue. Call fails as JSON is not passed correctly. Is there a workaround?
This is obviously something to do with the fact that paste0 is escaping even the double quotes you do not want to escape.
No doubt someone is aware of the details. However, when I get weird stuff with paste0, I just use sprintf, which seems to work in this case:
property_name = "Projects"
value = "1d148a9e-783d-47a7-b3e8-2d9c34210355"
payload <- sprintf(
"{\"properties\":{\"%s\":{\"relation\":[{\"id\":\"%s\"}]}}}",
property_name, value
)
print(payload)
# [1] "{\"properties\":{\"Projects\":{\"relation\":[{\"id\":\"1d148a9e-783d-47a7-b3e8-2d9c34210355\"}]}}}"
I'd like to have no images for the flipbox, how can I do it? Tried this (and also setting to NULL):
flipBox(
id = 1,
main_img = "",
header_img = "",
...
but instead it gives me this strange image.
I had the same problem and checked the source code of the function. If you copy it into a R script file there is the following code in line 13/14:
shiny::tags$img(class = "avatar",
src = main_img,
alt = "Avatar")
Since you cannot control this behavior by a flipBox() argument, I defined my own function flippBox and deleted the alt = "Avatar" argument. I do not see the necessity for this strange pill anyways..
I'm trying to combine some stings to one. In the end this string should be generated:
//*[#id="coll276"]
So my inner part of the string is an vector: tag <- 'coll276'
I already used the paste() method like this:
paste('//*[#id="',tag,'"]', sep = "")
But my result looks like following: //*[#id=\"coll276\"]
I don't why R is putting some \ into my string, but how can I fix this problem?
Thanks a lot!
tldr: Don't worry about them, they're not really there. It's just something added by print
Those \ are escape characters that tell R to ignore the special properties of the characters that follow them. Look at the output of your paste function:
paste('//*[#id="',tag,'"]', sep = "")
[1] "//*[#id=\"coll276\"]"
You'll see that the output, since it is a string, is enclosed in double quotes "". Normally, the double quotes inside your string would break the string up into two strings with bare code in the middle:
"//*[#id\" coll276 "]"
To prevent this, R "escapes" the quotes in your string so they don't do this. This is just a visual effect. If you write your string to a file, you'll see that those escaping \ aren't actually there:
write(paste('//*[#id="',tag,'"]', sep = ""), 'out.txt')
This is what is in the file:
//*[#id="coll276"]
You can use cat to print the exact value of the string to the console (Thanks #LukeC):
cat(paste('//*[#id="',tag,'"]', sep = ""))
//*[#id="coll276"]
Or use single quotes (if possible):
paste('//*[#id=\'',tag,'\']', sep = "")
[1] "//*[#id='coll276']"
I want to create an object in R, which will contain one string, with a few variables (in my case it is file path). When I try to use paste to concatenate the file paths I can see only one last variable instead of all variables in one string. I use next code:
for(i in seq_len(nrow(samples))) {
lib = samples$conditions[i]
txtFile = file.path(lib, "hits.txt")
testfiles = paste(txtFile, sep = ',')
}
print(testfiles)
and get something like
cond/hits.txt,
instead of
cond/hits.txt,cond1/hits.txt,cond2/hits.txt and so on
Thank you very much for help
Is there a way to make the R mcmcplot() function not open a browser when it's called? I need to run my R code on a cluster and if mcmcplot() tries to open a browser, it will puke.
Can the output be dumped to a file maybe?
That function writes everything to a file and also opens it in a browser. If you dont want to open the browser I would recommend editing the function to pass whether or not you want to open in a browser as an argument. You can retreive the function by just typing its name without any parenthesis.
mcmcplot
then copy that output to a editor and at the beginning change the name of the function and add teh argument:
mcmcplotnew=function (mcmcout, parms = NULL, regex = NULL, random = NULL,
leaf.marker = "[\\[_]", dir = tempdir(), filename = "MCMCoutput",
extension = "html", title = NULL, heading = title, col = NULL,
lty = 1, xlim = NULL, ylim = NULL, style = c("gray", "plain"),
greek = FALSE,ShouldIPlotinbrowser=T) #new argument here
then there are much more parts of the function
then at the end there is
cat("\r", rep(" ", getOption("width")), "\r", sep = "")
cat("\n</div>\n</div>\n", file = htmlfile, append = TRUE)
.html.end(htmlfile)
full.name.path <- paste("file://", htmlfile, sep = "")
browseURL(full.name.path)
invisible(full.name.path)
}
Where you have the browsURL line, make it something like:
if(ShouldIPlotinbrowser) { browseURL(full.name.path) }
Then initialize that function before you run it with:
mcmcplotnew(whatever, usual, arguments,then,ShouldIPlotinbrowser=F)
Looking at the source, it seems not. There is an unconditional call to browseURL() there. Maybe by making a dummy version of that function which does nothing in your global namespace, it's effect can be avoided.
browseURL <- identity
This may break other browser activity as well, so after the mcmcplot calls, you may want to
rm(browseURL)
Alternatively, copy all the code from mcmcplot except for the browseURL line and use that function instead.