Skip to content
This repository has been archived by the owner on Dec 29, 2019. It is now read-only.

Latest commit

 

History

History

07-knitr

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Writing a report

  1. Start an R session in this directory (07-knitr)

    • RStudio users, open the *.Rproj file in this directory
  2. Build the project:

    remake::make()
  3. Fetch the ragout:

    remake::fetch("ragout")
  4. Create a new file final.Rmd in this directory, with the following contents:

    ---
    title: "Final cooking report"
    author: "<Your name>"
    output: rmarkdown::html_document
    ---
    
    ```{r init, echo = FALSE, message = FALSE}
    if (!isNamespaceLoaded("remake")) remake::create_bindings()
    ```
    
    Look at the ragout I made today:
    
    ```{r}
    ragout
    ```
    
    I combined fried meat and vegetables:
    
    ```{r}
    fried_meat
    cooked_potatoes
    peeled_carrots
    ```
    
    Some of the components were more difficult to prepare than others:
    
    ```{r}
    plot(
      factor(c("Fried meat", "Cooked potatoes", "Peeled carrots")),
      sapply(
        list(fried_meat, cooked_potatoes, peeled_carrots),
        function(x) length(format(x))
      )
    )
    ```
    
  5. Render the document using rmarkdown::render("final.Rmd"), preferably in a fresh R session.

    • RStudio users can hit Ctrl+Shift+K when final.Rmd is open
  6. View the resulting final.html in your browser.

  7. Create a file R/knit.R with the following contents:

    knit_with_deps <- function(target_name, source_name, ..., envir = parent.frame()) {
      dep_names <- as.character(unlist(substitute(list(...))[-1]))
      deps <- list(...)
      names(deps) <- dep_names
      deps <- deps[unique(names(deps))]
    
      dep_env <- as.environment(deps)
      parent.env(dep_env) <- envir
    
      rmarkdown::render(
        input = source_name,
        output_format = "all", envir = dep_env
      )
    }
  8. Add the following rule to your targets in remake.yml:

      final.html:
        command: knit_with_deps(target_name, "final.Rmd", ragout, peeled_carrots, cooked_potatoes, fried_meat)
    
  9. Make sure the final.html target is part of the main target!

    targets:
      all:
        depends:
        - ragout
        - final.html
    
  10. Build the project.

  11. View the resulting final.html in your browser, again.