Run R script from command line

前端 未结 7 1599
甜味超标
甜味超标 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:43

    Yet another way to use Rscript for *Unix systems is Process Substitution.

    Rscript <(zcat a.r)
    # [1] "hello"
    

    Which obviously does the same as the accepted answer, but this allows you to manipulate and run your file without saving it the power of the command line, e.g.:

    Rscript <(sed s/hello/bye/ a.r)
    # [1] "bye"
    

    Similar to Rscript -e "Rcode" it also allows to run without saving into a file. So it could be used in conjunction with scripts that generate R-code, e.g.:

    Rscript <(echo "head(iris,2)")
    # Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    # 1          5.1         3.5          1.4         0.2  setosa
    # 2          4.9         3.0          1.4         0.2  setosa
    
    0 讨论(0)
提交回复
热议问题