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.
Related
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
]
I am creating a bookdown document in which I provide a link for people to download the PDF, DOCX and TEX outputs of the current section they are looking at.
All output documents are in a folder named "Compilation" and have the same name of the original Rmd documents they were retrieved.
For exemple, I have the file "1.2-Endowment-effect.Rmd" in which I coded:
Download Links: [[PDF]](./Compilation/1.2-Endowment-effect.pdf)
[[DOCX]](./Compilation/1.2-Endowment-effect.docx)
[[TEX]](./Compilation/1.2-Endowment-effect.tex)
I wanted to know if, instead of writting "1.2-Endowment-effect" in the code, there would be a way to take the name of the current Rmd file and add the extension. Something like:
Download Links: [[PDF]](./Compilation/NameCurrentFile.pdf)
[[DOCX]](./Compilation/NameCurrentFile.docx)
[[TEX]](./Compilation/NameCurrentFile.tex)
I have to repeat that process for lots of Rmd files, and I want to avoid always updating the links.
Thank you for your help.
The output filenames can be generated with inline R expressions, e.g.,
Download Links: [PDF](./Compilation/`r xfun::with_ext(knitr::current_input(), 'pdf'`)
Please read the help pages of the functions xfun::with_ext() and knitr::current_input() if you are not familiar with them.
To include this part in all other Rmd documents, you can put the content in a child document named, say, _download.Rmd, and in your main Rmd documents, use
```{r, child='_download.Rmd'}
```
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.
I have used file.create and file.append successfully to aggregate multiple .txt files. When I try it with .rtf files, however, I get a larger rtf file that only shows the contents of the first .rtf of many to be aggregated.
So I have 5 .rtf files, for example. dirFiles is the list of names to be aggregated:
file.create(fileCollection_r)
file.append(fileCollection_r,dirFiles_r)
Is this a bug, and I would I report it?
How can I aggregate multiple .rtf files?
First of all, it is not clear what file.create() resp. append is doing. You didn't tag for a specific programming language, so that part of your question is really unclear and you need to improve that.
Having said that: RTF files, are in the end, pure text files. They contain formatting information, such as
{\rtf1\ansi\ansicpg1252\cocoartf1671\cocoasubrtf100
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
So, theoretically, you can just pull that text content from multiple RTF files, and put all of that into a single file.
Thus: simple use a file viewer, such as less, cat, or some windows/macos pendant, and A) check the textual content of your single RTF files and B) check out the textual content of the file that you created this way. That will tell you if the pure textual append did work.
But beyond that: it could very well be that the RTF format itself has certain limitations, that simply make it not possible to just append arbitrary RTF file content and end up with something that works as a correct RTF document.
For example I have a markdown file called index.md in the root folder and another markdown file in a subfolder called docs/tools/git.md. When I run render_site(), index.html is generated from index.md, but no document is generated from docs/tools/git.md.
The help page of rmardown::render_site says:
R Markdown (.Rmd) and plain markdown (.md) files in the root directory are rendered.
I have tried to add include: ["docs/tools/git.md"] to the _site.yml but this doesn't help.
This question is related, but I already know how to work with sub directories in knitr.
What I want to know is how to render documents in sub-directories with rmarkdown::render_site()?
It is not possible to have sub-directories with rmardown::render_site() according to the author Yihui Xie who referred to the Rmarkdown website page of the bookdown documentation in a comment above:
"Before blogdown was invented, there was actually a relatively simple way to render websites using rmarkdown. The structure of the website has to be a flat directory of Rmd files (no subdirectories for Rmd files) and a configuration file in which you can specify a navigation bar for all your pages and output format options."
It goes on to specify that these simple sites are still useful in some cases. The first two points:
"You are familiar with generating single-page HTML output from R Markdown, and all you want is to extend this to generating multiple pages from multiple Rmd files."
"It suffices to use a flat directory of Rmd files. You do not write a blog or need RSS feeds."