The line expression R returns an invalid value

If the R code in the Rmd file repeats the same variable name, the inline r expression seems to return the last value of that variable regardless of the location of the inline expression. Is there any way to avoid this behavior, except that the same variable name is not reused in different parts of the document?

Reproducible example

---
title: "R Notebook"
output: html_notebook
---


```{r}
df <- cars
nrow(df)
```

The dataset has `r nrow(df)` rows.


```{r}
df <- iris
nrow(df)
```

The dataset has `r nrow(df)` rows.

This creates the following output

enter image description here

I use: R version 3.3.2 (2016-10-31) Platform: x86_64-w64-mingw32 / x64 (64-bit version) Works under: Windows 7 x64 (build 7601) Service Pack 1

rmarkdown_1.4 knitr_1.15.1

+6
source share
2 answers

, "" , . HTML, , .

---
title: "R Notebook"
output: html_notebook
---

---
title: "R Notebook"
output: 
    html_document: default
    html_notebook: default
---

. -, RMarkdown "Inline knitr" (. http://rmarkdown.rstudio.com/lesson-4.html)

-, YAML , RStudio . ,

---
title: "R Notebook"
output: 
    html_notebook: default
---
+3

cache = TRUE

---
title: "R Notebook"

output: 
html_notebook: default


---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(cache=TRUE)
```


```{r}
df <- cars
nrow(df)
```

enter image description here

+2

Source: https://habr.com/ru/post/1016039/


All Articles