hi R folks, I need read a file from hardisk or www web. Then I need to define some new functions according to the contents of the read file. For example, i need write a package name "mypackage" like this:>library(mypackage) >read(some_file_on_web) #to see its content, suppose it contains:eat.drink.sleep then 3 new functions need to be created and usable. the problem is, how could I create functions after executing ">library(mypackage)" and make these new functions visible and usable immediately? Any information are appreciated. forgive me if this question is very stupid. but I really need help. Thank you all.
I got a dirty way to solve this. write a temp .R source file including these new functions, then>source(this_temp_file)but I don't know if there are some temp directory for R to store temp files? On 6/7/05, Hu Chen <chencheva at gmail.com> wrote:> hi R folks, > I need read a file from hardisk or www web. Then I need to define some > new functions according to the contents of the read file. > For example, i need write a package name "mypackage" like this: > >library(mypackage) > >read(some_file_on_web) #to see its content, suppose it contains: > eat.drink.sleep > then 3 new functions need to be created and usable. > the problem is, how could I create functions after executing > ">library(mypackage)" and make these new functions visible and usable > immediately? > > Any information are appreciated. forgive me if this question is very > stupid. but I really need help. > Thank you all. >
It't not clear to me exactly what you want and how you envision it could be done, but it probably helps to realize that functions in R (and S in general) are first class objects, so you can manipulate them pretty much like any other objects. You can "compute" them "on-the-fly", e.g., by having a fuction returning a function (or a list of functions). I find reading the Language Definition manual very helpful for things like this. As an example, the ecdf() function returns a function that can compute the estimated ecdf for any input. There may be simpler examples elsewhere. Andy> From: Hu Chen > > hi R folks, > I need read a file from hardisk or www web. Then I need to define some > new functions according to the contents of the read file. > For example, i need write a package name "mypackage" like this: > >library(mypackage) > >read(some_file_on_web) #to see its content, suppose it contains: > eat.drink.sleep > then 3 new functions need to be created and usable. > the problem is, how could I create functions after executing > ">library(mypackage)" and make these new functions visible and usable > immediately? > > Any information are appreciated. forgive me if this question is very > stupid. but I really need help. > Thank you all. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >
help.search("temporary") tells you to look at ?tempfile. Andy> From: Hu Chen > > I got a dirty way to solve this. > write a temp .R source file including these new functions, then > >source(this_temp_file) > but I don't know if there are some temp directory for R to > store temp files? > > On 6/7/05, Hu Chen <chencheva at gmail.com> wrote: > > hi R folks, > > I need read a file from hardisk or www web. Then I need to > define some > > new functions according to the contents of the read file. > > For example, i need write a package name "mypackage" like this: > > >library(mypackage) > > >read(some_file_on_web) #to see its content, suppose it contains: > > eat.drink.sleep > > then 3 new functions need to be created and usable. > > the problem is, how could I create functions after executing > > ">library(mypackage)" and make these new functions visible > and usable > > immediately? > > > > Any information are appreciated. forgive me if this question is very > > stupid. but I really need help. > > Thank you all. > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >
I've already given you an example (ecdf). You can look at the polynom package as well. Try: install.packages("polynom") library(polynom) as.function.polynomial Here's one trivial example:> f <- function(type) { g <- if (type == 1) cos else sin; g; } > myfun1 <- f(1) > myfun2 <- f(2) > myfun1(pi)[1] -1> myfun2(pi)[1] 1.224606e-16 As I said, reading the R Language Definition manual or S Programming helps a lot. Andy> From: Hu Chen > > Sorry for my confusing expression. > I need create some new temp functions after the package is loaded. And > these new functions should be visible and usable. > > Functions could be returned as a object, as Liaw mentioned. However I > can't find some examples in R-intro. I also want to know whether > functions returned by their "father function" could be called outside. > > Thank u all. > > > On 6/7/05, Uwe Ligges <ligges at statistik.uni-dortmund.de> wrote: > > Hu Chen wrote: > > > > > I got a dirty way to solve this. > > > write a temp .R source file including these new functions, then > > > > > >>source(this_temp_file) > > > > Don't know if you really have to do it that way, but I also > really don't > > understand what you are going to do... > > > > > > > but I don't know if there are some temp directory for R > to store temp files? > > > > See ?tempfile and ?tempdir > > > > Uwe Ligges > > > > > On 6/7/05, Hu Chen <chencheva at gmail.com> wrote: > > > > > >>hi R folks, > > >>I need read a file from hardisk or www web. Then I need > to define some > > >>new functions according to the contents of the read file. > > >>For example, i need write a package name "mypackage" like this: > > >> > > >>>library(mypackage) > > >>>read(some_file_on_web) #to see its content, suppose it contains: > > >> > > >>eat.drink.sleep > > >>then 3 new functions need to be created and usable. > > >>the problem is, how could I create functions after executing > > >>">library(mypackage)" and make these new functions > visible and usable > > >>immediately? > > >> > > >>Any information are appreciated. forgive me if this > question is very > > >>stupid. but I really need help. > > >>Thank you all. > > >> > > > > > > > > > ______________________________________________ > > > R-help at stat.math.ethz.ch mailing list > > > https://stat.ethz.ch/mailman/listinfo/r-help > > > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > > > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >
Mike R
2005-Jun-08 02:37 UTC
[R] how to run a script at the beginning of an interacive session ?
Hi All, How does one create an executable R-script, similar to an executable python script, or shell script except that when the R-script is finished, the R session remains open and becomes interactive. If I do this: R < script.r then R exits when finished. On the other hand, starting R then typing source("script.r",echo=T) is becoming tedious. Thanks in advance. Mike
Marc Schwartz
2005-Jun-08 03:05 UTC
[R] how to run a script at the beginning of an interacive session ?
On Tue, 2005-06-07 at 19:37 -0700, Mike R wrote:> Hi All, > > How does one create an executable R-script, similar > to an executable python script, or shell script except > that when the R-script is finished, the R session remains > open and becomes interactive. > > If I do this: > > R < script.r > > then R exits when finished. > > On the other hand, starting R then typing > > source("script.r",echo=T) > > is becoming tedious. > > Thanks in advance. > > MikeSee ?Startup, specifically the use of the .First function in .Rprofile. Note importantly that .First is run before any packages are loaded, including the base packages. As an example, if you wanted to create a plot() on startup each time, create a file called "script.r" containing: library(graphics) plot(1:10) Then in .Rprofile, create an entry as follows: .First <- function() {source("script.r")} With this in place, the plot will get created each time you start R. HTH, Marc Schwartz
Gabor Grothendieck
2005-Jun-08 03:05 UTC
[R] how to run a script at the beginning of an interacive session ?
On 6/7/05, Mike R <mike.rstat at gmail.com> wrote:> Hi All, > > How does one create an executable R-script, similar > to an executable python script, or shell script except > that when the R-script is finished, the R session remains > open and becomes interactive. > > If I do this: > > R < script.r > > then R exits when finished. > > On the other hand, starting R then typing > > source("script.r",echo=T) > > is becoming tedious.If you mean that you want to run certain R code every time you start up R then in R: ?Startup
Mike R
2005-Jun-08 04:33 UTC
[R] how to run a script at the beginning of an interacive session ?
On 6/7/05, Marc Schwartz <MSchwartz at mn.rr.com> wrote: On 6/7/05, Gabor Grothendieck <ggrothendieck at gmail.com> wrote: On 6/7/05, Liaw, Andy <andy_liaw at merck.com> wrote: <snips> Thanks Andy, Gabor and Marc. ---- contents of .Rprofile ---- .First <- function() {source("startup.r")} ---- contents of wrapR.v1 ---- #!/bin/bash ln -s $1 startup.r R rm -f startup.r ---- contents of wrapR.v2 ---- #!/bin/bash echo ".First <- function() {source(\"$1\")}" > .Rprofile R ---- contents of project_A.r ---- library(graphics) plot(1:10) and then start R with either of the following command lines: ## ./wrapR.v1 project_A.r ## ./wrapR.v2 project_A.r What do you think? is this the most simple way? Thanks in advance, Mike
Marc Schwartz
2005-Jun-08 22:41 UTC
[R] how to run a script at the beginning of an interacive session ?
On Wed, 2005-06-08 at 11:32 -0700, Mike R wrote:> On 6/8/05, Marc Schwartz <MSchwartz at mn.rr.com> wrote: > <snips> > > Thanks Marc, > > Cool ideas, thanks! Building on them, here is a twist. > > With this line in .Rprofile: > > .First <- function() {if(Sys.getenv("R_PROJECT")!="") > source(Sys.getenv("R_PROJECT"))} > > R can be run with this bash command line: > > ## export R_PROJECT=project_A.R; R; unset R_PROJECT > > > Alternatively, R could be run with this bash command line: > > ## wrapR project_4.R > > ---- contents of wrapR ---- > #! /bin/bash > export R_PROJECT=$1 > R > ---- end ---- > > What do you think? > > Thanks in advance, > Mike >Mike, At this point, I think whatever approach to implementing and managing the process you might find easier is the way to go. Whether it be the above or my approach. The initial iteration resulted in overwriting the default ~/.Rprofile file, which could cause problems if you had other options/settings in it. The key is ease of implementation, reduction in conflicts/errors and ongoing maintenance if you find yourself needing multiple startup options. HTH, Marc