Run R script from command line
If you want the output to print to the terminal it is best to use Rscript
Rscript a.R
Note that when using R CMD BATCH a.R
that instead of redirecting output to standard out and displaying on the terminal a new file called a.Rout will be created.
R CMD BATCH a.R
# Check the output
cat a.Rout
One other thing to note about using Rscript is that it doesn't load the methods
package by default which can cause confusion. So if you're relying on anything that methods provides you'll want to load it explicitly in your script.
If you really want to use the ./a.R
way of calling the script you could add an appropriate #!
to the top of the script
#!/usr/bin/env Rscript
sayHello <- function(){
print('hello')
}
sayHello()
I will also note that if you're running on a *unix system there is the useful littler package which provides easy command line piping to R. It may be necessary to use littler to run shiny apps via a script? Further details can be found in this question.
Running R Code from Command Line (Windows)
You want
Rscript.exe
.You can control the output from within the script -- see
sink()
and its documentation.You can access command-arguments via
commandArgs()
.You can control command-line arguments more finely via the getopt and optparse packages.
If everything else fails, consider reading the manuals or contributed documentation
How do I run an R script from within RStudio's built-in R console?
Short Answer using source() function
Once you download, install, and open the RStudio, you'll see a part in the lower left with blue greater than symbols >.
In the part of RStudio's GUI with the blue >
, enter the following
> setwd('/folder/where/the/file/is/')
> source('file_name')`
...output, if any, appears below...
Example:
Let's assume I have a file at /home/myusername/prj/r/learn_r/insurance_data.r
that I want to run.
I would start up RStudio, and enter the following in the little window it has labeled Console:
Annoyingly long answer with screenshots using source() function
Well, it turned out to be much simpler than I expected to run this from RStudio's built-in console. I was surprised that this had not already been asked about RStudio, before. If it has, I guess I'll have a burned question.
Anyway, a little trial and error showed me how to do this:
Yay, output has appeared below.
Make sure to set your working directory, first.
I did this as follows from inside RStudio 1.0.143 on my Ubuntu 16.04 LTS environment:
setwd("~/proj/r/learn_r")
Next, you can enter help(source)
, you can search for the syntax of the source()
function, and you can just type it in to the RStudio console for a prompt:
How to call R script from command line with multiple augument types (inc. list)
@MrFlick deserves credit for this answer. The issue was I was not accounting for a situation where the number of arguments would be greater than 7 (duh).
A quick very fix:
if (length(args) < 7)
{
stop('At least seven arguments must be supplied.', call.=FALSE)
}
if (length(args)==7)
{
project = args[1]
method = args[2]
lib_path = args[3]
storage = args[4]
compo = args[5]
res_file = args[6]
integrated_object = args[7]
}
if (length(args)>7)
{
project = args[1]
method = args[2]
lib_path = args[3]
storage = args[4]
compo = args[5]
res_file = args[6]
integrated_object = args[7:length(args)]
}
Thank you for your eyes @MrFlick
How to run R script without opening R or RStudio?
There are mainly two ways. with using Rscript, like below:
C:\Users\automat7> Rscript app.r
or in some cases, like with shiny or when running a one line script, usually, you can use
R -e "shiny::runApp(address_to_folder, args)"
You may need to add the R's bin folder to your PATH environment variable if you are using Windows.
You can follow the instructions here for that: How to Add a folder to Path environment variable in Windows10
Related Topics
Changing from Upper to Lower Case in Several Data Frames
How to Fix Spaces in Column Names of a Data.Frame (Remove Spaces, Inject Dots)
Extract Rows for the First Occurrence of a Variable in a Data Frame
Sum Across Multiple Columns With Dplyr
How to Declare a Vector of Zeros in R
Add Row to a Data Frame With Total Sum for Each Column
Removing Columns That Are All 0
How to Change Y Axis Limits in Decimal Points in R
Minimum (Or Maximum) Value of Each Row Across Multiple Columns
How to Find the Closest Date to a Given Date
Pass a String as Variable Name in Dplyr::Filter
How to Join (Merge) Data Frames (Inner, Outer, Left, Right)
Why Are These Numbers Not Equal
How Does the 'Prop.Table()' Function Work in R
Saving Output of Confusionmatrix as a .Csv Table
Count Occurrences of Value in a Set of Variables in R (Per Row)