[R] issue with "strange" characters (readHTMLTable)
R.T.A.J.Leenders
r.t.a.j.leenders at rug.nl
Thu May 5 11:33:45 CEST 2011
Thank you. The line of code you give certainly resolves several of the
issues.
I didn't realize that font support is such a tough matter to realize. Let me
express my gratitude to those who provide this for us in R.
On 04-05-11, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
Oh, please!
This is about the contributed package XML, not R and not Windows.
Some of us have worked very hard to provide reasonable font support in R,
including on Windows. We are given exceedingly little credit, just
the brickbats for things for which we are not responsible. (We even work
hard to port XML to Windows for you, again with almost zero credit.)
That URL is a page in UTF-8, as its header says. We have provided many ways
to work with UTF-8 on Windows, but it seems readHTMLTable() is not making
use of them.
You need to run iconv() on the strings in your object (which as it has
factors, are the levels). When you do so, you will discover that page
contains characters not in your native charset (I presume, not having your
locale).
What you can do, in Rgui only, is
for (n in names(Islands)) Encoding(levels(Islands[[n]])) <-"UTF-8"
but likely there are still characters it will not know how to display.
On Wed, 4 May 2011, R.T.A.J.Leenders wrote:
>
> WinXP-x32, R-21.13.0
> Dear list,
> I have a problem that (I think) relates to the interaction between
Windows
> and R.
> I am trying to scrape a table with data on the Hawai'ian Islands, This is
my
> code:
> library(XML)
> u <- "[1]http://en.wikipedia.org/wiki/Hawaii"
> tables <- readHTMLTable(u)
> Islands <- tables[[5]]
> The output is (first set of columns):
> Island Nickname
> > Islands
> Island Nickname
> Location
>1 Hawai�ûi[7] The Big Island 19�ð34âÀòN
155�ð30âÀòWïûÿ / ïûÿ19.567
>�ðN 155.5�ðWïûÿ / 19.567; -155.5
>2 Maui[8] The Valley Isle 20�ð48âÀòN
156�ð20âÀòWïûÿ / ïûÿ20.8�ðN
>156.333�ðWïûÿ / 20.8; -156.333
>3 Kaho�ûolawe[9] The Target Isle 20�ð33âÀòN
156�ð36âÀòWïûÿ / ïûÿ20.55
>�ðN 156.6�ðWïûÿ / 20.55; -156.6
>4 L�na�ûi[10] The Pineapple Isle 20�ð50âÀòN
156�ð56âÀòWïûÿ / ïûÿ20.833�ðN 15
>6.933�ðWïûÿ / 20.833; -156.933
>5 Moloka�ûi[11] The Friendly Isle 21�ð08âÀòN
157�ð02âÀòWïûÿ / ïûÿ21.133�ðN 1
>57.033�ðWïûÿ / 21.133; -157.033
>6 O�ûahu[12] The Gathering Place 21�ð28âÀòN
157�ð59âÀòWïûÿ / ïûÿ21.467�ðN 1
>57.983�ðWïûÿ / 21.467; -157.983
>7 Kaua�ûi[13] The Garden Isle 22�ð05âÀòN
159�ð30âÀòWïûÿ / ïûÿ22.083
>�ðN 159.5�ðWïûÿ / 22.083; -159.5
>8 Ni�ûihau[14] The Forbidden Isle 21�ð54âÀòN
160�ð10âÀòWïûÿ / ïûÿ21.9�ðN
>160.167�ðWïûÿ / 21.9; -160.167
>
> As you can see, there are "weird" characters in there. I have also tried
> readHTMLTable(u, encoding = "UTF-16") and readHTMLTable(u, encoding =
> "UTF-8")
> but that didn't help.
> It seems to me that there may be an issue with the interaction of the
> Windows settings of the character set.
> sessionInfo() gives
> > sessionInfo()
> R version 2.13.0 (2011-04-13)
> Platform: i386-pc-mingw32/i386 (32-bit)
> locale:
> [1] LC_COLLATE=Dutch_Netherlands.1252 LC_CTYPE=Dutch_Netherlands.1252
> LC_MONETARY=Dutch_Netherlands.1252
> [4] LC_NUMERIC=C LC_TIME=Dutch_Netherlands.1252
> attached base packages:
> [1] stats graphics grDevices utils datasets methods base
> other attached packages:
> [1] XML_3.2-0.2
> >
> I have also attempted to let R use another setting by entering:
> Sys.setlocale("LC_ALL", "en_US.UTF-8"), but this yields the response:
> > Sys.setlocale("LC_ALL", "en_US.UTF-8")
> [1] ""
> Warning message:
> In Sys.setlocale("LC_ALL", "en_US.UTF-8") :
> OS reports request to set locale to "en_US.UTF-8" cannot be honored
> >
> In addition, I have attempted to make the change directly from the
windows
> command prompt, using: "chcp 65001" and variations of that, but that
didn't
> change anything.
> I have searched the list and the web and have found others bringing forth
a
> similar issues, but have not been able to find a solution. I looks like
this
> is an issue of how Windows and R interact. Unfortunately, all three
> computers at my disposal have this problem. It occurs both under
WinXP-x32
> and under Win7-x86.
> Is there a way to make R override the windows settings or can the issue
be
> solved otherwise?
> I have also tried other websites, and the issue occurs every time when
there
> is an é, ÃÂŒ, À, î, et cetera in the text-to-be-scraped.
> Thank you,
> Roger
>______________________________________________
>R-help at r-project.org mailing list
>[2]https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
[3]http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, [4]http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
References
1. http://en.wikipedia.org/wiki/Hawaii
2. https://stat.ethz.ch/mailman/listinfo/r-help
3. http://www.R-project.org/posting-guide.html
4. http://www.stats.ox.ac.uk/%7Eripley/
More information about the R-help
mailing list