Running external .rmd files from a .rmd file - R - 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")

Related

How to get here() to work with R markdown/Quarto

Hoping for some help - I have been banging my head for a few hours and I can't find a solution. I am trying to optimise a workflow which involves sourcing a script from a Quarto file (I imagine this would be the same for an R markdown file). If I run the actual script using here() to load csv files, it works perfectly and sets the root directory as:
here() starts at /Users/Jobs/2023/project_x
Which is where the R project file is located.
But if I source that same script from within a Quarto file it sets the root directory to one up from the folder containing the .qmd file, and prevents the csv files from being able to be read:
here() starts at /Users/Jobs/2023/project_x/Analysis/code
The .qmd file is located at:
here() starts at /Users/Jobs/2023/project_x/Analysis/code/rmd
Is this expected behaviour and can I get around it?
Using:
here::set_here("/Users/Jobs/2023/project_x")
in a .qmd code chunk seems to work and keep all paths using here() intact in the source script, but I don't think it's ideal as I still need to specify an absolute path in the first instance.
Open to other suggestions...

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.

Access R file functions from .Rmd file

I'm new in R and Rstudio and I'm making a little project. The fact is that i have on one hand an .R file with the code I want to execute. And on the other hand I've an .Rmd file that I should use to report my work, including the results of the execution of my code in the other file.
How can I access the results and/or functions from de .Rmd file to the .R file?
Thank you,
By default, your .Rmd file will have its working directory as wherever the .Rmd file is saved. You can use all of R's standard functions inside the .Rmd file, including source() to run a .R file. So if your files are in the same directory, you can include source("your_r_file.R") to run the .R file. If they are in different directories, you can use relative or absolute file paths (though you should try to avoid absolute file paths in case the .Rmd file is ever run on a different computer).
If you are using RStudio, I would strongly recommend using the "Projects" feature and the here package. The readme for the here package is quite good for explaining its benefits.
Source the R file in at the top of your .Rmd file like
```{r}
source("file-name.R")
```
and the functions/objects in that R file will be avilable

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

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

Automatically update PDF as I write .Rnw file with knitr

I'm writing a .Rnw file in TextMate when using knitr to convert this file to a .tex file, then converting the .tex file a PDF. I'm wondering if there are any tools available that will automate this process, ie everytime anything updates in the .Rnw file, the PDF also updates? Using knitr to combine R and LaTeX code is great, but I think the conversion process to PDF takes up a lot of time, especially if changes to the .Rnw file are small but need to be cross-checked in the PDF file everytime they are made.
If you are on Linux use inotifywait for that. Most distributions have the package inotify-tools
Then do something like
while inotifywait -e close_write myfile.Rnw; do compile_myfile.sh; done
or
while inotifywait -e delete_self myfile.Rnw; do compile_myfile.sh; done
It depends on how your editor actually saves files. Just try and read the man page.
You have to write compile_myfile.sh of course to compile the .Rnw to .pdf - but I guess you know the commands.

Resources