Search code examples
rrscriptopenxlsx

R script to arguments


currently, in this code below, there is manual intervention I want to define the path of 2 input files. but then I am not sure how to do it

library("openxlsx")
nm=readline("Enter  data file name:")                
readline("Enter input file name: (Press Enter)")        
input_file=read.xlsx(file.choose())

I want to add paths to line 3,4 to arguments so far I have tried this but it isn't correct

library("openxlsx")
args = commandArgs(trailingOnly=TRUE)
nm=C:\Program Files\R-3.5.2\bin\tdd_data.xlsx
input_file=C:\Program Files\R-3.5.2\bin\tdd_rinput.xlsx

when I execute the code it gives me an error

***Error in source("tddarg.r") : tddarg.r:5:6: unexpected '/'
4: args = commandArgs(trailingOnly=TRUE)
5: nm=C:/*** 

Solution

  • you can make some changes and get exactly what you want.

    nm="C:/Program Files/R/R-3.5.2/bin/tdd_data1.xlsx"              
    input_file=read.xlsx("C:/Program Files/R/R-3.5.2/bin/tdd_rinput2.xlsx")
    

    I've only changed the slashes and added the read function to it.