Displaying 20 results from an estimated 30000 matches similar to: "Extracting specific arguments from "...""
2025 Jan 08
1
Extracting specific arguments from "..."
That's very nice, Hadley. Simple and clean. Never would have thought of it
myself.
As usual, however, in the course of my churnings, I have a further
complication to add. But first ...
**TO ALL**: Feel free to ignore the following, as I'm just fooling around
here and don't want to waste your time with my stupid stuff.
Anyway, the complication is motivated by the use of formals() or
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 09
1
Extracting specific arguments from "..."
I might add that there seems to be a subtle difference between using
`...elt()` and `match.call()`, which is that the former causes `a` itself
to be evaluated while the latter doesn't:
```
# Some approaches that have been suggested:
# 1. Using `list()` (Bert Gunter)
f1 <- function(...) list(...)[["a"]]
# 2. Using `...elt()` (Bert Gunter)
f2 <- function(...)
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
2025 Mar 30
1
[External] Creating model formulas programmatically
my take of the assignment was to avoid 'parse' specifically.
we start with a character vector, so avoiding characters is not possible. i was dealing with the fortune "if parse is the answer, you have the wrong question"
Sent from my iPhone
On Mar 29, 2025, at 15:39, Bert Gunter <bgunter.4567 at gmail.com> wrote:
?
Thanks, Rich.
I thought of that, too, but it violates
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
2025 Mar 30
1
[External] Creating model formulas programmatically
I am confused. Richard's answer that Bert did not like did not use parse explicitly. Richard pasted together a string that a function like lm() will have to parse to run the analysis. However, the answers so far do not use parse(). In the reply to Richard, Bert indicated we cannot use strings. Even if I pass a vector where R can assume that the first variable is the dependent variable and all
2025 Mar 29
2
[External] Creating model formulas programmatically
Thanks, Rich.
I thought of that, too, but it violates the spirit of my restraints (to
avoid character strings), which I unfortunately did not clearly articulate.
So my apologies for that failure. My concern is that with more complex
model formula, using as.formula, etc. to parse/convert character strings
can get a bit hairy. But in most cases, as here maybe, it may be perfectly
fine. So think of
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 =
2025 Mar 30
1
[External] Creating model formulas programmatically
As always, I would like to thank all who responded for their insights and
suggestions. I have learned from them.
Thus far, my own aesthetic preference -- and therefore not to be considered
in any sense as a "best" approach -- is to use Duncan's suggestion to
produce the call directly with call() rather than substitute in my simple
for() loop; i.e.
somenames <-
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 <-