Hi Stack Overflow community, thank you so much in advance for any help on this issue.
I'm interested in writing a non-interactive Rscript that designates variables that are read in from the command line. For example:
Rscript ./script {1} {2}
The {1} is an argument I pass to the script and the {2} is an argument corresponding to the full path that points to a text file composed of one column, filled with numbers and names.
I wrote a small Rscript whose function is to read in the text file and delete the rows that contain a particular string. I want the string to correspond to a variable I designate at the beginning of the script and provide on the command line. So far, my script reads:
args = commandArgs(trailingOnly=TRUE)
POP = args[1]
FREQ = args[2]
# read the file in
POP.freq <- paste0("FREQ")
# write a function to remove the rows that hold the string "POP"
clean_names <- function(x, ...) {
POP.freq[!POP.freq$POP == "POP",]
}
The problem is that in the function where I designate the string to be removed: "POP", R reads this literally as "POP" and not whatever the argument is I am trying to pass it (usually the name of a country e.g. Germany). In bash, the $ designates the variable but I am not sure what the equivalent is in R. How can I designate that POP is a variable within the string but also let R know that it is a string I am trying to identify.
I would appreciate any advice on this matter!
Related
In an R script, I assign a name to some data. The name depends on parameters. I do this using
number<-1
assign(paste("variable", as.character(number), sep=""),2)
The above accomplices the same as variable1<-2. Now I want to save the result for later
save(?,file=paste("variable",as.character(number),".RData",sep=""))
What code can go in the ? slot where it should say variable1 except I need to construct this name using paste or some similar technique. Simply putting get(paste("variable",as.character(number),".RData",sep="")) does not work.
save can also use list as parameter. According to ?save
list - A character vector containing the names of objects to be saved.
Thus, we specify the object name as a string (paste0('variable', number)) for the list argument and file as the one used by OP (or make it more concise with paste0 (as.character is not necessary as integer/numeric gets automatically convert to type character in paste
save(list = paste0('variable', number),
file = paste0("variable", number, ".RData"))
Check for the file created in the working directory
list.files(getwd(), pattern = '\\.RData$')
#[1] "variable1.RData"
I know there are ways to extract all arguments to a function using, for example, rlang::fn_fmls. But is it possible to extract the description of one of these arguments from the package documentation?
For example, what if I wanted to extract the description of the na.rm argument to base::sum()?
I'm imagining something like:
get_argument_description(fn = 'base::sum', arg = 'na.rm')
Return:
"logical. Should missing values (including NaN) be removed?"
You could try to read the associated help file for that function, and grep the line where \item{argument}. However, multi-line help texts are allowed, if the next line does not start with a \ you would want to grab that too.
This answer shows a way to acess the file, then it is just a matter of grabbing the correct line(s). I also want to highlight a different function in tools,
tools:::.Rd_get_text()
Which almost gets you where you want, (if you find the correct line)
library(tools)
db = Rd_db("base")
tools:::.Rd_get_text(x = db[["sum.Rd"]])[21] # 21 is the line you want here
[1] " na.rm: logical. Should missing values (including 'NaN') be removed?"
I am running an R script via the command line (which is initiated by a Jython script). I want to pass a file location to the R script however if the file location has spaces in it, the string gets split at those spaces to make a list.
I can pass an argument and run the R script in Jython using:
cmd = '"C:\\Program Files\\R\\R-3.5.1\\bin\\Rscript"'+" "+'"C:\\filelocation"'+" "+"C:/filelocation"
Runtime.getRuntime().exec(cmd)
In R I then use the following to get the passed argument:
args <- commandArgs(trailingOnly = TRUE)
The problem I having is that if there is a space in the name of the folder, the string gets split at the spaces. Therefore instead of args being one string for the file location it becomes a list split at every space. I can fix this problem in R using the following (to make the file location one string):
location <- paste(args, collapse = " ")
is there a better solution to this problem?
thanks for your time,
Mike
I want to read multiple images which are in a folder in Scilab. My code is:
I1=dir('G:\SCI\FRAME\*.jpg');
n=length(I1);
disp(n);
for i=1:n
I2=strcat('G:\SCI\FRAME\',I1(i).name);
I=imread(I2);
figure(),imshow(I);
end
But it does not work. It shows error "invalid index".
There are two mistakes to correct:
1.) length gives the number of characters (=length) of a string, but you want to get the number of elements (=size) in a vector (the filenames), hence you should use size.
2.) I1 is a list structure returned by dir. You can extract its content with the . operator, e.g. I1.name, I1.date, I1.bytes, I1.isdir. Type these into the consol, to see the contents! Since I1.name already contains the fullpath+filename+extension as a string vector, you don't have to construct it with strcat. Anyway if you want to "glue" 2 strings together, it's easier to use + e.g. S="fisrst_string"+"second_string".
So the revised code:
I1=dir('G:\SCI\FRAME\*.jpg');
n=size(I1.name,"*"); //size of the I1.name vector
disp(n);
for i=1:n
I=imread(I1.name(i)); //I1.name is a string vector
figure();
imshow(I);
end
I have a little problem in reading a file in R. In particular, I run a script that load a file, say X, which stores a string per each line. There are string with special characters like, ' and therefore I get some errors.
I run the script by command line as follows
Rscript.exe MyScript.R "C:\X.txt"
The content of file X is, for instance:
I'll win a prize
I'll shutdown my pc
The MyScript.R script initially loads the file X.txt as follows
args <- commandArgs(TRUE)
args <- read.table(args[1], sep="\n")
and then uses it as follows:
print(nrow(args))
The previous line returns 0. However, if I remove the ' character from the two lines in file X.txt then everything works fine (i.e., and the returned length is 2).
Any solution to handle this tricky input?
read.table is meant for reading structured data, ie data that is in the form of multiple fields per row. If you just want to read a bunch of strings, use readLines.
args <- readLines(args[1])