An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090213/d6ffde5b/attachment-0001.pl>
Distinguishing variables from functions with the same name
8 messages · JLucke at ria.buffalo.edu, Gabor Grothendieck, Stavros Macrakis +2 more
See ?get and try:
sin <- 1
get("sin", mode = "numeric")
get("sin", mode = "function")
On Fri, Feb 13, 2009 at 10:20 AM, <JLucke at ria.buffalo.edu> wrote:
guRus:
I have a variable "beta" as an argument to R's beta function. So
essentially I have a case of beta(alpha, beta). What surprises me is that
R doesn't barf on this stupid programming practice. R gets the right
answer. How does R know "beta the variable" from "beta the function"?
Joseph F. Lucke
Senior Statistician
Research Institute on Addictions
University at Buffalo
SUNY
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list 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 Fri, Feb 13, 2009 at 10:47 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
See ?get and try:
Interesting. I hadn't paid attention to the 'mode' argument before.
Where would it be advisable to use anything but mode='any' or mode='function'?
-s
Stavros Macrakis wrote:
On Fri, Feb 13, 2009 at 10:47 AM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
See ?get and try:
Interesting. I hadn't paid attention to the 'mode' argument before. Where would it be advisable to use anything but mode='any' or mode='function'?
don't know if it really is advisable, but you can imagine a scenario
where you have different-moded like-named variables within nested
scopes, e.g.:
x = TRUE
local({
x = 0;
local({
x = 'foo';
list(get('x'), get('x', mode='numeric'), get('x',
mode='logical')) }) })
# list('foo', 0, TRUE)
basically, you want, within a scope, the value of a variable named 'x'
from in the closest environment in which it has the given type of value.
vQ
G'day Stavros, On Fri, 13 Feb 2009 11:11:28 -0500
Stavros Macrakis <macrakis at alum.mit.edu> wrote:
On Fri, Feb 13, 2009 at 10:47 AM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
See ?get and try:
Interesting. I hadn't paid attention to the 'mode' argument before. Where would it be advisable to use anything but mode='any' or mode='function'?
I guess the answer to this question is more often than not in the source. :)
On my machine:
berwin at berwin-nus1:/opt/src/R-devel-src/src/library$ find . -name "*.R" -type f | xargs grep "get(" \
| grep mode | grep -v function | grep -v all
./tools/R/QC.R: al <- get(al, envir = code_env, mode = "list") ./tools/R/QC.R: al <- get(al, envir = ns_env, mode = "list") ./tools/R/QC.R: al <- get(al, envir = data_env, mode = "list") ./utils/R/Sweave.R: syntax <- get(syntax, mode="list") ./utils/R/Sweave.R: syntax <- get(sname, mode = "list") ./utils/R/Sweave.R: s <- get(sname, mode="list") ./utils/R/str.R: str(get(nam, envir = E, mode = M), ./stats/R/model.tables.R: tables[[i]] <- tapply(data, model[model.cols[[i]]], get(fun)) ./stats/R/models.R: ## get(contr.funs[1 + isOF[nn]])(nlevels(data[[nn]])) ./base/R/get.R: .Internal(get(x, envir, mode, inherits)) ./base/R/get.R: .Internal(mget(x, envir, mode, ifnotfound, inherits)) ./base/R/match.fun.R: FUN <- get(as.character(FUN), mode = "any", envir = envir) The first few hits seem to indicate examples where it is necessary to look specifically for a list. Cheers, Berwin
On Fri, Feb 13, 2009 at 12:25 PM, Berwin A Turlach
<berwin at maths.uwa.edu.au> wrote:
On Fri, 13 Feb 2009 11:11:28 -0500 Stavros Macrakis <macrakis at alum.mit.edu> wrote:
Where would it be advisable to use anything but mode='any' or mode='function'?
I guess the answer to this question is more often than not in the source. :)
...
The first few hits seem to indicate examples where it is necessary to look specifically for a list.
Well, the presence of a construction in code is interesting, but
doesn't answer the question of whether it is *advisable* to write code
this way...!
-s
Berwin A Turlach wrote:
G'day Stavros, On Fri, 13 Feb 2009 11:11:28 -0500 Stavros Macrakis <macrakis at alum.mit.edu> wrote:
On Fri, Feb 13, 2009 at 10:47 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
See ?get and try:
Interesting. I hadn't paid attention to the 'mode' argument before.
Where would it be advisable to use anything but mode='any' or
mode='function'?
I guess the answer to this question is more often than not in the source. :)
On my machine:
berwin at berwin-nus1:/opt/src/R-devel-src/src/library$ find . -name "*.R" -type f | xargs grep "get(" \
| grep mode | grep -v function | grep -v all
hmm, looks like you can do it with just one call to grep: grep -R -P --include=*.R 'get\(.*mode\s*=\s*"(?!function|any)' . # ./src/library/utils/R/Sweave.R: syntax <- get(syntax, mode="list") # ./src/library/utils/R/Sweave.R: syntax <- get(sname, mode = "list") # ./src/library/utils/R/Sweave.R: s <- get(sname, mode="list") # ./src/library/tools/R/QC.R: al <- get(al, envir = code_env, mode = "list") # ./src/library/tools/R/QC.R: al <- get(al, envir = ns_env, mode = "list") # ./src/library/tools/R/QC.R: al <- get(al, envir = data_env, mode = "list") (you'll need grep compiled with --enable-perl-regexpr for this) note this fancy comment in ?get: " 'mode' here is a mixture of the meanings of 'typeof' and 'mode'" vQ
./tools/R/QC.R: al <- get(al, envir = code_env, mode = "list") ./tools/R/QC.R: al <- get(al, envir = ns_env, mode = "list") ./tools/R/QC.R: al <- get(al, envir = data_env, mode = "list") ./utils/R/Sweave.R: syntax <- get(syntax, mode="list") ./utils/R/Sweave.R: syntax <- get(sname, mode = "list") ./utils/R/Sweave.R: s <- get(sname, mode="list") ./utils/R/str.R: str(get(nam, envir = E, mode = M), ./stats/R/model.tables.R: tables[[i]] <- tapply(data, model[model.cols[[i]]], get(fun)) ./stats/R/models.R: ## get(contr.funs[1 + isOF[nn]])(nlevels(data[[nn]])) ./base/R/get.R: .Internal(get(x, envir, mode, inherits)) ./base/R/get.R: .Internal(mget(x, envir, mode, ifnotfound, inherits)) ./base/R/match.fun.R: FUN <- get(as.character(FUN), mode = "any", envir = envir) The first few hits seem to indicate examples where it is necessary to look specifically for a list. Cheers, Berwin
______________________________________________ R-help at r-project.org mailing list 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.
Stavros Macrakis wrote:
On Fri, Feb 13, 2009 at 12:25 PM, Berwin A Turlach <berwin at maths.uwa.edu.au> wrote:
On Fri, 13 Feb 2009 11:11:28 -0500
Stavros Macrakis <macrakis at alum.mit.edu> wrote:
Where would it be advisable to use anything but mode='any' or
mode='function'?
I guess the answer to this question is more often than not in the source. :)
...
The first few hits seem to indicate examples where it is necessary
to look specifically for a list.
Well, the presence of a construction in code is interesting, but doesn't answer the question of whether it is *advisable* to write code this way...!
... it doesn't even say if it really made sense in those cases, i.e., whether there was no better solution. vQ