Aditya Bhalla
Aditya Bhalla

Reputation: 43

R Markdown won't knit even though my code chunks work

For some reason, my code chunks work but when I try to knit the file, I have an "object not found" error. Here's the chunk which causes the error:

library(ggplot2)
library(tidyverse)
library(dplyr)
chart2 <- all_applicants %>%
  filter(Race == "Asian" | Race == "Black or African American" | Race == "White" | Race == "Hispanic or Latino") %>%
  ggplot(all_applicants, mapping = aes(preferred_role)) + geom_bar(aes(fill = Race)) +
  labs(x = "Preferred Role", y = "Number of Applicants", title = "Preferred Applicant Role by Gender", caption = "Data: 2021 Recruiting Data, Govern for America") + theme_bw()

print(chart2)
table(all_applicants$Race)

And here's the error message:

Line 57 Error: Problem with `filter()` input `...1` x object 'Race' not found i Input `...1` is `|...`.

The variable "Race" is part of the all_applicants dataset which I created in a previous chunk. Even though the chunk itself works as intended, when I knit I get the above error. Any help?

Upvotes: 3

Views: 29312

Answers (5)

Kristy Shield-Artin
Kristy Shield-Artin

Reputation: 21

I was getting this too. My code would run fine if I ran each chunk in order (after sweeping the environment to check) but it would not knit (I have other scripts that knit fine). I found that I had accidentally included eval=FALSE instead of echo=FALSE to a key Chunk.

Not sure of the right terminology but it is this bit at the start of the chunk.

{r setup, echo=FALSE} which worked vs {r setup, eval=FALSE} which didn't work

I know another person said this was not the answer, but it was the answer I needed.

Upvotes: 2

guyabel
guyabel

Reputation: 8366

I ran into a similar problem and got things working by deleting the cache folder related to the markdown file and re-running.

Upvotes: 1

HM_ft
HM_ft

Reputation: 166

As weird as it sounds, I solved the same problem by creating a copy of my .Rmd file and moving it into another folder.

Upvotes: 2

Poza
Poza

Reputation: 395

That might not be the solution to your problem, but a solution for others like me who had a similar problem as you have: I added a knit specification to the YAML header to save my knitted file in another directory. After I deleted that part, my Markdown file was able to knit again. So maybe the problem is not in the code that you presented, but in your header.

Upvotes: 1

Ethan Bass
Ethan Bass

Reputation: 476

Maybe you have eval=F in your chunk that defines Race.

Upvotes: 2

Related Questions