can someone detect where the unicode character is hidden?

Hi,

I use Rmarkdown and bookdown to write my thesis currently and got stuck for several hours on an error I hope the community can help me solve. Below is a reproducible example (Rmarkdown source code). When rendering, I keep on getting the following error:

! LaTeX Error: Unicode character (U+2009) not set up for use with LaTeX (...) Execution halted

---
title: "Untitled"
output: pdf_document

date: "2023-11-20"
---

knitr::opts_chunk$set(echo = FALSE, warning=FALSE, message=FALSE)
library(rvest)
library(tidyverse)
library(kableExtra)

# download table and store in tbl
url <- "https://www.nature.com/articles/s41598-020-66404-z/tables/1" 
html_content <- read_html(url)
tables <- html_table(html_content, fill = TRUE)
tb <- tables[[1]]

# now I reformat the table according to the original (see url for original)
ref_names <- tb[[1]]
tb <- tb[c(2:3, 5:7, 9:11, 13:15, 17:19, 21:23, 25:27, 29:31, 33:35, 37:39, 41:43, 45:46, 48:49), ]
cap <- "Descriptive statistics for demographic variables of infants and mothers included in the present study."

# the colnames do not work with mutate_all so I store them temporarily
# also they contain a unicode character I remove before reusing them
cnames <- colnames(tb) 
str_detect(cnames, "\u2009")
cnames <- str_replace_all(cnames, "\u2009", " ")
colnames(tb) <- make.names(colnames(tb))
tb <- mutate_all(tb, function(x) str_replace_all(x, "\\\\([()])", "\\1"))
colnames(tb) <- cnames
table1 <- kbl(tb, booktabs = TRUE, caption = cap, escape = FALSE) %>%
    pack_rows("Gender", 1, 2) %>%
    pack_rows("Age (days) PRE", 3, 5) %>%
    pack_rows("Age (days) POST", 6, 8) %>%
    pack_rows("Maternal Age (years)", 9, 11) %>%
    pack_rows("Birthweight (grams)", 12, 14) %>%
    pack_rows("Breastfeeding (Birth - PRE)", 15, 17) %>%
    pack_rows("Breastfeeding (PRE - POST)", 18, 20) %>%
    pack_rows("Formula-feeding (Birth - PRE)", 21, 23) %>%
    pack_rows("Formula-feeding (PRE - POST)", 24, 26) %>%
    pack_rows("Proportion breastfeeding (Birth - PRE)", 27, 29) %>%
    pack_rows("Proportion breastfeeding (PRE - POST)", 30, 32) %>%
    pack_rows("Siblings", 33, 34) %>%
    pack_rows("C-Section", 35, 36) 
table1

I cannot see it. This code runs okay on my machine,.

~~---
title: "Hendrik"
output: pdf_document
date: "2023-11-20"

#| include: false
knitr::opts_chunk$set(echo = FALSE, warning=FALSE, message=FALSE)
library(rvest)
library(tidyverse)
library(kableExtra)

download table and store in tbl

url <- "https://www.nature.com/articles/s41598-020-66404-z/tables/1" 
html_content <- read_html(url)
tables <- html_table(html_content, fill = TRUE)
tb <- tables[[1]]

now I reformat the table according to the original (see url for original)

ref_names <- tb[[1]]
tb <- tb[c(2:3, 5:7, 9:11, 13:15, 17:19, 21:23, 25:27, 29:31, 33:35, 37:39, 41:43, 45:46, 48:49), ]
cap <- "Descriptive statistics for demographic variables of infants and mothers included in the present study."

the colnames do not work with mutate_all so I store them temporarily

also they contain a unicode character I remove before reusing them

