kaseyzapatka
kaseyzapatka

Reputation: 169

knitr::include_graphics() doesn't render figure with `bookdown`

I'm trying to write up a report and simply want to include a .jpeg image in my write up. However, whenever i knit together my document, I get the following error:

! Missing $ inserted.
<inserted text> 
                $
l.128 ...tput/figures/chapter1/top100/top100_2000}

Try to find the following text in chp1.Rmd:
  ...tput/figures/chapter1/top100/top100_2000} 

You may need to add $ $ around a certain inline R expression `r ` in chp1.Rmd (see the above hint). See https://github.com/rstudio/rmarkdown/issues/385 for more info.
Error: LaTeX failed to compile chp1.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See chp1.log for more info.
Execution halted

I can't figure out what is going on here. I thought it be my use of the here package, but when I used the entire file path instead, it gave me the same error. I ran just the code chunk and it loaded the image, so this makes me think I have some setting that isn't allowing the output of the R chunk to be read properly, but I can't figure it out. Any suggestions?

Here is the YAML header:

---
title: "Chapter 1: "
author: "Kasey Zapatka"
date: " lasted edited: `r format(Sys.time(), '%B %d, %Y')`"
output:
  bookdown::pdf_document2:
    toc: no
    fig_caption: yes
    latex_engine: xelatex
    keep_tex: true
bibliography: "references/references.bib"
link-citations: yes
fontsize: 12pt
header-includes:
    - \usepackage{rotating}
    - \usepackage{setspace}
    - \newcommand{\beginsupplement}{\setcounter{table}{0}  \renewcommand{\thetable}{A\arabic{table}} \setcounter{figure}{0} \renewcommand{\thefigure}{A\arabic{figure}}}
    - \usepackage{lineno}
    - \linenumbers
abstract: |
  This chapter describes the trends and patterns in the key variables in my analysis. First, I identify the top 100 metro areas by population in 2019, which serves as the unit of analysis for this chapter. I look at the changes over time, both in rank and relative population growth. Next, I investigate gentrification patterns in the top 100 metro areas in both 2013 and 2019, as well as change over time. I specifically focus on the patterns in metros where they see the most intense gentrification. I find.. Finally, I review patterns of rent and mortgage burden in the top 100 metros across the country, in 2019 and change over time. I find strikingly different patterns of burden growth by tenure status. 
---

settings code chunk:


# chunk options ----------------------------------------------------------------
knitr::opts_chunk$set(
  fig.align = "center",        # center align figures
  warnings = FALSE,            # prevents warnings from appearing in code chunk
  message = FALSE,             # prevents messages from appearing in code chunk
  echo = FALSE,                 # do not echo code in file
  results = "markup",            # hide results
  fig.align = "center",        # center align figures
  fig.keep = "all"             # keep all figs
)


# load all packages using xfun, package2 will install if not available
xfun::pkg_attach2(c("tidyverse",
                    "tidycensus",
                    "tmap",
                    "tigris",
                    "sf",
                    "here",
                    "haven",
                    "ggalt",
                    "ggExtra",
                    "extrafont",
                    "showtext", 
                    "scales", 
                    "ggalluvial",
                    "kableExtra"))



# set relative file path to project directory
here()



# environment settings ---------------------------------------------------------

# set environment options
options(scipen=999)

## Loading Google fonts (https://fonts.google.com/)
font_add_google("Lato", "lato")
showtext_auto()

R Code chunk:



knitr::include_graphics(here("output/figures/chapter1/top100/top100_2000.jpeg"))

Upvotes: 0

Views: 647

Answers (1)

Ceres
Ceres

Reputation: 177

I had the same issue and it seems you will need to pass the absolute path. See this .

Instead of using here maybe try using normalizePath in include_graphics.

So omit your call here(), which may not be propagating across chunks (I don't know, because I can't see your chunks), and do:

knitr::include_graphics(normalizePath("output/figures/chapter1/top100/top100_2000.jpeg"))

In Windows I add winslash = "/":

knitr::include_graphics(normalizePath("output/figures/chapter1/top100/top100_2000.jpeg", winslash = "/"))

Upvotes: 2

Related Questions