R / Bookdown: specify rmd_files in render_book() function - r

We use bookdown very frequently to write course material at our university. We use rmd_files: within "_bookdown.yaml" to specify the rmd-files to be included in the book. Our bookdown projects tend to become rather long and complex, and _bookdown.yamlends up looking something like this:
rmd_files:
- index.Rmd
- folder1/subfolder1/file1.Rmd
- folder1/subfolder1/file2.Rmd
- folder1/subfolder2/file3.Rmd
- folder2/subfolder3/file4.Rmd
...
Using _bookdown.yaml to specify the files is limiting in the following ways:
it leads to a lot of repetition (e.g. folder1 is repeated for each child element)
the complex structure is implicit and obscure
selecting only a subset of files to be rendered involves manual labour (say I just want to use all files from folder 1)
So we have moved over to depicting our file-structure in a separate yaml file and using helper functions to update _bookdown.yaml according to our needs. This method seems a bit clunky, and it would be much more elegant if we could pass a vector of files to render_book() instead of specifying the rmd-files in _bookdown.yaml. Is that possible?
Note: using render_book(..., preview = TRUE) is not a solution, we would like to re-render the book with a specific set of rmd-files.

Have you read the new Rmarkdown Cookbook
Here is cuts from the book
This WOULD match your directory IF you pull all your .RMDs from the child folders and in the same parent directory as the index.RMD.
If you name the files accordingly, then your book will render in the correct order (numerically). If you do this, you will not have to name every .RMD file in the yaml. You just have to render your index.RMD which gets instructions from the YAML you have saved

You should be able to include any files from anywhere, using this line in your _bookdown.yml (NB: brackets, commas, and indent!)
rmd_files: [
index.Rmd,
folder1/subfolder1/file1.Rmd,
folder1/subfolder1/file2.Rmd,
folder1/subfolder2/file3.Rmd,
older2/subfolder3/file4.Rmd
]

Related

Quarto equivalent to "exclude:" YAML command in distill

In R, I am moving a distill website to quarto website. In distill, I can prevent all the Rmd documents in a directory called "internal" from being knitted by adding this to the _site.yml
exclude:
- internal
The same trick does not work in quarto. Is there a quarto equivalent, somehow to indicate which directories should be excluded during the knitting/rendering process?
Thanks to C Wickham for answering the question:
Yes, you can control which documents are knitted by specifying the render targets. Two options that might work for you are:
Renaming internal/ to _internal/, and relying on the automatic ignoring of files and directories with a prefix of _.
Explicitly, specifying that files in internal/ should not be rendered in _quarto.yml, e.g. (edited from example in docs linked above):
project:
render:
- "*.qmd"
- "!internal/"
The "*.qmd" is necessary to describe which files should be rendered, so if you've got some .Rmd files as well, you might need to add another line with "*.Rmd" to make sure they are rendered.

How to include an external file in a Moodle question with R/exams?

In order to include statistical tables when using R-exams, I know that one can just use the option pages inside the function exams2nops(). But when using exams2moodle() how should one proceed?
In Moodle one can upload a file within a question and add a link to the embedded file. Is it possible to do it through R exams?
You can easily include various kinds of supplementary files in R/exams and then export them to Moodle or other learning management systems. Two steps need to be taken:
While processing the .Rmd or .Rnw exercise the supplementary file(s) need to be created in the current working directory (which is a temporary directory by default). This can either be done be creating the file via some R code or by copying an existing file. The convenience function include_supplement() simplifies this.
The supplementary file then needs to be included as a link in the question text. In .Rmd this would be something like [myfile.pdf](myfile.pdf) and in .Rnw exercises \url{myfile.pdf}.
An example for such an inclusion is the lm exercise template shipped along with the package, see: http://www.R-exams.org/templates/lm/. This exercise creates a .csv file on the fly within R and then includes it.
An example for the include_supplement() function is available in the Rlogo exercise template that copies the R logo image from the R system and then includes it in the exercise. See: http://www.R-exams.org/templates/Rlogo/.
Final comment: For a distribution table it would also be possible to include this directly as an HTML table in Moodle. For example, you could generate suitable Markdown or LaTeX code within the R code of the exercise.

