Knit to pdf not working (or to Word)


I have problems with knit to pdf, but have installed a package (I think Tinytex) and I was able to make pdf-s when I started the Notebook. My html knit files did also start to look very strange with lots of strange signs. That worked before also. I get the error below:

Quitting from lines 413-423 (Exam-HEVAL5140.nb.Rmd)
Error: Aesthetics must be either length 1 or the same as the data (1): x, y and group

  1. -rmarkdown::render(...)
  2. -knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
  3. \-knitr:::process_file(text, output)
  4.   +-base::withCallingHandlers(...)
  5.   +-knitr:::process_group(group)
  6.   \-knitr:::process_group.block(group)
  7.     \-knitr:::call_block(x)
  8.       \-knitr:::block_exec(params)
  9.         \-knitr:::eng_r(options)
  10.           +-knitr:::in_dir(...)
  11.           \-knitr:::evaluate(...)
  12.             \-evaluate::evaluate(...)
  13.               \-evaluate:::evaluate_call(...)
  14.                 +-base:::handle(...)
  15.                 +-base::withCallingHandlers(...)
  16.                 +-base::withVisible(value_fun(ev$value, ev$visible))
  17.                 \-knitr:::value_fun(ev$value, ev$visible)
  18.                   \-knitr:::fun(x, options = options)
  19.                     +-base::withVisible(knit_print(x, ...))
  20.                     +-knitr::knit_print(x, ...)
  21.                     \-knitr:::knit_print.default(x, ...)
  22.                       \-evaluate:::normal_print(x)
  23.                         +-base::print(x)
  24.                         \-ggplot2:::print.ggplot(x)
  25.                           +-ggplot2::ggplot_build(x)
  26.                           \-ggplot2:::ggplot_build.ggplot(x)
  27.                             \-ggplot2:::by_layer(function(l, d) l$compute_aesthetics(d, plot))
  28.                               \-ggplot2:::f(l = layers[[i]], d = data[[i]])
  29.                                 \-l$compute_aesthetics(d, plot)
  30.                                   \-ggplot2:::f(..., self = self)
  31.                                     \-ggplot2:::check_aesthetics(evaled, n)

Warning messages:
1: package 'tibble' was built under R version 4.1.1
2: package 'readr' was built under R version 4.1.1
3: package 'huxtable' was built under R version 4.1.1
4: package 'Synth' was built under R version 4.1.1
Execution halted

This isn't a knitting error. It's a R ggplot2 error.

Does the code chunk run in the console? Just not when knitting?

If so, your console data / packages are different. When you knit, a "clean" R session is called. Consequently if you added a column or load a package in the development phase but that line isn't in the Rmd your knit will fail

The error is telling you that X, Y and Group are not the same length.

The warnings are unlikely to be a concern


I might have pasted the wrong text. This is from the console:


No. The error message is to check aesthetics and it tells you which linenumbers the code error is.

You will have code something like

ggplot(data=df, aes(x = xvals, y= yvals, group=group))

The AES may be in the ggplot line of the geom_

It may also be split across both.

data may be stated or passed through the %>% pipe

However, ggplot does not think your xvals, yvals and group are the same length. If you were plotting shoe size (Y) versus height (X) in boys and girls (group), and you have 30 children there should be 30 Y values, 30 X values and 30 corresponding boy or girl status. If your data is all in one data frame / tibble that is normally automatically the case.

But let's say you were not doing boy Vs girl but splitting your data into 4 groups sequentially, if you made group=1:4 that may appear to work - but it means the group level is actually 4 and ggplot doesn't know what to do.

To be more specific we need:

A sample dataset and the code chunk that breaks.

If you can't provide that, you need to look at your code. My starting point would be to do length(df$xvals) etc - or just inspect them. The other time I hit issues is where I've built the grouping and some delightful function gives results as a list so my group is not a 1:1 relationship to my X and Y values.

Thanks a lot! I will go through all my plots to check.
this was the code:

And from the table. I do not know how to attach it

But could error in this code also be the reason for not been able to knit? My figures print out by the way. I checked it was MikTex I had.


I have no idea why this is working in console and not in knit.

There has to be something wrong with that chunk. I'd try commenting out the second geom_line and see if it runs, if it does, you know that is the dodgy line.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.