Displaying 20 results from an estimated 30000 matches similar to: "Extracting specific arguments from "...""
2025 Jan 08
1
Extracting specific arguments from "..."
I'd propose an alternative that I think is superior: rely on the semantics
of ... to do the work for you:
f1 <- function(...){
one <- list(...)[['a']]
two <- ...elt(match('a', ...names()))
c(one, two, three(...))
}
three <- function(a, ...) {
a
}
f1(a = 1, b = 2, c = 3)
#> [1] 1 1 1
On Sun, Jan 5, 2025 at 12:00?PM Bert Gunter <bgunter.4567 at
2025 Jan 06
1
Extracting specific arguments from "..."
I think Bert Gunter is right, but do you want partial matches (not found by match), and how robust do you want the code to be?
f <- function(?)
{ pos <- match('a', ...names())
if (is.na(pos))
stop("a is required.")
?elt(pos)
}
Incidentally, what is the best way to extract the expression without evaluating it?
g <- function(...)
{ pos <-
2025 Jan 06
1
Extracting specific arguments from "..."
Thanks Jorgen.
I thought your approach to getting the argument expressions was clever, but
somewhat convoluted. I think the usual simple way is to use match.call()
(or sys.call() )to get the unevaluated argument expressions; e.g. ...
f <- function(...){
match.call()
}
> f(a = 'red', b = sin(zzz))
f(a = "red", b = sin(zzz))
The return value is an object of class call
2025 Jan 06
2
Extracting specific arguments from "..."
Bert and other on this Chain,
The original question asked by Bert Gunter, highlights one of my long standing wishes. Perhaps my wish has already been fulfilled (if it has, please let me know where I can look of fulfill my wish), if it hasn't perhaps someone can grant me my wish.
I have tried to understand how to write a function (beyond a basic function), get values of the parameters, and
2025 Jan 07
1
Extracting specific arguments from "..."
It is a pretty tricky topic, but IMO Advanced R [1] steps you through it systematically... you just have to be prepared to follow along in R with the examples as you read it. In particular, the chapter on Functions goes through this.
The subtleties of how base R gives you control over these topics is what lead to the tidyverse creating new packages to build such function interfaces. Manipulating
2025 Jan 07
1
Extracting specific arguments from "..."
Jeff:
Would you care to offer an example of:
"String literals are surprisingly simple alternatives that don't bite you
in the butt nearly so often as NSE does."
"No" is a perfectly acceptable answer.
I would generally agree with you about NSE, but my original query was
motivated by something simple. I like to use lattice graphics when I fool
around with graphing data, as
2024 Jul 21
2
[External] Using the pipe, |>, syntax with "names<-"
The main challenge in Bert's original problem is that `[` and `[<-` cannot
be called in a pipeline. The obvious solution is to define named versions,
e.g.:
elt <- `[`
`elt<-` <- `[<-`
Then,
> z <- data.frame(a = 1:3, b = letters[1:3])
> z |> names() |> elt(2)
[1] "b"
> z |> names() |> elt(2) <- "foo"
> z
a foo
1 1 a
2 2
2017 Aug 03
2
Extracting numeric part from a string
... Or if you just want to stick with basic regex's without extra packages:
> x <- "\"cm_ffm\":\"563.77\""
> sub("[^[:digit:]]*([[:digit:]]*.?[[:digit:]]*).*","\\1",x)
[1] "563.77"
Cheers,
Bert
On Wed, Aug 2, 2017 at 5:16 PM, Ismail SEZEN <sezenismail at gmail.com> wrote:
>
>> On 3 Aug 2017, at 02:59,
2017 Aug 03
0
Extracting numeric part from a string
... and Marc's solution is **much** better than mine.
-- Bert
Bert Gunter
"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Wed, Aug 2, 2017 at 5:59 PM, Bert Gunter <bgunter.4567 at gmail.com> wrote:
> ... Or if you just want to stick with
2024 Jul 20
1
[External] Using the pipe, |>, syntax with "names<-"
I second Rich's excellent suggestion.
As with all elegant solutions, Iris's clicked on the wee light bulb in
my brain, and I realized that a slightly more verbose, but perhaps
more enlightening, alternative may be:
z |> attr("names") |> _[2] <- "foo"
However, I would add this as an example *only with* Iris's solution.
Hers should be shown whether or not
2018 Jan 15
2
consolidate three function into one
Hi Richard,
Thank you so much!! I understand the problem now, I assign a name to the "ggsurvplot" object and then add print(fig) at bottom of function definition, now figure gets printed on screen.
Ding
# function to generate RFS curves
RFS <- function( inputfile, N ) {
cluster<- survfit(Surv(RFS_days2, OV_Had_a_Recurrence_CODE) ~ clusters,
data =
2018 Jan 15
0
consolidate three function into one
That is certainly OK, but you can also just use
print(ggsurvplot(...))
as your final statement.
out <- RFS( ...)
would then return the ggsurvplot object *and* graph it.
Any good R tutorial or a web search will provide more details on function
returns, which you might find useful.
Cheers,
Bert
Bert Gunter
"The trouble with having an open mind is that people keep coming along and
2017 Jul 12
2
Extracting sentences with combinations of target words/terms from cancer patient text medical records
Hi Bert,
Thanks for your reply. It appears that I didn't replace the variable name "sampletxt" with the argument "x" in my function. I've corrected that and now my code seems to be working fine.
Paul
________________________________
From: Bert Gunter <bgunter.4567 at gmail.com>
Cc: R-help <r-help at r-project.org>
Sent: Tuesday, July 11, 2017 2:00 PM
2017 Jul 12
0
Extracting sentences with combinations of target words/terms from cancer patient text medical records
Hi Paul,
Sounds like you have your answer, but for fun I thought I'd try solving your problem using only a regular expression query and base R. I believe this works:
> txt <- "Patient had stage IV breast cancer. Nothing matches this sentence. Metastatic and breast match this sentence. French bike champion takes stage IV victory in Tour de France."
> pattern <-
2018 Jan 15
1
consolidate three function into one
Thank you, your suggestion is simpler and logically better. I had impression that the last object in a function gets returned, so I did not add the print function at the bottom line of the function definition. Returning an object and graph the object are different process, I am a beginner for writing R function and need to find a good guide source about writing R functions. If you know a good
2025 Jan 07
1
Extracting specific arguments from "..."
There's an ancient (2003) document on the CRAN "developers' page"
https://developer.r-project.org/model-fitting-functions.html that is
sort of (but not exactly) what you're looking for ...
On 2025-01-07 5:03 p.m., Sorkin, John wrote:
> Colleagues,
>
> My interest is not in writing ad hoc functions (which I might use once to analyze my data), but rather what I
2017 Jul 13
1
Extracting sentences with combinations of target words/terms from cancer patient text medical records
Hi Robert,
Thank you for your reply. An attempt to solve this via a regular expression query is particularly helpful. Unfortunately, I don't have much time to play around with this just now. Ultimately though, I think I would like to implement a solution something along the lines of what you have done. I have a book on regular expressions that I am now starting to read. In the meantime, the
2018 Jan 14
2
consolidate three function into one
Hi Bert,
I am sorry to bother you on weekend.
I am still struggling on defining a correct function.
I first defined the function RFS (see below), then run it by provide the two argument.
m52.2cluster <-RFS(inputfile =allinfo_m52, N=2 )
I do not get error message, but no figure displays on screen. I do not know what is going on.
Can you help me a little more on this issue?
Thank you,
2025 Jan 07
1
Extracting specific arguments from "..."
Ben,
As always, thank you.
You are correct, it is something like what I want, but not exactly. Perhaps someday someone will write a more complete guide.
Thank you,
John
John David Sorkin M.D., Ph.D.
Professor of Medicine, University of Maryland School of Medicine;
Associate Director for Biostatistics and Informatics, Baltimore VA Medical Center Geriatrics Research, Education, and Clinical
2025 Jan 07
1
Extracting specific arguments from "..."
Interesting discussion. A few things occurred to me.
Apologies to Iris Simmons: I mixed up his answer with Bert's question.
Bert raises questions about promises, and I think they are related to John Sorkin's question. A big difference between R and most other languages is that function arguments are computed lazily. match.call & substitute tell us what expressions will be evaluated