Course Hero Logo

Dependson should contain a character vector of every

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 459 - 461 out of 520 pages.

dependsonshould contain a character vector ofeverychunk that thecached chunk depends on.knitrwill update the results for thecached chunk whenever it detects that one of its dependencies haschanged.Note that the chunks won’t update ifa_very_large_ le.csvchanges,becauseknitrcaching only tracks changes within the.Rmdfile. Ifyou want to also track changes to that file you can use thecache.extraoption. This is an arbitrary R expression that will inva‐lidate the cache whenever it changes. A good function to use isfile.info(): it returns a bunch of information about the fileincluding when it was last modified. Then you can write:```{r raw_data, cache.extra = file.info("a_very_large_file.csv")}rawdata <- readr::read_csv("a_very_large_file.csv")```As your caching strategies get progressively more complicated, it’sa good idea to regularly clear out all your caches withknitr::clean_cache().I’ve used the advice ofDavid Robinsonto name these chunks: eachchunk is named after the primary object that it creates. This makesit easier to understand thedependsonspecification.Global OptionsAs you work more withknitr, you will discover that some of thedefault chunk options don’t fit your needs, and want to changethem. You can do that by callingknitr::opts_chunk$set()in acode chunk. For example, when writing books and tutorials I set:knitr::opts_chunk$set(comment="#>",collapse=TRUE)This uses my preferred comment formatting, and ensures that thecode and output are kept closely entwined. On the other hand, ifyou were preparing a report, you might set:knitr::opts_chunk$set(echo=FALSE)That will hide the code by default, only showing the chunks youdeliberately choose to show (withecho = TRUE). You might con‐Code Chunks|433
sider settingmessage = FALSEandwarning = FALSE, but thatwould make it harder to debug problems because you wouldn’t seeany messages in the final document.Inline CodeThere is one other way to embed R code into an R Markdown docu‐ment: directly into the text, with:`r `. This can be very useful if youmention properties of your data in the text. For example, in theexample document I used at the start of the chapter I had:We have data about`r nrow(diamonds)`diamonds. Only`rnrow(diamonds) - nrow(smaller)`are larger than 2.5 carats. Thedistribution of the remainder is shown below:When the report is knit, the results of these computations are inser‐ted into the text:We have data about 53940 diamonds. Only 126 are larger than 2.5carats. The distribution of the remainder is shown below:When inserting numbers into text,format()is your friend. It allowsyou to set the number ofdigitsso you don’t print to a ridiculousdegree of accuracy, and abig.markto make numbers easier to read.I’ll often combine these into a helper function:comma<-function(x)format(x,digits=2,big.mark=",")comma(3452345)#> [1] "3,452,345"comma(.12358124331)#> [1] "0.12"Exercises1. Add a section that explores how diamond sizes vary by cut,color, and clarity. Assume you’re writing a report for someonewho doesn’t know R, and instead of settingecho = FALSEoneach chunk, set a global option.

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 520 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Spring
Professor
Hector Vazquez
Tags

Newly uploaded documents

Show More

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture