Search code examples
rencodingreadr

Read non-english text as data frame using readr package (encoding issue)


I have a text with data. The text is non-English with European-style numbers and tab-delimited values:

Ežeras  Plotas, ha  Gylis, m
Drūkšiai    4479,0  33,3
Dysnai  2439,4  6,0

I want to read it into R using functions from readr package, but I face an encoding issue in the resulting dataset.

The code:

Sys.setlocale(locale = "Lithuanian")

library(readr)

read_tsv(locale = locale(decimal_mark = ","),
"Ežeras     Plotas, ha  Gylis, m
Drūkšiai    4479,0  33,3
Dysnai  2439,4  6,0
")

The result:

# A tibble: 2 x 3
  `E\xfeeras`      `Plotas, ha` `Gylis, m`
  <chr>                   <dbl>      <dbl>
1 "Dr\xfbk\xf0iai"        4479        33.3
2 Dysnai                  2439.        6  

I also tried encoding = "native" and encoding = "unknown" inside the function locale(), but these options are not recognized.

I can write the data into a text file and read that file as well as use data.table::fread(), but these are not the options I am searching for.


devtools::session_info()

Session info --------------------------------------------------------------------
 setting  value                       
 version  R version 3.5.1 (2018-07-02)
 system   x86_64, mingw32             
 ui       RStudio (1.1.456)           
 language (EN)                        
 collate  Lithuanian_Lithuania.1257   
 tz       Europe/Helsinki             
 date     2018-10-15                  

Packages ------------------------------------------------------------------------
 package    * version    date       source                          
 assertthat   0.2.0      2017-04-11 CRAN (R 3.5.0)                  
 base       * 3.5.1      2018-07-02 local                           
 cli          1.0.1      2018-09-25 CRAN (R 3.5.1)                  
 compiler     3.5.1      2018-07-02 local                           
 crayon       1.3.4      2017-09-16 CRAN (R 3.5.0)                  
 datasets   * 3.5.1      2018-07-02 local                           
 devtools     1.13.6     2018-06-27 CRAN (R 3.5.1)                  
 digest       0.6.18     2018-10-10 CRAN (R 3.5.1)                  
 fansi        0.4.0      2018-10-05 CRAN (R 3.5.1)                  
 graphics   * 3.5.1      2018-07-02 local                           
 grDevices  * 3.5.1      2018-07-02 local                           
 hms          0.4.2.9001 2018-07-25 Github (tidyverse/hms@979286f)  
 memoise      1.1.0      2017-04-21 CRAN (R 3.5.0)                  
 methods    * 3.5.1      2018-07-02 local                           
 pillar       1.3.0      2018-07-14 CRAN (R 3.5.1)                  
 pkgconfig    2.0.2      2018-08-16 CRAN (R 3.5.1)                  
 R6           2.3.0      2018-10-04 CRAN (R 3.5.1)                  
 Rcpp         0.12.19    2018-10-01 CRAN (R 3.5.1)                  
 readr      * 1.1.1      2017-05-16 CRAN (R 3.5.1)                  
 rlang        0.2.2      2018-08-16 CRAN (R 3.5.1)                  
 rstudioapi   0.8        2018-10-02 CRAN (R 3.5.1)                  
 stats      * 3.5.1      2018-07-02 local                           
 tibble       1.4.2      2018-01-22 CRAN (R 3.5.0)                  
 tools        3.5.1      2018-07-02 local                           
 utf8         1.1.4      2018-05-24 CRAN (R 3.5.0)                  
 utils      * 3.5.1      2018-07-02 local                           
 withr        2.1.2      2018-09-05 Github (jimhester/withr@8b9cee2)
 yaml         2.2.0      2018-07-25 CRAN (R 3.5.1)   

Solution

  • encoding = stringi::stri_enc_get() should work. c.f.: https://stackoverflow.com/a/46999569/5397672

    read_tsv(locale = locale(decimal_mark = ",",
                             encoding = stringi::stri_enc_get()),
             "Ežeras     Plotas, ha  Gylis, m
    Drūkšiai    4479,0  33,3
    Dysnai  2439,4  6,0
    ")