In RStudio/RMarkdown, how to setwd?

Here's what I've been using, and it seems to work well when using R Projects (.Rproj files):

knitr::opts_chunk$set(
    # This should allow Rmarkdown to locate the data
    root.dir = rprojroot::find_rstudio_root_file()
)

See Issue #277 and for further background, the package author's comments here

What you are looking for is the root.dir option in knitr::opts_knit.

The following will set the root directory for subsequent code chunks (but not this chunk):

```{r setup}
knitr::opts_knit$set(root.dir = '/tmp')
```

EDIT: RStudio 1.0.44

as of RStudio's latest release (Oct/Nov 2016), the following snippet is needed for knitr's render default:

```{r setup}
knitr::opts_knit$set(root.dir = '/tmp')
```

see Etienne's comment about versions below.