A "clean"(er) bookdown install - r

The wonderful bookdown example drops all its bits and pieces into the project root directory (for an Rstudio project), and leaves a variety of files there. I know I can move the Rmd files to subfolders, and I know I can direct the output elsewhere, but there are still several .css files, .sh files (these look entirely optional) the (essential) _bookdown.yml file.
Ideally I'd want to "just" have a "report" folder in the project root, and leave the Rmd files, the outputs, and all the subsidiary files somewhere lower down in the folder structure within a project.
How far can I do in this pursuit? Recommendations? I'd welcome suggestions. Thanks,

Related

Using subdirectories with quarto and RStudio

Please consider the following.
I like to structure my R projects with different subdirectories. Using bookdown or quarto seems to make this quite difficult since both like to have their source files (.Rmd or .qmd) in the project directory. I was hoping that this will get easier with quarto but this does not seem the case, although it should be possible according to this post.
Problem description (replicable example)
Start a new quarto book project in RStudio: File > New Project > New Directory > Quarto Book.
This will create several .qmd files in the R project directory. Now create new subdirectories. Starting from the R project directory: scripts/qmd.
If we put all .qmd files from the R project directory into scripts/qmd, we also need adapt their path in the _quarto.yml (e.g., from index.qmd to / scripts/qmd/index.qmd).
After doing so and pressing the Render button, we receive the ERROR: Book contents must include a home page (e.g. index.md).
Putting only the index.qmd file back into the R Project directory (and adapt the _quarto.yml), the book can be rendered.
Question
How can we put all .qmd files into a dedicated subdirectory?
Edit (ignore below)
Originally the below question was also asked but is now answered: quarto does not seem to have a "merge-knit" option like bookdown (see here). This unfortunately makes quarto useless for my use case.
If this is not possible, how can we make sure that object created in the R project directory are accessible for the .qmd files in the subdirectories? (setting execute-dir: project as suggested here did not work for me.
I'm assuming your purpose is just a specific personal preference for file organization.
You can have a large portion of your content in subdirectories (and that's my understanding of what Alison's blog post refers to), but you still need to have your index.qmd in the root. For example, you can have a subdirectory for projects, another one for posts, another one for tutorials, like Alison does.
In any case, one way of achieving what you want is by using pre- and post-render scripts.
You could move all the files from the scripts/qmd directory before rendering and then move them all back afterwards.
pre-render: mv scripts/qmd/*.qmd .
post-render: mv *.qmd scripts/qmd
Incidentally, that doesn't match my own personal preference ;-)
#Lucas A. Meyer, I tested the pre-render and post render options you suggested above, however, it throws an error after moving the file from my subdirectory to the project root:
ERROR: NotFound: The system cannot find the file specified. (os error 2)
Then, it fails to render the file and move it back to the original subdirectory location.
I then set execute-dir: project in my .yml file thinking it was trying to execute in the wrong folder, but it did not change anything.
P.S. I would have added as a comment, but my reputation is not high enough, yet. :(
UPDATE1:
I thinking I found a workaround for the time being. I've include the following project options beneath the project: header:
output-dir: subdir1/subdir2 Moves the html file, only.
post-render: xcopy *_files subdir1/subdir2 /E Copies the supporting files over.
The mv shell command can't be used for directories or you get the Directory Not Empty error.`

Use the same styles.css file for multiple Rmarkdown projects

I have a styles.css file (which also refer to a logo png file) I use for my Rmarkdown documents that I'd like to be available to all my projects. My current workflow is to copy it to each of my repo, but that also means I have to update all my repos when I want to make a change.
Any ideas? Might be as simple as hosting it in a S3, but I'm not sure that rmarkdown allows that.

How do I use classes from files in another directory in R?

I have a bunch of .R files in a directory (classes with inheritance). These files depend on each other.
I need to use these files' classes in a file in a different directory.
Is there a way to directly call their functions/classes from a file in a different location? Maybe by adding them to some kind of path? I was thinking of including each source file manually but they are numerous.

address project root in Rstudio

When you open or create a project in RStudio, the working directory is automatically changed to that of the project. However, if you are saving files or doing work in other subdirectories, it's often convenient to change the working directory then. It can get to be a pain to manage this in every script.
Is there a variable that always points to the project root (that is also readable by the session forked by the "Knit HTML" button) that can be used to make this easier?
The "Knit HTML" button appears to set the working directory to that of the R Markdown file. For example, if you have a variable called project.root in your ./Rprofile, and you click "Knit HTML" with this script opened,
```{r}
getwd()
source('./Rprofile')
setwd(project.root)
getwd()
```
the first and last result will be the script directory and the rest will throw errors.
To recap, as you inferred, you can set R's working directory at the command line like so:
setwd("~/NateProjects")
You can also use RStudio's Files pane to navigate to a directory and then set it as working directory from the menu: Session --> Set Working Directory --> To Files Pane Location. (You'll see even more options there). Or within the Files pane, choose More and Set As Working Directory.
But, you can do better & set up an environment like a professional programmer. To do this, you can choose to keep all the files associated with a project (a project being loosely defined as I have all my personal code under one project) organized together -- input data, R scripts, analytical results, figures. In fact, RStudio has built-in support for this. There is an excellent tutorial here that you can have a look at which explains how to walk through in step by step detail:
http://www.rstudio.com/ide/docs/using/projects
Essentially, taking from the documents, you need to do the following:
Create a new project use the Create Project command (available on the Projects menu and on the global toolbar). This can be the place that you put all future sub projects and sub-folders in
Then, once you create this new project in RStudio, the following actions happen:
You create a project file (with an .Rproj extension) within the project directory. This file contains various project options (discussed below) and can also be used as a shortcut for opening the project directly from the filesystem.
You create a hidden directory (named .Rproj.user) where project-specific temporary files (e.g. auto-saved source documents, window-state, etc.) are stored. This directory is also automatically added to .Rbuildignore, .gitignore, etc. if required.
You load the project into RStudio and display its name in the Projects toolbar (which is located on the far right side of the main toolbar)

How to ignore symlinks or directories recursively in kdevelop?

I am using KDevelop 4.2.3 and in my project directory there are symbolic links to a large collection of test instances. I want to tell kdevelop to ignore these directories. There is the possibility to put a file named .kdev_ignore into the directory that should be ignored. Unfortunately this feature is not propagated into subdirectories.
So, is there any way to exclude a directory and all of its subdirectories without putting .kdev_ignore files everywhere?
Update with Makefile (not CMake):
I also found a four year old Makefile symlink recursive bug which was originally patched in Kdevelop 3.5 (plus a possible second reference to the same issue).
If you decide to file a new bug report, and personally I think that you should, you should reference those two links to demonstrate regression.

Resources