Knit error. Object not found
Asked Answered
L

6

14

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:'

enter image description here

Lelahleland answered 20/2, 2017 at 9:35 Comment(3)
Try using 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.Sandarac
but why i van run it with ctrl+enter ?Lelahleland
Can you run it with ctrl+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
P
16

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.

Piling answered 20/2, 2017 at 10:4 Comment(2)
I did File -> Import Dataset -> From CSV So, I have the adult in the Global environment window. Isn't it enough? How can i add it to my knit environment?Lelahleland
adult <- read.csv2('path/to/file') read ?read.csv2 for its optionsPiling
P
6

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.

Parkin answered 24/3, 2019 at 14:5 Comment(0)
P
4

Because usually I've a lot of code that prepares the data variables effectively used in the knitr documents my workaround use two steps:

  1. In the global environment, I save all the objects on a file using save()
  2. 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

Peroxidize answered 10/12, 2018 at 10:21 Comment(0)
C
4

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.

Coachman answered 15/3, 2020 at 8:17 Comment(0)
S
0

When knitting to PDF

```{r setup}
knitr::opts_chunk$set(cache =TRUE)
```

Worked fine.

But not when knitting to Word.

Sixtyfourmo answered 14/12, 2018 at 16:5 Comment(0)
W
0

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")
Waggery answered 2/11, 2020 at 17:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.