Hi, I have a couple of quick questions regarding R. 1) I have a ".First" function that automatically loads the quadprog package into the workspace. The .First function resides in R and is only saved if I save the workspace. The exact function is as follows: .First<-function() { library(quadprog) } Is there any way that this function can be saved outside of R? That is, is there any procedure that will run specified R commands whenever R is invoked? 2) Is there anyway to specify additional paths for R to search. For example, I have some R script named, myProg.r that is saved in a directory C:\Programs\RLanguage\Rob\. I run the script in the following way: source("C:/Programs/RLanguage/Rob/myProg.r") This works fine, but I was wondering if there is any command that could be entered into the .First function described above that adds the directory C:\Programs\RLanguage\Rob\ to the R path. This way, C:\Programs\RLanguage\Rob\ will always be checked for the code and I could simply run myProg.r as follows: source("myProg.r") 3) Is there anyway that I could save functions that I have written outside of R. That is, could I write functions and have them saved in some directory, such as, C:\Programs\RLanguage\RobFunctions\? This way I would be able to copy the functions to other pcs. This is very simple in MatLab. Please let me know. Thanks for the help. Rob
Rob, On Wed, Mar 31, 2004 at 05:28:19PM -0500, Kissell, Robert [EQRE] wrote:> 1) I have a ".First" function that automatically loads the quadprog package into the workspace. The .First function resides in R and is only saved if I save the workspace. The exact function is as follows:[...]> Is there any way that this function can be saved outside of R? That is, is there any procedure that will run specified R commands whenever R is invoked?Try help(Startup) You probably want .Rprofile (on a per-directory basis), and/or the site-wide $R_HOME/etc/Rprofile.> 2) Is there anyway to specify additional paths for R to search. For example, I have some R script named, myProg.r that is saved in a directory C:\Programs\RLanguage\Rob\. I run the script in the following way: > source("C:/Programs/RLanguage/Rob/myProg.r") > > This works fine, but I was wondering if there is any command that could be entered into the .First function described above that adds the directory C:\Programs\RLanguage\Rob\ to the R path. This way, C:\Programs\RLanguage\Rob\ will always be checked for the code and I could simply run myProg.r as follows: > source("myProg.r")For library(), there is an argumemnt lib.loc. You can also set things from Rprofile. Source is simpler and wants pathname, so maybe you want to write a local function that does your desired magic with the directory and appends the filename you supply, before calling source() on the combined string?> 3) Is there anyway that I could save functions that I have written outside of R. That is, could I write functions and have them saved in some directory, such as, C:\Programs\RLanguage\RobFunctions\? This way I would be able to copy the functions to other pcs. This is very simple in MatLab. Please let me know.See 2), save() and friends have pathnames too. So save("N:/my/network/share/foo.R") works just fine. Hth, Dirk -- The relationship between the computed price and reality is as yet unknown. -- From the pac(8) manual page
At 5:28 PM -0500 3/31/04, Kissell, Robert [EQRE] wrote:>Hi, >I have a couple of quick questions regarding R. ><- snip ->>3) Is there anyway that I could save functions that I have written >outside of R. That is, could I write functions and have them saved >in some directory, such as, C:\Programs\RLanguage\RobFunctions\? >This way I would be able to copy the functions to other pcs. This is >very simple in MatLab. Please let me know.Yes. Here are two ways. - 1 - In the directory where you have your function definitions, load the functions into R. Quit R, and say "yes" when asked whether or not to save the workspace image. Find the file in which the image is saved, copy it to the other pcs. On the other pcs, use the "attach" function to attach that file. The functions are now available. -2- Go get the R Extensions manual, learn how to create a package. Take your functions, make them into a package. Copy the package file to the other pcs. Use R's "install package" functionality to install the package. Then, whenever the functions are needed, use the "library" command to load your package.>Thanks for the help. > >Rob >-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA