create R script files, Saving as R script file and opening R script file from command line - r

I am using R from a distant linux server. I want to make an R script file, open an scripts files and edit them. However, I can just use the R command line. I have no idea how I can create an R script file like in RGui, or how to open such files for editing.

There are multiple non graphical editors to edit your file that you have access to in command line, like nano or vim.
To create an Rscript file, simply add #!/usr/bin/env Rscript at the top of your .R file and make it executable.
Also see Run R script from command line

Related

How to "source" (not run line by line) an R script from the Windows command line

I am creating an R script myscript.R which manipulates an excel file by means of XLConnectpackage.
The point is it refers to several external files: the excel file itself and another R file (functions library), so I need to set a working directory in the location of the script (so that relative paths to external files work properly).
I am using the following in my script
new_wd <- dirname(sys.frame(1)$ofile)
setwd(new_wd)
When I source the script from my RStudio it gets the job done. The problem is that the script is to be used by non-programmers, non-Rstutio-users, so I create .bat file (which I want to turn into an .exe one)
"C:\Program Files\R\R-4.0.3\bin\Rscript.exe" "C:\my\path\to\myscript.R"
It executes the script line by line but sys.frame(1) only works when sourcing.
How could I solve it?
Thanx
I have found a solution and it works properly.
From CMD command line or from a .bat file one can add an argument -e to the command, so that you can use a R language.
absolute\path\to\Rscript.exe -e "source('"relative\path\to\myscript.R"')"
It worked for me.
Besides, as Compo commented, I think there's no need for a .exe file, since a .bat does the job.

Running external .rmd files from a .rmd file - R

I want to run an external .rmd file(AKA rstudio notebook file) when I run a specific .rmd file. For example, the project X has a.Rmd, b.Rmd, and c.Rmd files. However, to run c.Rmd I need to run a.Rmd and b.Rmd first. Currently, each time I run c.Rmd I have to run a.Rmd and b.Rmd manually beforehand. Is there a codeline like when I run c.Rmd, it will automatically run a.Rmd and b.Rmd?, for example:
Inside c.Rmd:
execute(a.Rmd)
execute(b.Rmd)
I don't know the difference between an RStudio notebook file and any other R Markdown file, but this would work in the latter:
rmarkdown::render("a.Rmd")
rmarkdown::render("b.Rmd")

How can i execute a jar file and capture the output for use in my R script?

The jar file I am using has an output of a bunch of text files. I would like to use these files for text mining. Rather than execute the jar file separately and then use the output for my R script, I want my R script to initiate the jar execution as well. Is there a way to do this?
Figured how to do this. I used the system() command like so
system ("java -jar tika-app-1.14.jar -t -i test.pdf")

Some linux commands in R

In matlab, we could write down cd, ls to see the file and go to directory. Whether there are some similar commands in R? (easy to find the file i want)
Also, in the terminal, open filename.r could open the file, but what is the command to close the file?
there are setwd, getwd and list.files

Batch R Script - setting the working directory and selecting output folder

I've been digging into several places for 2 simple needs but couldn't find a final answer.
I'm running an R script in batch mode. Not sure whether my solution is the best one but I'm using R CMD BATCH as per http://stat.ethz.ch/R-manual/R-patched/library/utils/html/BATCH.html included in a bat file.
First I'd like to have the directory where the R script is saved set up as the working directory rather than where the bat file is saved.
Secondly I'd like to divert all the output from the R script (csv files and charts) to a specific directory other than the working directory. I cannot find any options for such basic requirement.
The final idea is to be able to run the bat file across different computers no matter where the R script is saved.
Thanks
You don't give code so my answer will be just an advise or what I would do for such job.
Use Rscript.exe it is the way to go for batch script. R CMD is a sort of legacy tool.
You don't need to set or change the working directory. It is a source of problems
You can launch you bat file where you want and within it you go to R script location using cd for example you bat file can be like:
cd R_SCRIPT_PATH
Rscript youscript.R arg1 arg2
You can use one of the script argument as an output directory for your result files. For example inside your script you do somehing like this:
args <- commandArgs(trailingOnly = TRUE)
resultpath <- as.character(args[1])
.....
write.table(res1, file=paste(resultpath,'res1.csv',sep='/')

Resources