We will use R Markdown for our compulsory exercises in this course - you therefore need to know
knit
?knit
?Hands-on: We look at the template for Compulsory exercise 1 at https://www.math.ntnu.no/emner/TMA4268/2019v/CompEx1mal.Rmd
knit
button and observe that happens.In the future: If you want to make a new document in RStudio: “File New R Markdown”
Remember: for Compulsory exercise 1 we have list all packages you need to install on the top of the assignment: https://www.math.ntnu.no/emner/TMA4268/2019v/Compulsory1.html#r_packages Smart of you to start by installing all of these!
output value | creates |
---|---|
html_document | html |
pdf_document | pdf requires Tex |
word_document | Microsoft Word (.docx) |
odt_document | OpenDocument Text |
rtf_document | Rich Text Format |
md_document | Markdown |
github_document | Github compatible markdown |
ioslides_presentation | ioslides HTML slides |
slidy_presentation | slidy HTML slides |
beamer_presentation | Beamer pdf slides (requires Tex) |
See also list of sub-options in cheat sheet. https://github.com/rstudio/cheatsheets/raw/master/rmarkdown-2.0.pdf
knit
? (rendering)Knitting is also done by: Ctrl+Shift+K (Windows) or Cmd+Shift+K (MacOS).
Creating documents with R Markdown starts with an .Rmd file that contains a combination of markdown (content with simple text formatting) and R code chunks.
The .Rmd file is fed to knitr
which executes all of the R code chunks and creates a new markdown (.md) document which includes the R code and it’s output.
The markdown file generated by knitr
is then processed by pandoc
which is responsible for creating a finished web page, PDF, MS Word document, slide show, handout, book, dashboard, package vignette or other format.
More: About pandoc - the swiss army knife NB: even if you write tex this is first translated to md and then via pandoc to pdf, so subtile tex stuff may be missed on the way.
Do you get a separate window popping up, or is your output shown in the Viewer tab of one of the window panes? Go to RStudio-preferences-RMarkdown and check what is your value of “show output preview in”.
html_document
, pdf_document
and beamer_presentation
.To produce a pdf_document
RStudio (using pandoc) will call a latex-installation, so you need to have latex installed on your laptop to be able to produce a pdf-file.
Toggle comment/uncomment with hashtag in YAML header output
to make different options active, then press knit
. Alterntively this can be done by calling the function rmarkdown::render()
from your Console window.
Optional: check that uncommenting pdf_document
and commenting out html_document
and pressing knit
will give you a pdf-file.
During rendering we use the location of the .Rmd file as the working directory, and the rendering is done in a new session.
Optional: can instead run rmarkdown::render("file.Rmd","pdf_document")
from command line, orrmarkdown::render("file.Rmd","html_document")
. Then you can have several options in the YAML together!
$
and equation with $$
Hands-on: go the the Compulsory exercise 1 template, look at Q1 and Q2, and and just write and press knitr
to see! Try for example to answer Q1 (incorrectly) by writing \(Y_i=\beta_0+\beta_1 x_{i1} +\varepsilon_i\) (the correct answer will use the correct model).
First option: then the link is displayed with the address:
<link>
as we have done with
<https://github.com/rstudio/cheatsheets/raw/master/rmarkdown-2.0.pdf>
Second option: if you do not want to display the address but some other text
[name of link](link)
as with
[R Markdown cheat sheet](https://github.com/rstudio/cheatsheets/raw/master/rmarkdown-2.0.pdf)
echo
: display the code in the chunk, TRUE or FALSE or selected lines, or maybe with an R-object (later)eval
: run code in the chunk, TRUE or FALSEHands-on: go the the Compulsory exercise 1 template and insert coef(modelA)
in the end of the first code chunk. Press knit
to see the effect. Then change the chunk options to eval=FALSE
and knit
. What happens. Finally, change the chunk options to echo=FALSE
and knit
and observe the result.
Smart things to add to the setup-chunk:
library(knitr)
opts_chunk$set(tidy.opts = list(width.cutoff = 68), results = "hold", tidy = TRUE,
warning = FALSE, error = FALSE, message = FALSE)
knitr::opts_chunk$set(echo = TRUE)
Use the ` r before and ` after an R command to integrate into the text.
Hi, 2+2 is equal to `r 2+2`.
This is what we have done in the YAML-header to include todays date on your submission:
r format(Sys.time(), '%d %B, %Y')
knit
with output: pdf_document no pdf-file is produced. Why?My solution
keep_tex: yes
yaml optiontexshop
and look for sensible log for errors,