From 870e706026e00de7e7a13f22a7e426da06771bd9 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 10 May 2023 15:05:13 -0500 Subject: [PATCH] Work around encoding issue (#1465) --- strings.qmd | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/strings.qmd b/strings.qmd index 0c23578..f985797 100644 --- a/strings.qmd +++ b/strings.qmd @@ -550,22 +550,26 @@ For example here are two inline CSVs with unusual encodings[^strings-7]: [^strings-7]: Here I'm using the special `\x` to encode binary data directly into a string. ```{r} -#| message: false +#| eval: false x1 <- "text\nEl Ni\xf1o was particularly bad this year" -read_csv(x1) +read_csv(x1)$text +#> [1] "El Ni\xf1o was particularly bad this year" x2 <- "text\n\x82\xb1\x82\xf1\x82\xc9\x82\xbf\x82\xcd" -read_csv(x2) +read_csv(x2)$text +#> [1] "\x82\xb1\x82\xf1\x82ɂ\xbf\x82\xcd" ``` To read these correctly, you specify the encoding via the `locale` argument: ```{r} -#| message: false -read_csv(x1, locale = locale(encoding = "Latin1")) +#| eval: false +read_csv(x1, locale = locale(encoding = "Latin1"))$text +#> [1] "El Niño was particularly bad this year" -read_csv(x2, locale = locale(encoding = "Shift-JIS")) +read_csv(x2, locale = locale(encoding = "Shift-JIS"))$text +#> [1] "こんにちは" ``` How do you find the correct encoding?