Find the right path of child document in r markdown - r

I got one code from a colleague, and I want to modify it for my purpose. It's a bookdown project. In the index.Rmd file, there is a code
r child = here::here("prelims", "00--prelim.Rmd")
This means, in the working directory, there is a folder called "prelims" and the child document "00-prelims.Rmd" exists in that "prelims" folder. I can knit the index.Rmd document with this code, when this index.Rmd is part of a folder. But if I have created an RStudio project, then the location of "00-prelims.Rmd" changes.
If projectname.Rproj file exists, then the location of the "00-prelims.Rmd becomes here::here("projectname","prelims","00-prelims.Rmd").
So, I have tried the following
r child = ifelse(file.exists(Sys.glob("*.Rproj")),here::here(sub("\\..*", "", list.files(pattern = "\\.Rproj$")),"prelims", "00-prelims.Rmd"),here::here("prelims", "00-prelims.Rmd"))
But it gives me logicl(0) error. How can I get the right path of the "00-prelims" file every time?

Related

Changing WD in RMarkdown

My R Markdown script was running well until I had to access documents from another WD than my RMarkdown-file. I tried to change the WD. But it doesn't take it.
I might access documents from different folder during my R Markdown Document. How can I be more flexible without coping my files into R Markdown folders? (that would be wasting space!)
Is this the right command?
knitr::opts_knit$set(root.dir = "C:/Users/Nadine/OneDrive/ZID_Kurse/Einführung/Kursmaterial")
And do I need to put it in the beginning of the document???
It just halts at the chunk with setWD() command.
Cheers,
Nadine
You probably do not need to change the working directory; just explain where to find the files relative to the project working directory. You can use an absolute path to the files on the filesystem, e.g.
list.files("C:/projects/another_project/data/")
Or you may try to use relative paths to navigate to files through the parent directory. e.g.
list.files("../another_project/data/")

How to import an external dataset into in a Moodle question?

I would like to import an external dataset using read.table() (or any other function for reading files) and then randomize or sample over it. The file is stored in a subfolder within the parent folder that contains the exercises *.rmd. I am working within a RStudio project. I tried placing the dataset in different levels of the folder structure. Using relative path did not work, but absolute paths did.
My folder structure is:
$home/project_name/exercises # It contains the RMD files
$home/project_name/exercises/data # It contains data files that I want to process
$home/project_name/datasets # this folder could eventually contain the dataset I want to process
To make this code more portable, I would like to know o the manage relative paths within *.Rmd for the knitting process.
The exercises are copied to a temporary directory and processed there. Hence, the easiest option is to copy these files to the temporary directory using include_supplement("file.csv"). By default this assumes that the file.csv is in the same directory that the exercise itself resides in. If it is in a subdirectory you can use include_supplement("file.csv", recursive = TRUE) and then subdirectories are searched recursively for file.csv.
After using include_supplement(), the copied file is available locally and can be processed with read.table() or also included in the exercise as a supplementary file. See http://www.R-exams.org/templates/Rlogo/ for a worked example. However, note that the Rlogo template explicitly specifies the directory from which the file should be copied. This is not necessary if that directory is the same as the exercise or a subdirectory.

here::here Pulls from Incorrect Directory and Ignores .here File in R

I am having trouble loading my .csv files with here::here.
The here package is setting its default directory as:
'Z:/PSE/Analysis/One'
My working directory is:
Z:/PSE and I have put a .here file in that directory. Here is the message I get after using dr_here():
Here() starts at Z:/PSE/Analysis/One, because it contains a file matching [.]Rproj$ with contents matching ^Version: in the first line
I thought the here function started at the current working directory, which is Z:/PSE, in which it should recognize the .here file and follow the path that I specify in the remainder of the here function?

R - Knit HTML output in wrong directory

When I knit my .Rmd file to HTML, instead of saving the output to the same directory as the .Rmd file, RStudio saves the .html file and a copy of the original .Rmd file to my home directory instead of my working directory. Any idea how to fix this? Purely for organization purposes I'd like the output to be in the same directory as the oringinal .Rmd file.
I'd suggest using Dean Aatali's ezknitr package, which he wrote to correct this and a few other infelicities of knitr's input and output directory defaults. ezknitr::ezknit() "just works" as you'd like it to, writing its outputted HTML file to the directory contain the markdown file, instead of (as knitr::knit() would) to the current working directory:
## Reproducible e.g.
dir.create("path/to", recursive=TRUE)
cat("Hello world", "path/to/eg.Rmd")
## Check how it works
ezknitr::ezknit("path/to/eg.Rmd")
dir("path/to/")
[1] "eg.html" "eg.md" "eg.Rmd"

How can I avoid hardcoding a file path?

I am using RStudio to knit an .Rnw file to .pdf. This .Rnw file is stored in directory that is under git version control. This directory also contains a .RProj file for the project.
I collaborate with colleagues who don't know the first thing about .Rnw files and git. These colleagues want to open a Word file and track change their hearts out. So I give the people what they want.
Everyone needs access, so storing the Word file on a cloud service like Box makes sense. In the past I created a subfolder in my repo that I shared—keeping everything within the root directory—but this time around I needed to store the file in a shared folder that someone else created. So my solution was to copy the Word file from this shared directory to my repository.
Technical Approach
I don't know how to make this a reproducible problem, but hopefully you will give me some latitude since I'm trying to make my work fully reproducible ;)
Let's say that my .Rnw file is stored in repoRoot/subfolder. Since knitr changes the working directory to subfolder where this .Rnw file is located, the first chunk sets the root.dir one level up at the project root.
<<knitr, include=FALSE>>=
library(knitr)
opts_knit$set(root.dir=normalizePath('../')) # go up 1 level
#
The next chunk copies the Word file from the shared folder to my git repo and runs the analysis file. The shared directory path is hard coded to my machine, which is the problem I'm writing for your help solving.
file.copy(from='/Users/ericpgreen/Box Sync/Project/Paper/draft.docx',
to='subfolder/draft.docx', # my repo
overwrite=TRUE)
source(scripts/analysis.R) # generates objects we reference in the .docx file
After adding \begin{document}, I include a chunk where I convert the .docx file to .txt and then rename it to .Rnw.
# convert docx to txt
system("textutil -convert txt 'subfolder/draft.docx'")
# rename txt to .Rnw
file.rename('subfolder/draft.txt',
'subfolder/draft.Rnw')
The next child chunk calls this .Rnw file that contains the text of the Word file with references to R objects included through \Sexpr{}:
<<include-draft, child='draft.Rnw', include=FALSE>>=
#
This works just fine for me. Whenever I knit the .Rnw file it grabs the latest version of the .docx file that my colleagues have edited (complete with track changes and comments) and, in a later step not shown here, returns the .pdf file to the shared folder.
Problem to Solve
This setup meets almost every need for me, except that the initial file.copy() command is hard coded to my machine. So if someone in my group clones my repo (e.g., research assistants who DO use version control), it won't run out of the box. Is there a workaround to hard coding in this type of case?
Ultimately you won’t get around hard-coding paths that are outside your control, such as paths to network shares. What you can and should avoid is hard-coding these paths in your documents.
Instead, relegate them to configuration files and/or environment variables (which, again, will be controlle by configuration files, to with .bashrc and similar). The simplest approach is then to use
network_share_path = Sys.getenv('NETWORK_SHARE_PATH',
stop('no network share path configured'))
file.copy(from = network_share_path, to = 'subfolder/draft.docx', overwrite = TRUE)

Resources