This question already has answers here:
Run R script from command line
(7 answers)
Closed 4 years ago.
I am calling a R function from R studio like below
source("test.R)
test()
I now want to call this using Unix shell script.
Please let me know how to achieve this. Thanks.
"The Unix way" to do this is to add a first line in the so-called shebang style
#!/usr/bin/env Rscript
to the file test.R, and to follow this with
chmod 0755 test.R
to make it executable. Then you can just say
./test.R
and you created a new command. As you are on Unix you may also like our littler alternative to Rscript which you can install from CRAN, or use from your distro (ie Ubuntu or Debian)
Rscript -e 'source("test.R"); test()'
You could always write another script, newScript.R, that contains:
source("test.R")
test()
and then from the command line, you can run:
Rscript newScript.R
Related
I am new to R script.
I found at the beginning of the R script,
#!/conda_env/myenv/bin/Rscript
library('httr')
May I know what does it mean #!/conda_env/myenv/bin/Rscript?
What happen if I do not add #!/conda_env/myenv/bin/Rscript?
Thank you
The symbol #! is known as a shebang.
When a script starts with #!/conda_env/myenv/bin/Rscript you can run it from your terminal without specifying that it needs Rscript. If your file is called myfile.R you can run it with
/path/to/myfile.R
If you remove this line you have to run the file with the command
/conda_env/myenv/bin/Rscript /path/to/myfile.R
Edit: If /path/to/myfile.R cannot run you must make it executable. In your terminal this can be done with the command chmod u+x myfile.R.
I am a beginner at R. If this question sounds childish to you then pardon me.
I need to run the following command in R studio by using Windows CMD.
copy *.txt combinedfiles.txt
This command actually combines all the files into one file in Windows CMD.
I tried using system() and system2(). But I could not get the results.
e.g.:
system(cmd.exe copy *.txt combinedfiles.txt)
Kindly help me out with this problem.
copy is not a standalone program, but a builtin command of the Windows commandline interpreter. To run this, use shell in R.
shell("copy *.txt combinedfiles.txt")
I have found this answer to a problem I had discussed previously here: How to get dialog/message box working in executable R file
The answer is here:
https://thesquareplanet.com/blog/interactive-r-scripts/
Specifically this bit:
We can use this to provide an interactive R script executor in /usr/local/bin/Rint: #!/bin/sh f=$1; shift; env "R_PROFILE_USER=$f" "ARGS=$#" R --no-save -q
My problem is that I know nothing about the command prompt or batch files. So what does this line mean, and do I just paste it into the command prompt once and then add "#!/usr/local/bin/Rint" to the top of my executable R script?
This line is not for a Windows cmd.exe shell. This is for a UNIX/Linux shell.
We can use this to provide an interactive R script executor in /usr/local/bin/Rint:
#!/bin/sh
f=$1;
shift;
env "R_PROFILE_USER=$f" "ARGS=$#"
R --no-save -q
For anyone still looking, this is the best resource I can find about executable R GUIs in Windows:
http://oddhypothesis.blogspot.com.au/2014/04/deploying-self-contained-r-apps-to.html
Still working through this tutorial, will update if it answers the question.
How can I check documentation for R code from a Linux command shell such as bash? I DO NOT mean an interactive session.
With Perl, I can use perldoc to print out documentation at the command line:
perldoc lib
I was hoping for something simple like that for R. I don't always want to pull up a full interactive R session just to look up some documentation.
There might be other ways, but one that works for me is using the -e flag to execute code on the command line. I also use the --slave flag, which prevents anything from being printed to standard output (e.g. no R startup messages, etc.):
R --slave -e '?function'
I actually created a super small script I call rdoc to act like a simple R version of perldoc:
#!/bin/bash
R --slave -e "?$1"
After installing that in my ~/bin directory (or however you install it in your PATH), it's easy:
rdoc function
If you want to look at documentation of a function from a particular package, prepend the library name followed by two colons. For example, to pull up documentation of the dmrFinder function from the charm package:
rdoc charm::dmrFinder
I am a bit lost when dealing with installing and using R. I installed R 3.0.1 from source and did the ./configure, make, make check, and make install as suggested. However I tried running R but it said that R wasn't in the /usr/bin folder. So I then copied the entire R-3.0.1/bin directory into my /usr/bin directory using cp. Now I'm getting a few errors regarding /usr/bin/env when trying to use RScript on a hello_world.R script I wrote from the O'Reilly R In a Nutshell book I store in a file hello_world.R the contents are below:
#! /usr/bin/env RScript
print("Hello World!");
Simple enough, but when I try to load it I get the following error:
$ ./hello_world.R
/usr/bin/env: RScript: No such file or directory
I'm not sure if this is a PATH problem or something, but when I search in my /usr/bin directory I do see the RScript file in there along with (R, BATCH, and the others associated with R programming language). Any help is greatly appreciated. Cheers.
You may be using an invalid command line option for Rscript in your shebang line.
For instance ...
#!/usr/bin/env RScript --vanilla
remove "--vanilla" (or other offending option) and rerun your script
#!/usr/bin/env RScript
I know you didn't put this in your example, but the solution may help others searching for the same issue.
Again, the good solution to this problem is very simple and clearly explained in the man page of env. The script should use the env command to invoke Rscript and not Rscript directly:
#!/usr/bin/env Rscript
some R code now...
But a script like this will read the user's .Rprofile among other things. When we want to have a vanilla R session (in order to start with a clean and controlled R), we must pass the option --vanilla. If you try something like
#!/usr/bin/env Rscript --vanilla
some R code now...
env will take the string Rscript --vanilla a the command to execute and will inevitably return the error message
/usr/bin/env: ‘Rscript --vanilla’: No such file or directory
In env's man page, there is an option called -S for splitting the strings. Its role is exactly to solve the problem above and use the first string Rscript as the command name, and the following strings (like --vanilla) as options to pass to Rscript.
The solution is therefore:
#!/usr/bin/env -S Rscript --vanilla
some R code now...
Put in the shebang line of your script #!/usr/bin/Rscript and it should work.
As a side remark if you want to keep up-to-date with the R versions from CRAN and not relying on the native R of your Linux distro (Ubuntu) then add the following line in your apt sources:
deb http://my_favorite_cran_mirror/bin/linux/ubuntu raring/
After that you can always use the apt system to install R which -I would agree with Jake above- it should be the preferable way to install R.
*Change the my_favorite_cran_mirror with a valid CRAN mirror that is close to you.
#! /usr/bin/env RScript
print("Hello World!");
Simple enough, but when I try to load it I get the following error:
$ ./hello_world.R
/usr/bin/env: RScript: No such file or directory
Here u make mistake is that instead of RScript write Rscript.
The syntax will be
#! /usr/bin/env Rscript
print("Hello World!");
Then run it it will work (y) all the best.
$./hello_world.R
I arrived at this question trying to understand this error message on a cluster computer where I did not have control over the R installation.
In general, when I converted Rscript in my makefile to /usr/bin/Rscript the error message no longer occurred.