Running R script from command line does not execute the code

10,305

You need to call your function inside your script, at this point you are only defining it but never call it! Happens to me too. So add:

test <- function(){
  print("test")
}
test()
Share:
10,305
Admin
Author by

Admin

Updated on June 15, 2022

Comments

  • Admin
    Admin almost 2 years

    I have tried both Rscript and R CMD BATCH.

    For example, if I run this simple R script:

    test <- function(){
      print("test")
    }
    

    by using

    > R CMD BATCH test.R
    

    I get the following test.Rout file:

    R version 3.0.1 (2013-05-16) -- "Good Sport"
    Copyright (C) 2013 The R Foundation for Statistical Computing
    Platform: x86_64-pc-linux-gnu (64-bit)
    
    R is free software and comes with ABSOLUTELY NO WARRANTY.
    You are welcome to redistribute it under certain conditions.
    Type 'license()' or 'licence()' for distribution details.
    
    Natural language support but running in an English locale
    
    R is a collaborative project with many contributors.
    Type 'contributors()' for more information and
    'citation()' on how to cite R or R packages in publications.
    
    Type 'demo()' for some demos, 'help()' for on-line help, or
    'help.start()' for an HTML browser interface to help.
    Type 'q()' to quit R.
    
    [Previously saved workspace restored]
    
    > test <- function(){
    +   print("test")
    + }
    > 
    > proc.time()
      user  system elapsed 
      0.176   0.016   0.186 
    

    I did not see the expected output:

    >[1] "test"
    

    anywhere - either on the command line or in the test.Rout file.

    With Rscript I do not see any output on the command line either.

    I have a long R script that writes a report file under a specific directory if I run it inside RStudio. However, when I run this script using R CMD BATCH I do not see the file generated in the directory. In the corresponding .Rout file I see similar run statements of the individual lines of the code - not the execution result.

    I am using Ubuntu 12.04 LTS.

    What am I doing wrong?

    Thanks!