Chapter 11 Intro to R Markdown
R Markdown is a better and more organized way to write scripts. Seriously, once you learn it, there’s no going back. New and don’t know where to start? Read The R Markdown Cookbook. Amazing overview with tons of neat tricks and how-to’s. This other source may also be of some help.
Below are some quick tips for common tasks; but be sure to read the Cookbook above.
11.1 Important code chunk options
cache
: TRUE or FALSE. Do you want to save the output of the chunk so it doesn’t have to run next time? Creates a cached folder in the directory.eval
: Do you want to evaluate (i.e., run) the code in the chunk?echo
: Do you want to print the code after it’s run?include
: Do you want to include code output in the final output document? Setting toFALSE
means the code does not appear in the output document, but it is still run.
11.2 Writing math equations and symbols
11.2.1 Greek symbols
A few notes first: Math notation is done with dollar signs and forward slashes…
For Greek letters, just type the name of the letter:
$\mu$
for \(\mu\)$\sigma$
for \(\sigma\)$\alpha$
for \(\alpha\)$\pi$
for \(\pi\)$\rho$
for \(\rho\)etc.
11.3 Including graphics/inserting pictures
The default method doesn’t work for me for some reason, but you can
still insert images using a combination of the here
package and
knitr
.
Use the include_graphics()
command and specify both the file location
and it’s name:
::include_graphics(here::here("pics","snapchat.png")) knitr
NOTE. Use 300-600 DPI to get good looking pictures.
The bookdown book notes that:
The syntax for controlling the image attributes is the same as when
images are generated from R code. Chunk options fig.cap
, out.width
,
and fig.show
still have the same meanings.
and: You can easily scale these images proportionally using the same
ratio. This can be done via the dpi
argument (dots per inch), which
takes the value from the chunk option dpi
by default If it is a
numeric value and the chunk option out.width
is not set, the output
width of an image will be its actual width (in pixels) divided by dpi ,
and the unit will be inches. For example, for an image with the size 672
x 480, its output width will be 7 inches ( 7in ) when dpi=96
. This
feature requires the package png and/or jpeg to be installed. You can
always override the automatic calculation of width in inches by
providing a non-NULL value to the chunk option out.width
, or use
include_graphics(dpi = NA)
11.4 Footnotes
To add a footnote, use the “^” symbol and put the note in brackets:
You can also write footnotes1 like this.
11.7 Making better tables
https://rfortherestofus.com/2019/11/how-to-make-beautiful-tables-in-r/