Displaying 20 results from an estimated 30000 matches similar to: "Using data and subset arguments in user-defined functions"
2013 Jan 01
1
Behavior or as.environment in function arguments/call (and force() behaviors...)
Happy 2013!
Can someone with more knowledge of edge case scoping/eval rules explain
what is happening below? Happens in all the versions of R I have on hand.
Behavior itself is confusing, but ?as.environment also provides no clue.
The term used in that doc is 'search list', which is ambiguous, but the
see also section mentions search(), so I would *think* that is what is
intended.
2006 Jun 22
2
weights in lm, glm (PR#9023)
Full_Name: James Signorovitch
Version: 2.2.1
OS: WinXP
Submission from: (NULL) (134.174.182.203)
In the code below, fn1() and fn2() fail with the messages given in the comments.
Strangely, fn2() fails for all data sets I've tried except for those with 100
rows.
The same errors occur if glm() is used in place of lm(), or if R 2.1.1 is used
on a unix system. Thanks for looking into this.
2008 Apr 17
1
Help with using 'get' function and variable scope
Hi -
I'm having a really hard time w/understanding R's get function, and would
appreciate any help with this.
Specifically, I'm using a for loop to call a function. I'd like the
function to have access to the variable being incremented in the for-loop,
i.e.
t.fn <- function() return( get( "i" ) )
t.fn2 <- function() {
for ( i in 1:5 )
cat( t.fn(),
2008 Aug 13
1
Arguments to lm() within a function - object not found
Hi all,
I'm having some difficulty passing arguments into lm() from within a
function, and I was hoping someone wiser in the ways of R could tell me
what I'm doing wrong. I have the following:
lmwrap <- function(...) {
wts <- somefunction()
print(wts) # This works, wts has the values I expect
fit <- lm(weights=wts,...)
return(fit)
}
If I call my function lmwrap, I get
2011 Mar 24
3
Extract the names of the arguments in an "expression"
Hi everybody:
I need to get the names of the arguments in an object of class "expression".
I've got the following expression:
> x <- expression(rho * cos(omega))
Is there any function or procedure that returns the names of the arguments
(in the example: "rho" and "omega")?
I tried a rough approach implemented in the function expr.args() shown
below. As
2004 Aug 09
1
error when calling debugger()
Hi,
I am getting an error message when I am trying to run the debugger() on
the last.dump. The debugger() stops after I make a selection. Could
someone please suggest what it might mean? The R log is included below.
This is R-1.8.1 on RH 7.3.
Thanks, Vadim
> load("last.dump.rda")
> debugger(last.dump)
Message: Error in split(x, f) : Group length is 0 but data length > 0
2015 Feb 09
1
WISH: eval() to preserve the "visibility" (now value is always visible)
Sorry to intervene.
Argument passed to 'eval' is evaluated first.
So,
eval(x <- 2)
is effectively like
{ x <- 2; eval(2) } ,
which is effectively
{ x <- 2; 2 } .
The result is visible.
eval(expression(x <- 2))
or
eval(quote(x <- 2))
or
evalq(x <- 2)
gives the same effect as
x <- 2 .
The result is invisible.
In function 'eval2',
res <-
2012 Sep 06
1
[Rscript] difficulty passing named arguments from commandline
Wanting a commandline solution (for a problem detailed @
http://mailman.unidata.ucar.edu/mailing_lists/archives/netcdfgroup/2012/msg00279.html
) I turned to Rscript, and whacked out the q'n'd
https://github.com/TomRoche/GEIA_to_netCDF/blob/master/netCDF.stats.to.stdout.r
However it wasn't as quick as hoped, because I spent quite a bit of time
figuring out how to pass the arguments.
2003 Nov 05
1
matching of arguments in ...?
I am puzzled by this (with R --vanilla):
> test <- function(formula, ...) lm(formula, ...)
> test(1:4 ~ 1, offset=rep(1,4))
Error in eval(expr, envir, enclos) : ..1 used in an incorrect
context, no ... to look in
> test(1:4 ~ 1, weights=rep(1,4))
Error in eval(expr, envir, enclos) : ..1 used in an incorrect
context, no ... to look in
> test(1:4 ~ 1, x=TRUE)
2015 Jul 15
2
bquote/evalq behavior changed in R-3.2.1
On Jul 15, 2015, at 12:51 PM, William Dunlap wrote:
> I think rapply() was changed to act like lapply() in this respect.
>
When I looked at the source of the difference, it was that typeof() returned 'language' in 3.2.1, while it returned 'list' in the earlier version of R. The first check in rapply's code in both version was:
if (typeof(object) != "list")
2005 Dec 21
2
Newbie - Summarize function
Dear R Users,
I have searched through the archives but I am still struggling to find a
way to process the below dataset. I have a dataset that has stratum and
plot identifier. Within each plot there is variable (Top) stating the
number of measurments that should be used to to calculate the mean to the
largest "top" elements within one of the vectors (X). I would like to
process
2010 Oct 25
2
Question on passing the subset argument to an lm wrapper
Hello,
How would you go about handling the following situation?
This is on R 2.12.0 on Ubuntu 32-bit.
I have a wrapper function to lm. I want to pass in a
subset argument. First, I just thought I'd use "...".
## make example reproducible
set.seed(123)
df1 <- data.frame(age = rnorm(100, 50, 10),
bmi = rnorm(100, 30, sd = 2))
## create a wrapper using
2015 Jul 15
2
bquote/evalq behavior changed in R-3.2.1
David,
If you are referring to the solution that would be:
rapply(list(test), eval, envir = fenv)
I thought I explained in the question that the above code does not work. It
does not throw an error, but the behavior is no different (at least in the
output or result). Using the above code still results in the x object not
being stored in fenv on 3.1.2.
Dayne
On Wed, Jul 15, 2015 at 4:40 PM,
2007 Dec 22
2
Understanding eval
After many hours of debugging code, I came to the conclusion that I
have a fundamental misunderstanding regarding eval, and hope that
someone here can explain to me, why the following code acts as it does:
foo <- function(expr) {
eval(substitute(expr), envir=list(a=5), enclos=parent.frame())
}
bar <- function(er) {
foo(er)
}
> foo(a)
[1] 5
> bar(a)
Error in eval(expr,
2010 Feb 09
1
The default argument 'envir' of eval()
In ?eval, it says
? ? ... ?If ?envir? is not
? ? specified, then the default is ?parent.frame()? (the environment
? ? where the call to ?eval? was made).
I tried the following example with "eval(expr)" ?and "eval(expr,
parent.frame())" in f(). The results are different, which are not
consistent with the help. Could somebody let know whether I
misunderstand the help? Or there
2013 Apr 21
1
Using copulas with user-defined marginal functions
I am trying to make a loglikelihood function using copulas. I am trying to
use mvdc to find the density function. When I run this I got the error that
the pdf and cdf of my function tobit doesn't exist. Can somebody guide me
where my mistake is?
dtobit <- function(beta,sigma, x, y) {ifelse(y>0, dnorm(y,x%*%beta,
sigma),(1-pnorm((x%*%beta)/sigma)))}
ptobit <- function(beta,sigma, x,
2013 Mar 19
1
source, sys.source and error line numbers
Hi,
is there a way to retrieve the line number of where en error occurred when
sourcing a file in a tryCatch statement? Is it stored somewhere accessible?
It is not found in the error object.
Consider the following code/output and note the difference in the traceback
between source (has line number) and sys.source (has no line number).
Thank you,
Renaud
########
# code
########
codefile <-
2017 May 09
3
R-3.3.3/R-3.4.0 change in sys.call(sys.parent())
Some formula methods for S3 generic functions use the idiom
returnValue$call <- sys.call(sys.parent())
to show how to recreate the returned object or to use as a label on a
plot. It is often followed by
returnValue$call[[1]] <- quote(myName)
E.g., I see it in packages "latticeExtra" and "leaps", and I suspect it
used in "lattice" as well.
This idiom
2010 Nov 16
2
Debugging segfault in foreach
Hi,
I'm using R-2.12 on a linux 64bit machine.
When I run a chunk of code inside a foreach() %do% { ...} or %dopar%
{...} (with doMC backend) I keep getting a segfault. Running the
*same* code within lapply(something, function(x) ... ) doesn't result
in any segfaults. I'll paste the output below, but I'm not sure it
would be helpful.
I'm more curious how to go about smoking
2005 Aug 15
2
stepAIC invalid scope argument
I am trying to replicate the first example from stepAIC from the MASS
package with my own dataset but am running into error. If someone can
point where I have gone wrong, I would appreciate it very much.
Here is an example :
set.seed(1)
df <- data.frame( x1=rnorm(1000), x2=rnorm(1000), x3=rnorm(1000) )
df$y <- 0.5*df$x1 + rnorm(1000, mean=8, sd=0.5)
# pairs(df); head(df)
lo <-