5

Is it possible to define encoding in R script?

Something like that (just example):

encoding("utf-8")

I know for Reopen with encoding in RStudio, but I am not looking for this.

nrussell
  • 18,382
  • 4
  • 47
  • 60
Anette
  • 90
  • 1
  • 5

2 Answers2

2
## x is intended to be in latin1
x <- "fa\xE7ile"
Encoding(x)
Encoding(x) <- "latin1"
x
xx <- iconv(x, "latin1", "UTF-8")
Encoding(c(x, xx))
c(x, xx)
Encoding(xx) <- "bytes"
xx # will be encoded in hex
cat("xx = ", xx, "\n", sep = "")

http://www.inside-r.org/r-doc/base/Encoding

https://stat.ethz.ch/R-manual/R-devel/library/base/html/Encoding.html

how to read data in utf-8 format in R?

Community
  • 1
  • 1
Hack-R
  • 22,422
  • 14
  • 75
  • 131
2

A workaround is to write another script that has the following code:

source('C:/Script.R', encoding = 'UTF-8')

replace "C:/Script.R" with your script path & name.

Then when you call this script, it will run 'Script.R' with UTF-8 encoding.

Titus Buckworth
  • 382
  • 3
  • 12