How do I handle randomly selected graphics files in R exams for export to qti and moodle?

I have a spreadsheet of exam questions that I want to use to generate quizzes and exams using R exams, and I'd like to include graphics in some of the questions.
The template here (http://www.r-exams.org/templates/fruit/) begins by defining the images as long base 64 encoded strings as generated by
base64enc::base64encode("file.png")
This seems fine, but if I have a dozen or so images of which I might only want a question to use one, two, or three images selected at random for programmatically generated exercises, how can I avoid including the encoding for all dozen images with every single exercise?
The best I can think of at the moment is to include LaTeX syntax for graphics inclusion in a spreadsheet of possible question options, and as exercises are generated, use regular expressions to find the file names inside the \includegraphics{} commands that will be included, encode those as base 64 strings, and include them in the exercise file, but I'm wondering if there's a way I can do this without writing my own code to parse LaTeX.
First a few clarifications:
The fruit exercises include the images as Base64 strings because the three icons are quite small (12K per icon) and it is convenient to have all information within the Rnw/Rmd exercise without the need to store graphics files separately. It is just one trick that can be nifty and that we wanted to demonstrate.
For more and larger images one could do the same trick but it is probably less convenient. To illustrate how static images can be included in an exercise, the following template is available: http://www.R-exams.org/templates/Rlogo/ It uses the include_supplement() function to declare a certain file as a supplement for the exercises. If this is a graphic it can then be integrated into the exercises via \includegraphics{...} in Rnw exercises and via ![...](...) in Rmd exercises.
Each exercise just has to include the supplements it actually uses (and not all files from which these were sampled). And there is no need to do the Base64 encoding manually. This is done by the exams2xyz(...) functions automatically if needed.
Now for the scenario you describe. Say you have an exercise foo.Rmd in which you want to show one of three static images foo-1.png, foo-2.png, foo-3.png and ask questions about it. Then your R code might do something like:
i <- sample(1:3, 1)
img <- paste0("foo-", i, ".png")
include_supplement(img)
which randomly selects one of the three files and declares it to be an attachment. Then within the question text you would include the image via something like:
![](`r img`)
Caveats:
The code above assumes that the PNG images are located in the same directory as the Rmd exercise itself. If it is in a sub-directory bar/ say, you would need include_supplement(img, dir = "bar") etc.
If this exercise is rendered into HTML then the original file name (foo-1.png or foo-2.png or foo-3.png) would be visible in the HTML source code. This may (or may not) provide a hint for students what the correct answer is. If so, it would be better to include the file with a neutral name, e.g., include_supplement(img, target = "foo.png").
In Rnw exercises the code for including the graphic would be something like: \includegraphics{\Sexpr{img}}.

docfx Shared Content between Markdown Files

In DocFX, I have a block of content which i need to put on all (or most) markdown files. Is there a way to merge or pull the contents of another md file in to the current file? Basically i am trying not to repeat myself and looking for some sort of sharing mechanism.
You can do this with file inclusion which is part of the DocFX flavored markdown (DFM).
DFM adds syntax to include other file parts into current file, the
included file will also be considered as in DFM syntax.

Bookdown: Set Page Breaks

I cannot find out how to make a page break in R bookdown.
With page break I mean that the content within one page is displayed as one website.
By default there is always a page break in front of every new section (like # Chapter 1).
So, if I run render_book each section becomes one html file.
But, if I have a lot of subsections then these sites get very long.
I would like to have page breaks before every subsection (like ## Chapter 1.1).
So far I tried adding \newpage, \pagebreak, --------------- to the .Rmd or to just provide the .Rmd files in the same structure as I would like to have them as .html files.
Either way, the .html files are always created according to the sections.
I think this is specified by the split_by argument of gitbook as documented here.
Sounds like you are using chapter but you want to be using section
The split_by argument specifies how you want to split the HTML output
into multiple pages, and its possible values are:
rmd: use the base filenames of the input Rmd files to create the HTML filenames, e.g., generate chapter3.html for chapter3.Rmd;
none: do not split the HTML file (the book will be a single HTML file);
chapter: split the file by the first-level headers;
section: split the file by the second-level headers;
chapter+number and section+number: similar to chapter and section, but the files will be numbered;

Resources