[R] Failed to read UTF-16LE file on Windows

Wush Wu wush978 at gmail.com
Wed Oct 28 14:02:58 CET 2015


Dear all,

Here is an answer I found.

```
library(stringi)
src <- readBin("orglist-100.CSV", "raw", file.info("orglist-100.CSV")$size)
src2 <- stri_encode(src, "UTF-16LE", "UTF-8")
con <- textConnection(src2)
answer <- read.table(con, header = TRUE, sep = ",")
```

Hope it will help someone in the future.

Wush


2015-10-27 21:24 GMT+08:00 Wush Wu <wush978 at gmail.com>:

> Dear all,
>
> I tried to run the following code on 3 different OS:
>
> ```
> download.file("
> https://raw.githubusercontent.com/wush978/DataScienceAndR/course/RBasic-07-Loading-Dataset/orglist-100.CSV",
> destfile = "orglist-100.CSV")
> con <- file("orglist-100.CSV", encoding = "UTF-16LE")
> src <- readLines(con)
> length(src) # should be 100
> ```
>
> On ubuntu and OS X, R correctly read 100 lines from the file. However, the
> windows will only read the first line with the following warning message:
>
> ```
> Warning message:
> In readLines(file("orglist-100.CSV", encoding = "UTF-16LE")) :
>   incomplete final line found on 'orglist-100.CSV'
> ```
>
> Is there any recommended way to read a local UTF-16LE file on windows?
>
> Thanks,
> Wush
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list