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



Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Poza
Solution 2 Ethan Bass
Solution 3 HM_ft