Week 6 - How to create better scripts

CHU0397 | National Taiwan Normal University

๐ŸŽฏ Goals

  • Advanced scripts: How to use Markdown documents to share better scripts?

๐Ÿ“Š Better and reproducible scripts with R Markdown

From raw script to functional: R Markdown

1. Create the file ๐Ÿ“„
Go to New File > R Markdown. This moves you from raw code to a โ€œlivingโ€ document.

2. Configure (YAML) โš™๏ธ
The header between --- controls your title, author, and output format (HTML/PDF).

3. Text + Code chunks ๐Ÿ“
Write text normally; insert Code Chunks ({r}) for analysis.

4. Run your codes โ–ถ๏ธ
Use the green Play Button to test individual steps.

5. The final โ€œKnitโ€ ๐Ÿงถ
Click Knit to stitch everything into a polished report automatically!

::: {.column width = โ€œ45%โ€} ::: {.r-stack}

:::

:::

๐Ÿ˜ฑ Common problem

When you try to knit your R Markdown document into a PDF file, you may encounter this message:

sh: pdflatex: command not found

Error: LaTeX failed to compile Untitled.tex. See The R package tinytex - Helper Functions to Manage TinyTeX, and Compile LaTeX Documents - Yihui Xie | ่ฐข็›Š่พ‰ for debugging tips. See Untitled.log for more info.
In addition: Warning message:
In system2(..., stdout = if (use_file_stdout()) f1 else FALSE, stderr = f2) :
error in running command
Execution halted

The solution

You need to install a package before creating the PDF document.

Here are the steps:

1. Run this line of code in the console block:

tinytex::install_tinytex()

It can take some time, up to 5 minutes.

2. Click one more time on โ€œKnit to PDFโ€

Itโ€™s supposed to work. If it still doesnโ€™t work, please contact me!

๐Ÿ“Š R Markdown cheatsheet

You can find more about how to write R Markdown documents on this website:

Link to the cheatsheet

๐Ÿ“ In-class exercise!



Task list:

๐Ÿ“ Assignment for next week