[R] writing function

Joshua Wiley jwiley.psych at gmail.com
Mon May 24 18:54:21 CEST 2010


Just thought I would add, if you want to specifically specify to look
in the function environment, you can do something like:


myfun <- function(x) {
env <- environment() #createa  variable of the function environment
rest of your stuff
mget(grep("DailyPL",ls(, envir=env),value=TRUE),envir=env)) #specifiy
function environment
}


On Mon, May 24, 2010 at 9:37 AM, arnaud Gaboury
<arnaud.gaboury at gmail.com> wrote:
> Thank you so much. You are totally right.
>
>
>
>
>> -----Original Message-----
>> From: Joshua Wiley [mailto:jwiley.psych at gmail.com]
>> Sent: Monday, May 24, 2010 6:33 PM
>> To: arnaud Gaboury
>> Cc: r-help at r-project.org
>> Subject: Re: [R] writing function
>>
>> My guess is that either ls(), called inside grep() or mget() is
>> looking in an environment you do not want it to.  When you create a
>> function, it has it's own environment.  If you want the dataframes to
>> be created inside the function call (which is what I think you were
>> doing before), you should set ls() and mget() to use your function
>> environment.  If the dataframes are supposed to be in the global
>> environment, I would try adding:
>>
>> mget(grep("DailyPL",ls(envir=.GlobalEnv),value=TRUE),envir=.GlobalEnv))
>>
>> HTH,
>>
>> Josh
>>
>> On Mon, May 24, 2010 at 9:17 AM, arnaud Gaboury
>> <arnaud.gaboury at gmail.com> wrote:
>> > Dear group,
>> >
>> > Here is my environment after I run a function, myfun()
>> >
>> >>myfun()
>> >> ls()
>> >  [1] "allcon"        "avprix16"      "DailyPL100416" "DailyPL100419"
>> > "DailyPL100420" "l"             "ll"            "myl"           "PL"
>> > "PLdaily"       "PLglobal"      "PLmonthly"
>> > [13] "Pos100415"     "Pos100416"     "Pos100419"     "Pos100420"
>> > "pose15"        "pose16"        "position"      "r"
>> "result"
>> > "sel"           "select"        "Trad100415"
>> > [25] "Trad100416"    "Trad100419"    "Trad100420"    "trade"
>> "tt"
>> > "value"         "w"             "zz"
>> >
>> > Elements "DailyPL100416" "DailyPL100419" "DailyPL100420" are data
>> frames
>> > created by a function, myfun().
>> > If then I pass this line manually at the prompt:
>> >
>> >>dd<-data.frame(do.call(rbind,
>> >
>> mget(grep("DailyPL",ls(),value=TRUE),envir=.GlobalEnv)),row.names=NULL)
>> >
>> > Here is what I get, wich is the expected result :
>> >
>> > dd <-
>> > structure(list(DESCRIPTION = structure(c(2L, 5L, 6L, 7L, 9L,
>> > 11L, 12L, 15L, 14L, 16L, 1L, 10L, 3L, 4L, 13L, 8L, 17L, 2L, 3L,
>> > 5L, 7L, 9L, 11L, 12L, 13L, 14L, 16L, 18L, 8L, 6L, 10L, 15L, 5L,
>> > 19L, 9L, 10L, 11L, 12L, 13L, 14L, 16L, 2L, 3L, 20L, 18L, 7L,
>> > 21L), .Label = c("COFFEE C Jul/10", "COPPER May/10", "CORN Jul/10",
>> > "CORN May/10", "COTTON NO.2 Jul/10", "CRUDE OIL miNY May/10",
>> > "GOLD Jun/10", "HENRY HUB NATURAL GAS May/10", "ROBUSTA COFFEE (10)
>> Jul/10",
>> >
>> > "SILVER May/10", "SOYBEANS Jul/10", "SPCL HIGH GRADE ZINC USD",
>> > "STANDARD LEAD USD", "SUGAR NO.11 Jul/10", "SUGAR NO.11 May/10",
>> > "WHEAT Jul/10", "WHEAT May/10", "CRUDE OIL miNY Jun/10", "HENRY HUB
>> NATURAL
>> > GAS Jun/10",
>> > "PALLADIUM Jun/10", "PLATINUM Jul/10"), class = "factor"), PL =
>> c(3500,
>> > -1874.99999999999, -2612.50000000003, -2169.99999999998, -680,
>> > 425, 1025, 1008.00000000000, -3057.59999999999, 3212.5, -
>> 1781.25000000001,
>> > -2265.0, 75, -387.5, 2950, 490.000000000013, 0, 2612.49999999998,
>> > -4162.5, 279.99999999998, 589.999999999964, -3670, -1212.5, 887.5,
>> > -625, 3976.00000000000, -7250, -1112.50000000000, -289.999999999999,
>> > -1414.99999999999, 275, -526.400000000003, 6914.99999999999,
>> > -19.9999999999978, -2330, 54.9999999999955, 1725, -3012.5, -5175,
>> > -1769.60000000001, 3787.5, -537.500000000009, 175, -5330.00000000001,
>> > 362.49999999999, 590.000000000009, -2995.00000000000)), .Names =
>> > c("DESCRIPTION",
>> > "PL"), row.names = c(NA, -47L), class = "data.frame")
>> >
>> > If I add this same line at the end of my function , here is what I
>> get:
>> >
>> >> myfun()
>> > data frame with 0 columns and 0 rows  #dd doesn't return
>> >
>> >
>> > What is wrong?
>> >
>> > Thank you for help.
>> >
>> > ______________________________________________
>> > 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.
>> >
>>
>>
>>
>> --
>> Joshua Wiley
>> Senior in Psychology
>> University of California, Riverside
>> http://www.joshuawiley.com/
>
>



-- 
Joshua Wiley
Senior in Psychology
University of California, Riverside
http://www.joshuawiley.com/



More information about the R-help mailing list