cnames <- colnames(tb) 
str_detect(cnames, "\u2009")
cnames <- str_replace_all(cnames, "\u2009", " ")
colnames(tb) <- make.names(colnames(tb))
tb <- mutate_all(tb, function(x) str_replace_all(x, "\\\\([()])", "\\1"))
colnames(tb) <- cnames
table1 <- kbl(tb, booktabs = TRUE, caption = cap, escape = FALSE) %>%
    pack_rows("Gender", 1, 2) %>%
    pack_rows("Age (days) PRE", 3, 5) %>%
    pack_rows("Age (days) POST", 6, 8) %>%
    pack_rows("Maternal Age (years)", 9, 11) %>%
    pack_rows("Birthweight (grams)", 12, 14) %>%
    pack_rows("Breastfeeding (Birth - PRE)", 15, 17) %>%
    pack_rows("Breastfeeding (PRE - POST)", 18, 20) %>%
    pack_rows("Formula-feeding (Birth - PRE)", 21, 23) %>%
    pack_rows("Formula-feeding (PRE - POST)", 24, 26) %>%
    pack_rows("Proportion breastfeeding (Birth - PRE)", 27, 29) %>%
    pack_rows("Proportion breastfeeding (PRE - POST)", 30, 32) %>%
    pack_rows("Siblings", 33, 34) %>%
    pack_rows("C-Section", 35, 36) 
table1

R version 4.3.2 (2023-10-31) -- "Eye Holes"
Copyright (C) 2023 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

sessionInfo()
R version 4.3.2 (2023-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.3 LTS

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0

locale:
[1] LC_CTYPE=en_CA.UTF-8 LC_NUMERIC=C LC_TIME=en_CA.UTF-8
[4] LC_COLLATE=en_CA.UTF-8 LC_MONETARY=en_CA.UTF-8 LC_MESSAGES=en_CA.UTF-8
[7] LC_PAPER=en_CA.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C

time zone: America/New_York
tzcode source: system (glibc)

attached base packages:
[1] stats graphics grDevices utils datasets methods base

loaded via a namespace (and not attached):
[1] compiler_4.3.2 pryr_0.1.6 magrittr_2.0.3 lobstr_1.1.2 rsconnect_1.1.1
[6] cli_3.6.1 tools_4.3.2 glue_1.6.2 rstudioapi_0.15.0 Rcpp_1.0.11
[11] stringi_1.7.12 codetools_0.2-19 stringr_1.5.0 lifecycle_1.0.3 rlang_1.1.1
[16] pak_0.6.0

1 Like

Dear John, thanks for helping out. It is weird: if I copy your code and paste it, it also runs on my system. It seems that maybe I copied initially this character without realizing it. However, I searched for it a lot and could not find it. Weird...

This can be tricky as this character is a thin space: “ ” U+2009 Thin Space Unicode Character
So not visible easily

And unicode in latex requires special engine

1 Like

I could solve the issue as I described it above but it still remains an issue that I cannot print this to a pdf book using bookdown. I did not post this here earlier because I thought once I fix it in a simple Rmarkdown document it will also work in the bookdown project but there I get the error:

Error: Functions that produce HTML output found in document targeting latex output.
Please change the output type of this document to HTML.
If your aiming to have some HTML widgets shown in non-HTML format as a screenshot,
please install webshot or webshot2 R package for knitr to do the screenshot.
Alternatively, you can allow HTML output in non-HTML formats
by adding this option to the YAML front-matter of
your rmarkdown file:

  always_allow_html: true

Note however that the HTML output will not be visible in non-HTML formats.

Execution halted

Exited with status 1.

I already tried the suggested solution in the error message but then the table will totally loose its formatting. I also tried the solution suggested in the book:

install.packages("webshot")
webshot::install_phantomjs()

Any ideas would be welcome. Thank you!

This topic was automatically closed 7 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.

Sorry for the delay on this. Hopefully you searched the web and found a solution.

In case you did not found one, here are some more information.

This error means that you have a content in your document (probably a chunk) that will produce HTML only output. It could be for example a HTML widgets like plotly or leaflet.

This type of output is not supported in PDF obviously so you need to handle it.

Usually you should aim for a screenshot, and as the message is saying you need webshot or webshot2 install and working so that knitr can trigger a screenshot of the content for you, so that it is included as an image.

This should have worked. If webshot is installed, and phantomjs also, then it should work. Be sure to uninstall webshot2 maybe - the latter is newer and prefered as it uses chrome, but if for some reason it is not working or your prefer webshot with phantomjs, it will be used as a fallback.

You can force webshot by default by adding knitr's option knitr::opts_chunk$set(webshot = "webshot")

Hope it helps