I have an uncleaned dataset. So, I have imported it to my R studio.Then when I run nrow(adult)
in the rmarkdown file and press ctrl+Enter
it works, but when i press the knit
the following error appears:'
When you knit
something it gets executed in a new environment.
The object adult
is in your environment at the moment, but not in the new one knit creates.
You probably did not include the code to read or load adult
in the knit.
If you clear your workspace, as per @sebastian-c comment, you will see that even ctrl+enter
does not work.
You have to create the adult
object inside your knit
. For example, if your data in from a csv add
adult <- read.csv2('Path/to/file')
in the first chunk.
Hope this is clear enough.
adult <- read.csv2('path/to/file')
read ?read.csv2
for its options –
Piling Another option, in the same way as the previous, but really useful in case you have a lot of diferent data
Once you have all your data generated from your R scripts, write in your "normal code" ( any of your R scripts):
save.image (file = "my_work_space.RData")
And then, in your R-Markdown script, load the image of the data saved previously and the libraries you need.
```{r , include=FALSE}
load("my_work_space.RData")
library (tidyverse)
library (skimr)
library(incidence)
```
NOTE: Make sure to save your data after any modification and before running knitr.
Because usually I've a lot of code that prepares the data variables effectively used in the knitr documents my workaround use two steps:
- In the global environment, I save all the objects on a file using save()
- In the knitr code I load the objects from the file using load()
Is no so elegant but is the only one that I've found.
I've also tried to access to the global environment variables using the statement get() but without success
If you have added eval = FALSE
the earlier R code won't execute in which you have created your object.
So when you again use that object in a different chunk it will fail with object not found message.
When knitting to PDF
```{r setup}
knitr::opts_chunk$set(cache =TRUE)
```
Worked fine.
But not when knitting to Word.
I am rendering in word. Here's what finally got my data loaded from the default document directory. I put this in the first line of my first chunk.
load("~/filename.RData")
© 2022 - 2024 — McMap. All rights reserved.
rm(list = ls())
to clean your workspace and then source your script. You're probably failing to read in the file as part of your script. – Sandaracctrl+enter
? – Lelahlelandctrl+enter
if you clear your environment first and only run uncommented lines in your script? When knitr runs, it runs in a clean environment. That is, it ignores everything in your workspace and makes its own new one. – Sandarac