[R-SIG-Mac] .First() function not being executed on R.app startup

Steven McKinney smckinney at bccrc.ca
Wed Nov 28 03:54:55 CET 2007


Hi Simon,

Thanks for the clarification.
I've moved .First to .Rprofile and that
now works fine.

Could you add this information to the 
R for Mac OS X FAQ
and/or other appropriate R.app documentation?

E.g. add this suggested note (modified as necessary) to section 4.3.3 of the FAQ:

Note that R.app loads the user workspace .RData (if present in the startup directory) after R has been initialized.  Thus the user should put .First() and similar startup functions in .Rprofile rather than the user workspace.

(Also, minor typo: "If R.app is not runnig" should be
"If R.app is not running")



R for Mac OS X FAQ

Next: Resizing the R Console, Previous: Copy and Paste, Up: The R Console 
4.3.3 The current and startup working directories

If not otherwise specified in the Preferences (see Preferences), or if the specified path is no longer available, then the default working directory at startup is the user home. The working directory can be changed using the setwd R command or using the Misc menu item Change working directory. Finally it is possible to use a specific directory for a single R session by dragging a folder onto the R.app icon. If R.app is not runnig, this will cause R.app to be started in the directory corresponding to the folder dragged. The same can be achieved on the command line - for example open -a R . causes R.app to be started using the current directory as the startup working directory.

This feature is useful if you want different startup procedures defined by the .Rprofile; you can edit a .Rprofile (containing you personal initialization R commands) in a particular directory and use the Preferences (or any other method mentioned above) to change the startup directory. Next time you launch R.app the .Rprofile is read and executed by R at startup. This is the equivalent on Unix (or the command line) to launch R from different directories.

Note that R.app loads the user workspace .RData (if present in the startup directory) after R has been initialized.  Thus the user should put .First() and similar startup functions in .Rprofile rather than the user workspace.



Best 
Steve M



-----Original Message-----
From: Simon Urbanek [mailto:simon.urbanek at r-project.org]
Sent: Tue 11/27/2007 6:30 PM
To: Steven McKinney
Cc: R-SIG-Mac at stat.math.ethz.ch
Subject: Re: [R-SIG-Mac] .First() function not being executed on R.app startup
 

On Nov 27, 2007, at 9:25 PM, Steven McKinney wrote:

> Hi Simon,
>
> Thanks for checking.  I'm not sure
> then why mine is not working.
>
> I removed all objects, and set up your
> example .First function:
>
>
>> rm(list = ls(all = TRUE))
>> ls(all = TRUE)
>   character(0)
>> .First <- function() cat(".First was called\n")
>> ls(all = TRUE)
>   [1] ".First"
>> .First
>   function() cat(".First was called\n")
>> q()
>
> I saved the workspace after q(), then started
> R.app again (from the R.app icon in the dock).
> The message does not appear, though the .First
> function is still there:
>
>   R version 2.6.1 (2007-11-26)
>   Copyright (C) 2007 The R Foundation for Statistical Computing
>   ISBN 3-900051-07-0
>
>   R is free software and comes with ABSOLUTELY NO WARRANTY.
>   You are welcome to redistribute it under certain conditions.
>   Type 'license()' or 'licence()' for distribution details.
>
>     Natural language support but running in an English locale
>
>   R is a collaborative project with many contributors.
>   Type 'contributors()' for more information and
>   'citation()' on how to cite R or R packages in publications.
>
>   Type 'demo()' for some demos, 'help()' for on-line help, or
>   'help.start()' for an HTML browser interface to help.
>   Type 'q()' to quit R.
>
>   [Workspace restored from /Users/stevenmckinney/.RData]
>
>> ls(all = TRUE)
>   [1] ".First"
>> .First
>   function() cat(".First was called\n")
>>
>
>
> I notice that your session does not show a message such as
> "[Workspace restored from /Users/stevenmckinney/.RData]"
>
> Something is different about our setup.
>
> Any idea why I get this message and you do not?
> Is your .First just saved in
> ~/.RData ?
>

No, mine is in ~/.Rprofile
AFAIR .RData is treated differently in the R GUI, because it is  
loaded *after* R has been initialized. This comes from the way R is  
started in the GUI, so I don't think you can use .First in the saved  
workspace.

Cheers,
Simon


> Best
> Steve M
>
>
> -----Original Message-----
> From: Simon Urbanek [mailto:simon.urbanek at r-project.org]
> Sent: Tue 11/27/2007 5:45 PM
> To: Steven McKinney
> Cc: R-SIG-Mac at stat.math.ethz.ch
> Subject: Re: [R-SIG-Mac] .First() function not being executed on  
> R.app startup
>
> Steven,
>
> can you be more specific, please? I cannot reproduce your
> problem, .First works for me just fine:
>
> R version 2.6.1 (2007-11-26)
> Copyright (C) 2007 The R Foundation for Statistical Computing
> ISBN 3-900051-07-0
>
> R is free software and comes with ABSOLUTELY NO WARRANTY.
> You are welcome to redistribute it under certain conditions.
> Type 'license()' or 'licence()' for distribution details.
>
>    Natural language support but running in an English locale
>
> R is a collaborative project with many contributors.
> Type 'contributors()' for more information and
> 'citation()' on how to cite R or R packages in publications.
>
> Type 'demo()' for some demos, 'help()' for on-line help, or
> 'help.start()' for an HTML browser interface to help.
> Type 'q()' to quit R.
>
> .First was called
>> .First
> function() cat(".First was called\n")
>
> Cheers,
> Simon
>
>
> On Nov 27, 2007, at 6:00 PM, Steven McKinney wrote:
>
>> Hi all,
>>
>> I've set up a .First function to run help.start()
>> but when I invoke R.app the .First function is not
>> apparently executed.  I've checked preferences and
>> documentation but can not find any indication that
>> .First() should not be executed.
>>
>> Is this a bug in R.app, or have I missed something
>> about how to get .First() to be invoked when
>> I run R.app?  I include session information below.
>> When I invoke R from a terminal shell, help.start()
>> is executed and a browser window does pop up, but
>> not when I invoke R from R.app.
>>
>> Any information appreciated.
>>
>> Best
>>
>> Steve McKinney
>>
>>
>> R version 2.6.1 (2007-11-26)
>> Copyright (C) 2007 The R Foundation for Statistical Computing
>> ISBN 3-900051-07-0
>>
>> R is free software and comes with ABSOLUTELY NO WARRANTY.
>> You are welcome to redistribute it under certain conditions.
>> Type 'license()' or 'licence()' for distribution details.
>>
>>   Natural language support but running in an English locale
>>
>> R is a collaborative project with many contributors.
>> Type 'contributors()' for more information and
>> 'citation()' on how to cite R or R packages in publications.
>>
>> Type 'demo()' for some demos, 'help()' for on-line help, or
>> 'help.start()' for an HTML browser interface to help.
>> Type 'q()' to quit R.
>>
>> [Workspace restored from /Users/stevenmckinney/.RData]
>>
>>> ls(all=TRUE)
>> [1] ".First"    ".required"
>>> .First
>> function(){utils::help.start()}
>>> sessionInfo()
>> R version 2.6.1 (2007-11-26)
>> powerpc-apple-darwin8.10.1
>>
>> locale:
>> en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
>>
>> attached base packages:
>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>>
>>
>> _______________________________________________
>> R-SIG-Mac mailing list
>> R-SIG-Mac at stat.math.ethz.ch
>> https://stat.ethz.ch/mailman/listinfo/r-sig-mac
>>
>>
>
>
>



More information about the R-SIG-Mac mailing list