title: ! Package pdftex.def Error - when knitting to PDF
body: <p>I am able to knit to PDF for the example below:</p>
  <pre><code>---
  title: &quot;R Notebook&quot;
  output:
    pdf_document: default
    html_notebook: default
    html_document:
      df_print: paged
  ---
  
  Table 1 example:
  
  ```{r, warning=FALSE, message=FALSE, echo=FALSE, include=FALSE, fig.pos=&quot;H&quot;}
  library(magrittr)
  library(tidyverse)
  library(kableExtra)
  library(readxl)
  library(modelsummary)
  library(scales)
  
  tmp &lt;- mtcars
  
  # create a list with individual variables
  # remove missing and rescale
  tmp_list &lt;- lapply(tmp, na.omit)
  tmp_list &lt;- lapply(tmp_list, scale)
  
  # create a table with `datasummary`
  # add a histogram with column_spec and spec_hist
  # add a boxplot with colun_spec and spec_box
  emptycol = function(x) &quot; &quot;
  final_4_table &lt;- datasummary(mpg + cyl + disp + hp + drat + wt + qsec + vs + am + gear + carb ~ N + Mean + SD + Heading(&quot;Boxplot&quot;) * emptycol + Heading(&quot;Histogram&quot;) * emptycol, data = tmp) %&gt;%
      column_spec(column = 5, image = spec_boxplot(tmp_list)) %&gt;%
      column_spec(column = 6, image = spec_hist(tmp_list))
  
  ```
  
  
  ```{r finaltable, echo=FALSE}
  final_4_table
  ```
  </code></pre>
  <p>However, I cannot knit to PDF my own code which involves more variables. My R Markdown starts by reading my excel file and then it is pretty much the same as example above:</p>
  <pre><code>---
  title: &quot;table1&quot;
  output:
    pdf_document: default
    html_document:
      df_print: paged
  ---
  
  Table 1
  
  ```{r prep-tableone, message=FALSE, warning=FALSE, echo=FALSE, include=FALSE, fig.pos=&quot;H&quot;}
  
  library(magrittr)
  library(tidyverse)
  library(kableExtra)
  library(readxl)
  library(modelsummary)
  library(scales)
  
  ### set directory
  setwd(&quot;/etcetc1&quot;)
  
  ## read dataset
  my_dataset &lt;- read_excel(&quot;my_dataset.xlsx&quot;)
  my_dataset &lt;- as.data.frame(my_dataset)
  
  ...
  </code></pre>
  <p>I can run this code in R script; it works just fine. I can also knit this code to HTML just fine. When trying to knit to PDF I get the following error:</p>
  <pre><code>output file: table1test.knit.md
  
  ! Package pdftex.def Error: File `table1test_files/figure-latex//boxplot_65c214bae9bb.pdf' not found: using draft setting.
  
      Error: LaTeX failed to compile table1test.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See table1test.log for more info.
      In addition: Warning messages:
      1: package 'ggplot2' was built under R version 4.1.1 
      2: package 'tibble' was built under R version 4.1.1 
      3: package 'tidyr' was built under R version 4.1.1 
      4: In in_dir(input_dir(), evaluate(code, envir = env, new_device = FALSE,  :
        You changed the working directory to /etcetc1 (probably via setwd()). It will be restored to /etcetc2. See the Note section in ?knitr::knit
      Execution halted
  </code></pre>
  <p>Am I missing any packages? Do you know what might be happening?</p>
  <p>I use TeXShop for LaTeX.</p>