Skip to content

Instantly share code, notes, and snippets.

@njtierney
Last active June 3, 2019 13:27
Show Gist options
  • Save njtierney/d5a877a6dc8a97f2b4a5 to your computer and use it in GitHub Desktop.
Save njtierney/d5a877a6dc8a97f2b4a5 to your computer and use it in GitHub Desktop.
This chunk is what I usually write at the start of most rmarkdown documents.
```{r global_options, include=FALSE, cache=FALSE}
library(knitr)
# Set basic options. You usually do not want your code, messages, warnings etc.
# to show in your actual manuscript however for the first run or two these will
# be set on.
opts_chunk$set(echo=FALSE,
warning=FALSE,
message=FALSE,
cache = TRUE,
include = FALSE,
results = 'hide',
error = TRUE)
# setup changes according to html or docx
output <- opts_knit$get("rmarkdown.pandoc.to")
if (output=="html") {
opts_chunk$set(fig.width=11,
fig.height=11)
} # # end html `if` statement
## setting up the figure parameters for docx
if (output=="docx") {
opts_chunk$set(dev = 'pdf',
fig.width = 6,
fig.height = 6)
} # end docx `if` statement
```
@njtierney
Copy link
Author

You have a library(knitr) call at the start so you can use the command ops_chuink$set. My options here say:

echo = F ........ don't echo the code I write
warning = F ... don't give me warnings in the document (I usually leave this on when drafting)
message = F.. don't give me messages in the doc.
cache = T....... save my data/results so that compiling is quicker (keep in mind sometimes this needs to be reset when you change one chunk that has other chunks that depend upon it. You can of course set a chunk to have a dependson category...but I'm lazy and don't do that)
include = F ..... This basically ENSURES that NOTHING is printed from the code, including graphics. This means that if I want a graphic or table to be shown, then I need to put include = T in the chunk header.
results = hide ... suppres side effects of writing code
errro = T ............ show me errors that pop up in the code.

@njtierney
Copy link
Author

The second part is really nifty.

It basically means that I can set different image heights, and other settings, depending on whether I decide to knit the document into html or docx formats. You could also include one for pdf if you wanted by mimicking the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment