[R] saving variables created by functions in the workspace
Gabor Grothendieck
ggrothendieck at myway.com
Sat Feb 21 17:06:22 CET 2004
[There was an error in #3. I had omitted the <<-. Here it
is again.
sinx is local to your function so it is only known within
the function, not outside it. The reason its like that is
to promote modularity.
You can do one of the following:
1. Return it explicitly from your function:
plotsinx <- function(x) { sinx <- sin(x); plot(sinx); sinx }
Now call it like this:
sinx <- plotsinx(x)
2. Use assign and explicitly assign it in the global
environment rather than the local environment of the function:
plotsinx <- function(x) {
assign("sinx",sin(x),.GlobalEnv)
plot(sinx)
}
See ?assign
3. Use <<- as in:
plotsinx <- function(x) {
sinx <<- sin(x)
plot(sinx)
}
If you use this one be sure that you don't nest the
definition of plotsinx in another function since it actually
searches through the environments of the definition's
parents. See ?"<<-"
If you just want to look at it but not manipulate it outside
of the function you could try these:
4. Use print. See ?print
plotsinx <- function(x) {
sinx <- sin(x)
print(sinx)
plot(sinx)
}
5. Use debug
debug(plotsinx)
Now when you run the function you can step through it line
by line displaying each of the local variables if you like.
See ?debug
Date: Sat, 21 Feb 2004 11:07:00 +0100 (CET)
From: =?iso-8859-1?q?Fulvio=20Copex?= <copellifulvio at yahoo.it>
To: <r-help at stat.math.ethz.ch>
Subject: [R] saving variables created by functions in the workspace
Hello ,
just a simple question from a beginner:
I write the function:
plotsinx <- function()
{
x<-seq(0,2*pi,0.01)
sinx<-sin(x)
plot(sinx)
}
I recall it:
plotsinx()
and the plot works properly.
but then in the workspace if I want to look at the
values of sinx the following error is displayed:
Error: Object "sinx" not found.
How to save the variables created by the function on
the workspace?
Thank you very much, it seems to be very trivial...
Copex.
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
As AttachmentAs Inline Text Move to Folder----- Folders ------InboxDraftsSentTrashBulk Mail---- My Folders ----RRcomRtmpSaved
< Prev Next > Back to Inbox Print View Full Header
Make My Way Your Home Page | Spread the Word
My Settings: Overview | Search | Email | Chat | Portfolio | Calendar | Groups | Profile
IMPORTANT: We do not present our users with pop-ups, banners or any other non-text advertising. Nor do we send email
to our users. If you see or receive one of these items, it is coming from an outside source, either as a result of something you
have previously downloaded or as an "exit" pop-up from the site you just visited. It is not coming from our site.
Privacy Policy Terms of Service Partner with Us Our Mission Sign In Sign Out Help Center
© 2002-2004 My Way
More information about the R-help
mailing list