Run R script from command line

前端 未结 7 1617
甜味超标
甜味超标 2020-11-22 09:20

I have a file, called a.r, it has a chmod of 755,

sayHello <- function(){
   print(\'hello\')
}

sayHello()

Ho

相关标签:
7条回答
  • 2020-11-22 09:28

    One more way of running an R script from the command line would be:

    R < scriptName.R --no-save  
    

    or with --save.

    See also What's the best way to use R scripts on the command line (terminal)?.

    0 讨论(0)
  • 2020-11-22 09:29

    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.

    0 讨论(0)
  • 2020-11-22 09:36

    How to run Rmd in command with knitr and rmarkdown by multiple commands and then Upload an HTML file to RPubs

    Here is a example: load two libraries and run a R command

    R -e 'library("rmarkdown");library("knitr");rmarkdown::render("NormalDevconJuly.Rmd")'
    
    R -e 'library("markdown");rpubsUpload("normalDev","NormalDevconJuly.html")'
    
    0 讨论(0)
  • 2020-11-22 09:38

    You need the ?Rscript command to run an R script from the terminal.

    Check out http://stat.ethz.ch/R-manual/R-devel/library/utils/html/Rscript.html

    Example

    ## example #! script for a Unix-alike
    
    #! /path/to/Rscript --vanilla --default-packages=utils
    args <- commandArgs(TRUE)
    res <- try(install.packages(args))
    if(inherits(res, "try-error")) q(status=1) else q()
    
    0 讨论(0)
  • 2020-11-22 09:39

    This does not answer the question directly. But someone may end up here because they want to run a oneliner of R from the terminal. For example, if you just want to install some missing packages and quit, this oneliner can be very convenient. I use it a lot when I suddenly find out that I miss some packages, and I want to install them to where I want.

    • To install to the default location:

      R -e 'install.packages(c("package1", "package2"))'
      
    • To install to a location that requires root privileges:

      R -e 'install.packages(c("package1", "package2"), lib="/usr/local/lib/R/site-library")' 
      
    0 讨论(0)
  • 2020-11-22 09:41

    Just for documentation, sometimes you need to run the script as sudo:

    sudo Rscript path/to/your/file.R
    
    0 讨论(0)
提交回复
热议问题