Jan van der Laan
2012-Nov-01 08:14 UTC
[R] Start R from bash/bat file and end in interactive mode
I have a r-script (rook.R) that starts a Rook server. To present users from having to start R and type in source("rook.R"), I want to create a bash script and bat file that starts R and sources the script. However, to keep the Rook server running R should not close after running the script and stay in interactive mode. This proves more difficult than expected. I tried various combinations of commandline parameters and pipes, but none of them seem to work: $R -f rook.R --interactive Runs and quits $ cat rook.R | R Fatal error: you must specify '--save', '--no-save' or '--vanilla' $cat rook.R | R --no-save Runs and quits $R --no-save < rook.R Runs and quits $R --no-save --interactive < rook.R Runs and quits I would have expected the --interactive parameter to do what I want, but it doesn't seem to do anything. What does work is to create a .Rprofile with sourc("rook.R") in it in the directory and then start R (just plain R). However I don't find this a very elegant solution. I could of create the .Rprofile file in the bash script which is somewhat better, but still not very elegant. I end up with the following bash script: #!/bin/bash echo "source(\"rook.R\")" > .Rprofile R Another, not very elegant, possible solution which I haven't tried is to start a while loop at the end of the script with a sleep command in it. Does there exist a better solution? Regards, Jan
Frank Schwidom
2013-Apr-07 09:45 UTC
[R] Start R from bash/bat file and end in interactive mode
one way could be: $ cat rook.R | R --interactive but with a little different console behaviour the problem in your other examples is, that R uses only the first appearing switch of: --interactive, --f, -e, --vanilla, --no-save, ... the switches cannot be combined On Thu, Nov 01, 2012 at 09:14:09AM +0100, Jan van der Laan wrote:> > I have a r-script (rook.R) that starts a Rook server. To present users > from having to start R and type in source("rook.R"), I want to create > a bash script and bat file that starts R and sources the script. > However, to keep the Rook server running R should not close after > running the script and stay in interactive mode. This proves more > difficult than expected. > > I tried various combinations of commandline parameters and pipes, but > none of them seem to work: > > $R -f rook.R --interactive > Runs and quits > > $ cat rook.R | R > Fatal error: you must specify '--save', '--no-save' or '--vanilla' > > $cat rook.R | R --no-save > Runs and quits > > $R --no-save < rook.R > Runs and quits > > $R --no-save --interactive < rook.R > Runs and quits > > I would have expected the --interactive parameter to do what I want, > but it doesn't seem to do anything. > > What does work is to create a .Rprofile with sourc("rook.R") in it in > the directory and then start R (just plain R). However I don't find > this a very elegant solution. I could of create the .Rprofile file in > the bash script which is somewhat better, but still not very elegant. > I end up with the following bash script: > > #!/bin/bash > echo "source(\"rook.R\")" > .Rprofile > R > > Another, not very elegant, possible solution which I haven't tried is > to start a while loop at the end of the script with a sleep command in > it. > > Does there exist a better solution? > > Regards, > > Jan > > ______________________________________________ > 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.
Frank Schwidom
2013-Apr-07 09:50 UTC
[R] Start R from bash/bat file and end in interactive mode
another working Example: { echo "print( 1)"; cat;} | R --interactive but do not end with <ctrl>-D, it overrides the "save workspace" question; use q() instead there is an additional echo where i don't know how to avoid them On Thu, Nov 01, 2012 at 09:14:09AM +0100, Jan van der Laan wrote:> > I have a r-script (rook.R) that starts a Rook server. To present users > from having to start R and type in source("rook.R"), I want to create > a bash script and bat file that starts R and sources the script. > However, to keep the Rook server running R should not close after > running the script and stay in interactive mode. This proves more > difficult than expected. > > I tried various combinations of commandline parameters and pipes, but > none of them seem to work: > > $R -f rook.R --interactive > Runs and quits > > $ cat rook.R | R > Fatal error: you must specify '--save', '--no-save' or '--vanilla' > > $cat rook.R | R --no-save > Runs and quits > > $R --no-save < rook.R > Runs and quits > > $R --no-save --interactive < rook.R > Runs and quits > > I would have expected the --interactive parameter to do what I want, > but it doesn't seem to do anything. > > What does work is to create a .Rprofile with sourc("rook.R") in it in > the directory and then start R (just plain R). However I don't find > this a very elegant solution. I could of create the .Rprofile file in > the bash script which is somewhat better, but still not very elegant. > I end up with the following bash script: > > #!/bin/bash > echo "source(\"rook.R\")" > .Rprofile > R > > Another, not very elegant, possible solution which I haven't tried is > to start a while loop at the end of the script with a sleep command in > it. > > Does there exist a better solution? > > Regards, > > Jan > > ______________________________________________ > 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.
Duncan Murdoch
2013-Apr-07 11:34 UTC
[R] Start R from bash/bat file and end in interactive mode
On 12-11-01 4:14 AM, Jan van der Laan wrote:> > I have a r-script (rook.R) that starts a Rook server. To present users > from having to start R and type in source("rook.R"), I want to create > a bash script and bat file that starts R and sources the script. > However, to keep the Rook server running R should not close after > running the script and stay in interactive mode. This proves more > difficult than expected. > > I tried various combinations of commandline parameters and pipes, but > none of them seem to work: > > $R -f rook.R --interactive > Runs and quits > > $ cat rook.R | R > Fatal error: you must specify '--save', '--no-save' or '--vanilla' > > $cat rook.R | R --no-save > Runs and quits > > $R --no-save < rook.R > Runs and quits > > $R --no-save --interactive < rook.R > Runs and quits > > I would have expected the --interactive parameter to do what I want, > but it doesn't seem to do anything. > > What does work is to create a .Rprofile with sourc("rook.R") in it in > the directory and then start R (just plain R). However I don't find > this a very elegant solution. I could of create the .Rprofile file in > the bash script which is somewhat better, but still not very elegant. > I end up with the following bash script: > > #!/bin/bash > echo "source(\"rook.R\")" > .Rprofile > R > > Another, not very elegant, possible solution which I haven't tried is > to start a while loop at the end of the script with a sleep command in > it. > > Does there exist a better solution?Yes, don't use a script, use a package. Packages can run startup code when they load. Then include your package among those loaded by default. See the ?options help on defaultPackages for how to do that via environment variable. Duncan Murdoch
Seemingly Similar Threads
- Rook: software and specification for R web applications and servers
- Rook: software and specification for R web applications and servers
- Command-line arguments and --interactive
- concurrent requests (Rook, but I think the question is more general)
- sshd behaviour when people are trying to break in