Extract function argument description from R package - r

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?"

Related

Can I skip until I reach a certain line when iterating through text in RStudio?

I am reading text data using the read.delim() -&- read.delim2() methods. They accept a skip argument, but it works with number of lines, (i.e. it skips 2,3,4,100 the line you pass into it).
I am using the methods...
read.delim()
read.delim2()
...to read text data. The methods above are able to skip lines, consequently; the methods have a skip parameter, and the parameter accepts an array of line-numbers as an argument. All line-numbers passed into the skip-parameter are skipped by the reader methods (i.e. the lines are not read by the reader methods).
I want to iterate through a body of text, skipping every line until I get to a specific line. Does anyone know how this can be done?
You cannot do that in base R functions, and I don't know of a package that directly provides that. However, here are two ways to get the effect.
First, a file named file.txt:
I want to skip this
and this too
Absolute Irradiance
I need this line
txt <- readLines("file.txt")
txt[cumany(grepl("Absolute Irradiance", txt))]
# [1] "Absolute Irradiance" "I need this line"
If you don't want the "Irradiance" line but want everything after it, then add [-1] to remove the first of the returned lines:
txt[cumany(grepl("Absolute Irradiance", txt))][-1]
# [1] "I need this line"
If the file is relatively large and you do not want to read all of it into R, then
system2("sed", c("-ne", "'/Absolute Irradiance/,$p'", "file.txt"), stdout = TRUE)
# [1] "Absolute Irradiance" "I need this line"
This second technique is really not that great ... it might be better to run that from file.txt into a second (temp) file, then just readLines("tempfile.txt") directly.

How to designate a variable within a string quote in R?

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!

Two PASTE functions in a character vector

attach.files = c(paste("/users/joesmith/nosection_", currentDate,".csv",sep=""),
paste("/users/joesmith/withsection_", currentDate,".csv",sep=""))
Basically, if I did it like
c("nosection_051418.csv", "withsection_051418.csv")
And I did that manually it would work fine but since I'm automating this to run every day I can't do that.
I'm trying to attach files in an automated email but when I structure it like this, it doesn't work. How can I recreate this so that the character vector accepts it?
I thought your example implied the need for "parallel" inputs to the path stem, the first portion of the file name, and the date portions of those full paths. Consider this illustration of using a 2 item vector and a one item vector (produced by Sys.Date, replacing your "currentdate") to populate the %s positions in that sprintf string (suggested by #Gregor):
sprintf("/users/joesmith/%s_%s.csv", c("nosection", "withsection"), Sys.Date() )
[1] "/users/joesmith/nosection_2018-05-14.csv" "/users/joesmith/withsection_2018-05-14.csv"

Combining many vectors into one larger vector (in an automated way)

I have a list of identifiers as follows:
url_num <- c('85054655', '85023543', '85001177', '84988480', '84978776', '84952756', '84940316', '84916976', '84901819', '84884081', '84862066', '84848942', '84820189', '84814935', '84808144')
And from each of these I'm creating a unique variable:
for (id in url_num){
assign(paste('test_', id, sep = ""), FUNCTION GOES HERE)
}
This leaves me with my variables which are:
test_8505465, test_85023543, etc, etc
Each of them hold the correct output from the function (I've checked), however my next step is to combine them into one big vector which holds all of these created variables as a seperate element in the vector. This is easy enough via:
c(test_85054655,test_85023543,test_85001177,test_84988480,test_84978776,test_84952756,test_84940316,test_84916976,test_84901819,test_84884081,test_84862066,test_84848942,test_84820189,test_84814935,test_84808144)
However, as I update the original 'url_num' vector with new identifiers, I'd also have to come down to the above chunk and update this too!
Surely there's a more automated way I can setup the above chunk?
Maybe some sort of concat() function in the original for-loop which just adds each created variable straight into an empty vector right then and there?
So far I've just been trying to list all the variable names and somehow get the output to be in an acceptable format to get thrown straight into the c() function.
for (id in url_num){
cat(as.name(paste('test_', id, ",", sep = "")))
}
...which results in:
test_85054655,test_85023543,test_85001177,test_84988480,test_84978776,test_84952756,test_84940316,test_84916976,test_84901819,test_84884081,test_84862066,test_84848942,test_84820189,test_84814935,test_84808144,
This is close to the output I'm looking for but because it's using the cat() function it's essentially a print statement and its output can't really get put anywhere. Not to mention I feel like this method I've attempted is wrong to begin with and there must be something simpler I'm missing.
Thanks in advance for any help you guys can give me!
Troy

How to add a space to an object name in R

Piston_Rings<-diameter[1:25,]
I want my quality control graph NOT to have the underscore in the object name.
At the moment there is an underscore (not a hyphen) in that object name. It is possible to construct objects whose names have spaces in them but in order to access them you will then always need to use backticks in order to get the interpreter to understand what you want:
> `Piston Rings` <- list(1,2)
> `Piston Rings`[[1]]
[1] 1
> `Piston Rings`[[2]]
[1] 2
The problem you incur is cluttering up your code, at least relative to obeying the usual conventions in R where a space is a token-ending marker to the parser. Hyphens (at least short-hyphens) are actually minus signs.
If on the other hand you only want to use a modified version of a name that contains an underscore as the title for a graph, then try something like this:
Piston_Rings <- list() # just for testing purposes so there will be an object.
plot( 1:10,10:1, main = sub("_", " ", quote(Piston_Rings)) )
#BondedDust's answer is correct, but (guessing, since you haven't been very specific) a simpler way to get what you want is just to specify xlab or ylab arguments to the plot() function. Let's say you have variables stuff (x) and Piston_Rings (y). If you just
plot(stuff,Piston_Rings)
then the plot will have "Piston_Rings" as the y-axis label. But if you
plot(stuff,Piston_Rings,ylab="Piston Rings")
you'll get the label you want. You can also include lots more information this way:
plot(stuff,Piston_Rings,
xlab="Important stuff (really)",
ylab="Piston Rings (number per segment)")
See ?plot.default for many more options.

Resources