<commit_before>---
layout: default
title: Code Reference
subtitle: Use an external R script with your document
category: demo
---

You do not have to put the R code in the input document; with **knitr**, you can separate your input document with the R script (think the reverse of Stangle).

There are several advantages of separating the main document with the R script(s), e.g., R code can be reusable across several documents, and you can run the R code continuously in a separate file (if you embed the code in the document, you often have to jump through texts); this feature is especially useful for LyX users, and it saves a huge amount of time since you do not have to re-compile the whole document to see the results; instead, you can tune your R code freely in another R session.

The key to use code reference is the `ref` option; it specifies a filename, e.g. `ref=Stat579-HW8-Xie.R`. See the [options](/knitr/options) page for details. Since this is a chunk option, it is possible for chunks to use different R scripts.

- A homework solution
  - Rnw source: [Stat579-HW8-Xie.Rnw](https://github.com/downloads/yihui/knitr/Stat579-HW8-Xie.Rnw)
  - R source: [Stat579-HW8-Xie.R](https://github.com/downloads/yihui/knitr/Stat579-HW8-Xie.R)
  - PDF output: [Stat579-HW8-Xie.pdf](https://github.com/downloads/yihui/knitr/Stat579-HW8-Xie.pdf)
- Another homework
  - Rnw source: [Stat546-HW4-Xie.Rnw](https://github.com/downloads/yihui/knitr/Stat546-HW4-Xie.Rnw)
  - R source: [Stat546-HW4-Xie.R](https://github.com/downloads/yihui/knitr/Stat546-HW4-Xie.R)
  - data source: [GOOG2010.csv](https://github.com/downloads/yihui/knitr/GOOG2010.csv)
  - PDF output: [Stat546-HW4-Xie.pdf](https://github.com/downloads/yihui/knitr/Stat546-HW4-Xie.pdf)<commit_msg>tweak demo of code externalization<commit_after>---
layout: default
title: Code Externalization
subtitle: Use an external R script with your document
category: demo
---

You do not have to put the R code in the input document; with **knitr**, you can separate your input document with the R script (think the reverse of Stangle).

There are several advantages of separating the main document with the R script(s), e.g., R code can be reusable across several documents, and you can run the R code continuously in a separate file (if you embed the code in the document, you often have to jump through texts); this feature is especially useful for LyX users, and it saves a huge amount of time since you do not have to re-compile the whole document to see the results; instead, you can tune your R code freely in another R session.

The function `read_chunk()` was designed for this feature. For example, if the R code is in [Stat579-HW8-Xie.R](https://github.com/downloads/yihui/knitr/Stat579-HW8-Xie.R), you can use `read_chunk('Stat579-HW8-Xie.R')` to read the code into the input document in an early chunk. It is possible to use multiple external R scripts (just call `read_chunk()` for multiple times).

- A homework solution
  - Rnw source: [Stat579-HW8-Xie.Rnw](https://github.com/downloads/yihui/knitr/Stat579-HW8-Xie.Rnw)
  - R source: [Stat579-HW8-Xie.R](https://github.com/downloads/yihui/knitr/Stat579-HW8-Xie.R)
  - PDF output: [Stat579-HW8-Xie.pdf](https://github.com/downloads/yihui/knitr/Stat579-HW8-Xie.pdf)
- Another homework
  - Rnw source: [Stat546-HW4-Xie.Rnw](https://github.com/downloads/yihui/knitr/Stat546-HW4-Xie.Rnw)
  - R source: [Stat546-HW4-Xie.R](https://github.com/downloads/yihui/knitr/Stat546-HW4-Xie.R)
  - data source: [GOOG2010.csv](https://github.com/downloads/yihui/knitr/GOOG2010.csv)
  - PDF output: [Stat546-HW4-Xie.pdf](https://github.com/downloads/yihui/knitr/Stat546-HW4-Xie.pdf)
{'fraction_non_alphanumeric': 0.11295139831658974, 'fraction_numerical': 0.034754276405104534, 'mean_word_length': 4.145251396648045, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 15, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1184108', 'n_tokens_mistral': 1298, 'n_tokens_neox': 1246, 'n_words': 417}