I am making a presentation in R markdown and I would like that when executing each code fragment I do not have to call the dataframe that I use in each fragment. I explain:
First, in my R markdown document I call my dataframe
```{r, include =FALSE}
library(readxl)
datos_def<- read_excel("datos_def_excel")
summary(datos_def)
Now, I write an introduction and add another code snippet:
```{r, eval=TRUE,echo=FALSE}
options(digits=3,scipen=999)
summary(datos_def$edad)
When I go to generate the pdf document (I click on Knit) it will do so without any problem. But when I hit the green play prompt to run each chunk separately , it returns the following error:
Error in summary(data_def$age) : object 'data_def' not found
What do I need to do to be able to execute each code snippet without having to call the dataframe in each snippet datos_def
?
In the generation by means of
knitr
a totally new R session is instantiated and independent of the one you are using, where obviously, the is processedRmd
sequentially, and then yes, itdatos_def
exists because in the first place the reading from the file was already evaluated Excel.In your interactive session where you run each chunk separately, you should respect this same order, i.e. run first, reading from the file, then the object
datos_def
will be instantiated in the current environment, and the next chunk that does reference to that object should not give you an error.