3

Code entered into R Notedbook:

Code:

---
title: "test"
output: html_notebook
---
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir=WORKING.DIRECTORY)
summary(cars)
head(cars)

Output: Output produced by code above

Phil
  • 7,287
  • 3
  • 36
  • 66
  • Hi, and welcome to StackOverflow! According to your post, it looks like you're a new contributor. These links are great resources: [How to make a great R reproducible example](http://Hi%20and%20welcome%20to%20StackOverflow!%20According%20to%20your%20post,%20it%20looks%20like%20you're%20a%20new%20contributor.%20Please%20consider%20reviewing%20the%20following%20links:%20https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and [FAQ Index for Stack Overflow](https://meta.stackoverflow.com/questions/251225/faq-index-for-stack-overflow) – Eric Jul 25 '20 at 23:39

2 Answers2

0

Try using getwd() instead of WORKING.DIRECTORY.

``` r{r setup, include=FALSE}

knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir=getwd())

```

Generate new code chunk:

The keyboard shortcut to do this is Ctrl + Alt + I (OS X: Cmd + Option + I)

```{r}

summary(cars)
head(cars)

```

Output:

#>      speed           dist       
#>  Min.   : 4.0   Min.   :  2.00  
#>  1st Qu.:12.0   1st Qu.: 26.00  
#>  Median :15.0   Median : 36.00  
#>  Mean   :15.4   Mean   : 42.98  
#>  3rd Qu.:19.0   3rd Qu.: 56.00  
#>  Max.   :25.0   Max.   :120.00


#>   speed dist
#> 1     4    2
#> 2     4   10
#> 3     7    4
#> 4     7   22
#> 5     8   16
#> 6     9   10

Created on 2020-07-25 by the reprex package (v0.3.0)

Eric
  • 2,699
  • 5
  • 17
0

Make sure you actually run the code chunks you want to see displayed and save the file:


I've also set root.dir to the result of getwd(). This doesn't change the result as far as displaying the tables, but here is the whole file for reference:

---
title: "test"
output: html_notebook
editor_options: 
  chunk_output_type: inline
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir=getwd())
``` 
```{r}
summary(cars)
head(cars)
```
5eb
  • 14,798
  • 5
  • 21
  • 65