I am aiming to write an R script that would take the user path for the file and output name of the file. This will be followed by its processing and then output being stored in that file.
Normally, if I had to break this code on R studio it will look like this:
d<- read.table("data.txt" , header =T)
r<-summary(d)
print(r)
the output that is being displayed is also to be written in output file.
where data.txt is
1
2
3
4
45
58
10
What I would like to do is to put the code in a file called script.R and then run it as follows
R script.R input_file_path_name output_file_name
Could anyone spare a minute or two and help me out.
Many thanks in advance.
The most natural way to pass arguments from the command line is to use the function commandArgs. This function scans the arguments which have been supplied when the current R session was invoked. So creating a script named sillyScript.R which starts with
#!/usr/bin/env Rscript
args = commandArgs(trailingOnly=TRUE)
and running the following command line
Rscript --vanilla sillyScript.R iris.txt out.txt
will create a string vector args which contains the entries iris.txt and out.txt.
Use the args[1] and args[2] vector as the input and outputfile paths.
https://www.r-bloggers.com/passing-arguments-to-an-r-script-from-command-lines/
You can consider this method:
script<-function(input_file){
input_file<-read.table("data.txt", header=T)
r<- summary(input_file)
return(r)}
If you want to manually choose the input file location you can use:
read.table(file.choose(),header=T)
Just executing the script with the input name is sufficient to return the desired output. For eg,
output_file<-script(input_file)
If you also want to export the data from R, you can modify the script as follows:
script<-function(input_file,output_file_name){
input_file<-read.table("data.txt", header=T)
r<- summary(input_file)
write.table(r,paste0(output_file_name,".txt",collapse="")
return(r)}
You have to use file name within inverted commas:
> vector<- script(input_file,"output_file_name")
By default, the output will be exported to current R directory. You can also specify output file location by putting the file location before output file_name within the script:
write.table(r,paste0("output_location",output_file_name,".txt",collapse="")
Related
I have a function which takes multiple csv files and processes them into an excel file.
#review.R
review <- function(... ,savename) {
somecodes
}
and I have these files in my folder:
fileA.csv
fileB.csv
fileC.csv
fileD.csv
...
and this is how I run it:
review("fileA","fileB","fileC","fileD", savename="analysis")
And then it processes and outputs "analysis.xlsx"
I have no problem running it in RStudio, but I really would like to run my script in cmd line like this:
rscript.exe f_wrapper.r "fileA" "fileB" "fileC" "fileD" savename="analysis"
This is my f_wrapper.R
#f_wrapper.R
#this script doesn't work at all
args <- commandArgs(TRUE)
obj <- list(...)
source("my_R_folder/review.R")
review(obj)
I googled all over but all I could find was passing fixed arguments like a,b,c but I am trying to pass a,b,c,d,e .... and more arguments to my function.
Please help.
Just a question... supposing I have the following R file, called script.R:
calculation1 <- function(){
# multiple calculations goes here.
}
calculation2 <- function(){
# multiple calculations goes here.
}
And I want to open this file on another file in order to use calculation1 and calculation2 multiple times in different cases.
Is that possible? I mean, something like the following.
calculation_script <- Some way, function or method that instantiate script.R in this variable.
calculation_script.calculation1()
calculation_script.calculation2()
I need to do this because I need to open script.R file in a Tableau Calculated field.
I have a directory made of 50 files, here's an excerpt about how the files are names :
input1.txt
input2.txt
input3.txt
input4.txt
I'm writing the script in R but I'm using bash commands inside it using "system"
I have a system command X that takes one file and outputs it to one file
example :
X input1.txt output1.txt
I want input1.txt to output to output1.txt, input2.txt to output to output2.txt etc..
I've been trying this:
for(i in 1:50)
{
setwd("outputdir");
create.file(paste("output",i,".txt",sep=""));
setwd("homedir");
system(paste("/usr/local/bin/command" , paste("input",i,".txt",sep=""),paste("/outputdir/output",i,".txt",sep="")));
}
What am I doing wrong? I'm getting an error at the line of system , it says incorrect string constant , I don't get it.. Did I apply the system command in a wrong manner?
Is there a way to get all the input files and output files without going through the paste command to get them inside system?
There is a pretty easy method in R to copy files to a new directory without using the system commands. This also has the benefit of being cross-capable on different operating systems (just have to change the files structures).
Modified code from: "Copying files with R" by Amy Whitehead
Using your method of running for files 1:50 I have some psudocode here. You will need to change the current.folder and new.folder to something
# identify the folders
current.folder <- "/usr/local/bin/command"
new.folder <- "/outputdir/output"
# find the files that you want
i <- 1:50
# Instead of looping we can use vector pasting to get multiple results at once!
inputfiles <- paste0(current.folder,"/input",i,".txt")
outputfiles <- paste0(new.folder,"/output",i,".txt")
# copy the files to the new folder
file.copy(inputfiles, outputfiles)
In R, I would like to read in data from a file, then do a bunch of stuff, then write out data to another file. I can do that. But I'd like to have the two files have similar names automatically.
e.g. if I create a file params1.R I can read it in with
source("c:\\personal\\consults\\ElwinWu\\params1.R")
then do a lot of stuff
then write out a resulting table with write.table and a filename similar to above, except with output1 instead of params1.
But I will be doing this with many different params files, and I can foresee making careless mistakes of not changing the output file to match the params file. Is there a way to automate this?
That is, set the number for output to match the number for params?
thanks
Peter
If your source file always contains "params" which you want to change to "output" then you can easilly do this with gsub:
source(file <- "c:\\personal\\consults\\ElwinWu\\params1.R")
### some stuff
write.table(youroutput, gsub("params","output",file) )
# Will write in "c:\\personal\\consults\\ElwinWu\\output1.R"
Edit:
Or to get .txt as filetype:
write.table(youroutput, gsub(".R",".txt",gsub("params","output",file)))
# Will output in c:\\personal\\consults\\ElwinWu\\output1.txt"
Edit2:
And a loop for 20 param files then would be:
n <- 20 # number of files
for (i in 1:n)
{
source(file <- paste("c:\\personal\\consults\\ElwinWu\\params",i,".R",sep=""))
### some stuff
write(youroutput, gsub(".R",".txt",gsub("params","output",file)))
}
If the idea is just to make sure that all the outputs go in the same directory as the input then try this:
source(file <- "c:\\personal\\consults\\ElwinWu\\params1.R")
old.dir <- setwd(dirname(file))
write.table(...whatever..., file = "output1.dat")
write.table(...whatever..., file = "output2.dat")
setwd(old.dir)
If you don't need to preserve the initial directory you can omit the last line.
I'm working on a script in R that processes some data and writes an output file. I'd like that output file to be named in a way that reflects the input file, and I'd like something about the file to be unique so older files aren't overwritten.
So I thought to use a timestamp. But this isn't working the way I'd hoped, and I'd like to understand what's happening and how to do this correctly.
This is how I'm trying to name the file (file_base is the name of the input file):
now<-format(Sys.time(), "%b%d%H%M%S")
outputfile<-cat(file_base, "-",now,"-output.txt", sep="")
The output of this pair of functions looks great. But executing 'outputfile' subsequently results in 'NULL' as output.
What's happening here and how can I create an output filename with the properties that I'd like?
You're confusing cat and paste. You want:
outputfile <- paste(file_base, "-",now,"-output.txt", sep="")
You can also use the function sprintf(), it's a wrapper for the C function.
example:
filepath <- file.path(outdir, sprintf("abcdefg_%s.rda", name))
You could also use the separator argument of paste:
outputfile <- paste(file_base,now,"output.txt", sep="-")