Knit to HTML Error in Markdown

I continue to get the following error when I work with any dataset that isn't already part of an R package (for example gapminder). I've imported the file as a csv, created the object and it appears in my global environment, but when I go to knit it continuously says. I've tried clearing the environment and other work arounds to no avail...

Error in eval(lhs, parent, parent) : object 'women_research' not found Calls: ... withCallingHandlers -> withVisible -> eval -> eval -> %>% -> eval -> eval Execution halted.

women_research %>% 
  group_by(field) %>% 
  summarise(mean_percent_women = mean(percent_women)) -> percent_women_by_field
ggplot(data = percent_women_by_field) +
  aes(x = mean_percent_women*100, y = field) +
  theme(panel.grid = element_blank(),
        panel.background = element_rect("cornsilk"),
        text = element_text(family = "Courier"), 
        axis.ticks = element_blank(),
        axis.text.y = element_blank()) +
  theme(legend.position = "right",
        legend.key = element_rect("transparent")) +
  scale_x_continuous(limits = c(10,50)) +
  labs(x = "% by sector", y = NULL, title = "Arcoss the world, what are the major sectors women work in?", subtitle = "Average percent of women by field of 39 leading economies", caption = "viz by Rich with ggplot, source: TidyTuesday") +
  scale_color_brewer(palette = "Set1") +
  geom_point(aes(color = field)) +
  aes(size = mean_percent_women) +
  guides(size = FALSE)

One simple test would be use the assigment of "percent_women_by_field at the top as in:

r...
percent_women_by_field <- women_research %>%
group_by(field) %>%
summarise(mean_percent_women = mean(percent_women))
...

I only suggest this because of the part of the error that says "eval(lhs, parent,parent)"

See this article https://stat.ethz.ch/R-manual/R-devel/library/base/html/assignOps.html

Are these step to load the women_research dataset part of your Rmarkdon file?

Rmarkdown file will be rendered in its own process and environment. Everything must be available.

It is like me if I want to reproduce your example. I can't because I don't have women_research dataset on my side. I only get your code but I don't know the libraries you are using (I can guess, rmarkdown won't), and I don't know from where women_research comes from ! So your code will error on my side too.

Having the data available in your global environment is not enough, you need to load it inside you Rmd document so that it is available to the R environment during rendering.

Please also post the full reproducible example to help us help you. As your issue is with knitting a Rmd file, you should share the example as a Rmd file too.

Hope it helps.

2 Likes

cderv, thank you for the feedback. First off, yes, the steps to load the dataset are part of the markdown. Also, here is the link to the dataset, it was from tidytuesday.

Also, here is the Rmd. code chunk where I use the data. The first step was a manipulation...

  group_by(field) %>% 
  summarise(mean_percent_women = mean(percent_women)) -> percent_women_research
ggplot(data = percent_women_research) +
  aes(y = mean_percent_women*100, x = field) +
  geom_point(aes(color = field,
             size = mean_percent_women)) +
  scale_color_brewer(palette = "Set1") +
  guides(size = FALSE,
         color = FALSE) +
  theme(panel.grid.major = element_line(linetype = "dashed", color = "black", size = .11),
        panel.background = element_rect("cornsilk"),
        text = element_text(family = "Courier"), 
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_text(size = 13),
        axis.text.x = element_blank()) +
  labs(title = "Across the world, what are the major sectors women work in?", subtitle = "Average percent of women by field of 39 leading economies", caption = "viz by Rich with ggplot, source: TidyTuesday") +
  scale_y_continuous(limits = c(10,50)) +
  geom_text(data = . %>% 
              group_by(field), 
            aes(label = field),
            nudge_y = -1.2,
            nudge_x = .1,
            family = "Courier") 

Your code above don't show how you load the women_research dataset in you Rmd.

Your error is with this object not being found, so it would help to have all the code to reproduce from the CSV file on the web.

The file you linked don't have a field column, so you must have done other thing.

We need a reproducible example to be able to help you.

Thank you !

The knit button indeed uses a sort of separate background process. It is indeed annoying that therefore, variables from the global environment cannot be used.
As a beginner it seemed inconsistent to me that the code block runs without error, but a rather cryptic error message is thrown when trying to knit.

The simple solution is to render the document via console. You need to add some more options to choose format and such.

In its simplest form do:

rmarkdown::render("your_doc.Rmd")

1 Like

That seems to work. Thank you so much for the help!

I think this for reproducibility. Rmarkdown file is best used if built as a reproducible document that contains the analysis. If the document depends on other variable from the Global environment, it will not be able to be rendered without knowing how to create the missing variable.

Also, this can be source of error to render from console when you have lots of object in the global environment. it requires extra care. We had some weird behavior due to that sometimes.

Cheers!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.