littler (pronounced little r) (cran) provides, besides other features, two possibilities to run R scripts from the command line with littler's r
command (when one works with Linux or MacOS).
install.packages("littler")
The path of r
is printed in the terminal, like
You could link to the 'r' binary installed in
'/home/*USER*/R/x86_64-pc-linux-gnu-library/3.4/littler/bin/r'
from '/usr/local/bin' in order to use 'r' for scripting.
To be able to call r
from the system's command line, a symlink is needed:
ln -s /home/*USER*/R/x86_64-pc-linux-gnu-library/3.4/littler/bin/r /usr/local/bin/r
sudo apt-get install littler
With r
from littler
it is possible to execute standalone R scripts without any changes to the script.
Example script:
# User message (\n = end the line)
cat("Input numbers, separated by space:\n")
# Read user input as one string (n=1 -> Read only one line)
input <- readLines(file('stdin'), n=1)
# Split the string at each space (\\s == any space)
input <- strsplit(input, "\\s")[[1]]
# convert the obtained vector of strings to numbers
input <- as.numeric(input)
# Open the output picture file
png("hist.png",width=400, height=300)
# Draw the histogram
hist(input)
# Close the output file
dev.off()
Note that no shebang is at the top of the scripts. When saved as for example hist.r
, it is directly callable from the system command:
r hist.r
It is also possible to create executable R scripts with littler, with the use of the shebang
#!/usr/bin/env r
at the top of the script. The corresponding R script has to be made executable with chmod +X /path/to/script.r
and is directly callable from the system terminal.