I am trying to redefine the default behavior of base::table by editing
.Rprofile in my home directory:
table <- function(...,
useNA = "ifany",
exclude = if (useNA == "no") c(NA, NaN),
dnn = list.names(...),
deparse.level = 1){
base::table(...,
useNA = useNA,
exclude = exclude,
dnn = dnn,
deparse.level = deparse.level)
}
Trying it, I get
> table(c(1,1,2,NA))
Error in list.names(...) : could not find function "list.names"
However, by removing the argument 'dnn', it apparently works. I am
probably doing this in the wrong way, but how should it be done?
I can guess what 'list.names' is (from the documentation), but where and
how is it defined?
Thanks, G?ran
(On R-4.1.3)
Where is list.names?
6 messages · Ivan Krylov, Eric Berger, Göran Broström
? Wed, 30 Mar 2022 11:27:05 +0200 G?ran Brostr?m <goran.brostrom at umu.se> ?????:
I can guess what 'list.names' is (from the documentation), but where and how is it defined?
Interesting! It's defined first thing inside the function, so by the time the function tries to use the argument, list.names exists and can be called, but for all other purposes, it's not there. What if you don't set a default value for dnn in your wrapper or even mention dnn as a formal argument in your wrapper? In many cases, it's possible to forward missing arguments to functions and have them do the right thing. The only other solution I can think of is non-standard evaluation.
Best regards, Ivan
At the R console prompt do:
table
It prints out the source of the table() function. The first thing there is the definition of list.names() HTH, Eric On Wed, Mar 30, 2022 at 12:27 PM G?ran Brostr?m <goran.brostrom at umu.se> wrote:
I am trying to redefine the default behavior of base::table by editing
.Rprofile in my home directory:
table <- function(...,
useNA = "ifany",
exclude = if (useNA == "no") c(NA, NaN),
dnn = list.names(...),
deparse.level = 1){
base::table(...,
useNA = useNA,
exclude = exclude,
dnn = dnn,
deparse.level = deparse.level)
}
Trying it, I get
> table(c(1,1,2,NA))
Error in list.names(...) : could not find function "list.names" However, by removing the argument 'dnn', it apparently works. I am probably doing this in the wrong way, but how should it be done? I can guess what 'list.names' is (from the documentation), but where and how is it defined? Thanks, G?ran (On R-4.1.3)
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
On 2022-03-30 11:48, Ivan Krylov wrote:
? Wed, 30 Mar 2022 11:27:05 +0200 G?ran Brostr?m <goran.brostrom at umu.se> ?????:
I can guess what 'list.names' is (from the documentation), but where and how is it defined?
Interesting! It's defined first thing inside the function, so by the time the function tries to use the argument, list.names exists and can be called, but for all other purposes, it's not there.
Thanks for that (also thanks to Eric)!
What if you don't set a default value for dnn in your wrapper or
That works, if I check for missing argument:
table <- function(...,
useNA = "ifany",
exclude = if (useNA == "no") c(NA, NaN),
dnn,
deparse.level = 1){
if (missing(dnn)){
base::table(...,
exclude = exclude,
useNA = useNA,
deparse.level = deparse.level)
}else{
base::table(...,
useNA = useNA,
exclude = exclude,
dnn = dnn,
deparse.level = deparse.level)
}
}
Is this the final word?
G?ran
even mention dnn as a formal argument in your wrapper? In many cases, it's possible to forward missing arguments to functions and have them do the right thing. The only other solution I can think of is non-standard evaluation.
? Wed, 30 Mar 2022 12:43:52 +0200 G?ran Brostr?m <goran.brostrom at umu.se> ?????:
That works, if I check for missing argument
Is this the final word?
How about omitting it entirely and letting ... handle it? table <- function( ..., useNA = "ifany", exclude = if (useNA == "no") c(NA, NaN), deparse.level = 1 ) base::table( ..., useNA = useNA, exclude = exclude, deparse.level = deparse.level ) table(c(1,1,2,NA)) # # 1 2 <NA> # 2 1 1 table(c(1,1,2,NA), dnn = 'a') # a # 1 2 <NA> # 2 1 1
Best regards, Ivan
Den 2022-03-30 kl. 13:01, skrev Ivan Krylov:
? Wed, 30 Mar 2022 12:43:52 +0200 G?ran Brostr?m <goran.brostrom at umu.se> ?????:
That works, if I check for missing argument
Is this the final word?
How about omitting it entirely and letting ... handle it?
Looks nice, thank you!
table <- function( ..., useNA = "ifany", exclude = if (useNA == "no") c(NA, NaN), deparse.level = 1 ) base::table( ..., useNA = useNA, exclude = exclude, deparse.level = deparse.level ) table(c(1,1,2,NA)) # # 1 2 <NA> # 2 1 1 table(c(1,1,2,NA), dnn = 'a') # a # 1 2 <NA> # 2 1 1