slidify lectureSite: workflow and customizations - r

I am getting started with http://slidify.github.io/lectureSite/. I like the idea and the template very much. However, being new to this, I am struggling with customization.
Let me first explain what this 'lectureSite' architecture offers. The main code is based on rmarkdown and slidify and the customizations of the layout are mainly controlled in css files. The 'lectureSite' consists of an html webpage that serves as a contents page and starting point to access different slides ('lectures'), much of it created automagically. It looks as great as it sounds!
Each lecture is stored in a separate directory with its own 'assets' directory. So in a course made of 10 lectures, one has 10 directories, each with its own css and js. However, to achieve a common style for the whole course, one would like most of the customizations to be shared among all the lectures. My main problem is how to deal with this.
Question: How should I deal with the css customizations that I would like to share among all the lectures?
I have been successful in customizing each lecture by inserting the css code into each rmarkdown file between <style> and </style> tags, but now I would like to take the customizations to a place where they may be shared. I expected that by including a slidify.css file inside assets/css the styles would be picked up, but they are not: could I be doing something wrong or was my expectation incorrect to begin with? And besides, there are so many of those assets/css directories that it would be tedious to have to copy the css into each every time it is modified. Is there a mechanism to set a single css file that would override the css inside assets/css?
I also tried to make a 'declaration' at the top of the rmarkdown file (something I saw there: http://rmarkdown.rstudio.com/html_document_format.html) with:
css : slidify.css
but that gave the following error message:
pandoc: Could not fetch slidify.css
slidify.css: openBinaryFile: does not exist (No such file or directory)
Error: pandoc document conversion failed with error 67
Execution halted
An alternative would be to source() a text file with the customization between <style> and </style> tags. Are there any disadvantages to this approach? And btw what is the code for sourcing an external file from rmarkdown?

Consider the following structure:
assets -> put custom img/js/css/layout assets
lectures -> folder containing lectures
Lecture 01
Lecture 02
Lecture 03
libraries -> frameworks, highlighters and widgets
index.Rmd -> Rmd source for home page
site.yml -> Site related configuration
and suppose you are inside one of the Lectures index.Rmd files. I found the following seems to 'by-pass' the local assets directory to source the 'root' directory instead:
url : {lib: "../../libraries", assets: "../../assets"}
If you place your css files into "../../assets/css/custom.css", that is the 'master' assets directory at the top of the structure (at the root, to say it differently), then it will be sourced by slidify. I deleted all the other assets directories and didn't find it did any harm (all they contained to begin with was one single file ribbons.css). Not extensively tested, but it worked on Firefox and Chrome.
In this way, I can have a single customization css for all the chapters.
However, this structure appears to work only for the html5slides framework and not for io2012. For io2012 I explain here what I did (it worked for me, but I have no idea if it's the right way):
https://github.com/ramnathv/slidify/issues/409
Somewhat more involved. Hopefully future versions of slidify will make it easier. One problem with dumping style files into the root assets directory is that, with the html5slides framework, all files are sourced. So my idea of having framework-specific css files in the assets directory doesn't work, because the styles are all sourced and some overwrite one another. So keeping the custom css files in framework-specific directories is probably a better approach anyhow.
I also found the following way of sharing R customizations and knit opts (or whatever they're called)
```{r 'preamble', message = FALSE, warning = FALSE, error = FALSE, echo = FALSE, tidy = FALSE, comment = NA, cache = FALSE} # probably several redundant ones in there
require(knitr)
opts_chunk$set(echo = FALSE, cache = FALSE) # example of knit options
source('../../shared/shared.R') # here I share common R code
```

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.

choose working directory in code option of r chunk

So I am using RMarkdown and I am sourcing code from a script contained in a separate folder (Scripts) at the same level as the folder which contains the Markdown file (Report). Both directories are at the root of the R project.
I achieve that first by setting the working directly using the following code in the setup chunk:
knitr::opts_knit$set(root.dir = normalizePath(".."))
Then calling the following in a R chunk:
```{r datacreation, echo = FALSE}
source("./Scripts/20212022Summary.R")
```
All that is great but I also want to display later on in the knitted document, the whole code contained in that script so it can be reviewed if necessary.
To achieve this, I do :
```{r 20212022Summary_readlinesversion, code = readLines("../Scripts/20212022Summary.R"),echo=TRUE,include = knitr::is_html_output(),cache=FALSE,eval = FALSE}```
Which just shows the code without running it, with a nice formatting.
What I would like to know is how I would go about defining once and for all what is my working directory for consistency sake. Specifically when I call code = readLines("../Scripts/20212022Summary.R") I clearly operate with the knowledge it will consider the working directory to be the one containing the report instead of the one (parent directly aka project root directory) which I defined previously in the set up chunk.
Ideally I would define things once and for all, especially as then I could pick those up programmatically and source from anywhere within the project folder, consistently.
Anyone has tips on how to achieve this? What I have done works, it's just ugly, manual and not consistent. And it bugs me :)

R / Bookdown: specify rmd_files in render_book() function

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
]

How do I assign the working directory for rmarkdown/knitr interactive doc on shinyApps.io?

I have a fully functional interactive shiny doc using knitr/r markdwon.
However, when I try to publish (deploy) it to shinyApps.io, I get an error saying the .PNG file I try to incorporate into the doc (using readPNG from the png package) is unable to open.
I know the problem is related to working directories.
In my original code I assigned a working directory to my folder (i.e., "C:/Users/NAME/documents/...." that contains both my .rmd file and my .png file. However, obviously my folder doesn't exist on the shinyapps.io.
So how exactly do I set my working directory to open the .png file via my doc on shinyapps.io?
I can't find anywhere that explicitly walks through this process. Please help explain this simply/basically for me.
Example:
Assume I have a folder in "C:/Users/NAME/documents/Shiny" that contains 2 files: "shiny.rmd" and "pic.png". I want "pic.png" to be rendered in my shiny.rmd doc.
Currently I have:
---
title: "TroubleShoot"
output: html_document
runtime: shiny
---
```{r ,echo=FALSE,eval=TRUE}
library(png)
library(grid)
direct <- "C:/Users/NAME/documents/Shiny/"
img <- readPNG(paste0(direct,"pic.PNG"))
grid.raster(img)
```
How do I rewrite my code so it works on shinyApps.io?
Do I need to create and name folders in specific ways to place my 2 files into before deploying?
When you paste the c drive to direct, and read the direct into img, you are sending the app/markdown to your local drive. You need to use a relative path to the project. I would suggest something like:
direct <- "./"
Which uses the relative path, not the absolute path.
That said you might be able to skip that step entirely if the .png is in the same folder as the shiny object. just call the file name and format and leave it as that.
Also, check you use of ". Your syntax highlighting indicates your code won't run properly as it thinks some of your functions and variables are character strings because you've misplaced your quotes around read.png(). That's probably just a copy and paste typo though.
In summary, call the image via grid.raster("./pic.PNG"), and brush up on how working directories and relative paths work.
In the absence of knowing exactly how you're 'setting' your working directory:
In rStudio to correct and easiest approach is to make a 'New Project' for every New Project. You can do this as the first step in your project, or you can add a project file to an existing directory.
When you do this it automatically sets your working directory correctly. The way you seem to be doing it is not correct, you are merely supplying a 'path'. If you were to not want to use a project for some reason (I can't think of a case where that would be right), then you can always use setwd(), however, I believe this needs to be used in every session. A true project doesn't have that requirement.
This will then allow you to deploy your app properly as it will update the working directory to the one on the host machine at shinyapps.io when it is initialised there.
Finally, using the correct relative syntax "./path/to/my.file" to reference all your assets, images, data etc, should correct your issue as stated in the question.

Avoiding to compile the whole document each time in Rmarkdown

I am a new user to R Markdown and was wondering if there was a way to "incrementally" compile an html markdown page as I am writing the code. Say I add 20 lines of code to an existing markdown file today. Is there a way to have the program "remember" the past compilations so that only the 20 new lines are added to the html file preserving the past rendering. I have a lot of memory heavy steps in my code (loading unloading files), and I find that when I add new bits of code I am having to compile everything from the beginning.
I tried looking into the "cache" option but it does not seem to be working.
I am assuming that all the variables I will be needing are present in my environment. In other words, i want to incrementally build an html markdown file without having to compile everything the moment I add an extra line to an existing document. Thanks for your help!
You should look into using the excellent editR package. It does exactly what you're looking for:
https://github.com/swarm-lab/editR

Resources