2011
06.12

A while ago I was working on a piece of R code for the data analysis of my Master These project. While experiencing some encoding issues running the code on different platforms I decided to build in OS detection. After some Googling I found out there are several ways to do so. First of all we can search in the `version` variable:

> version

platform       x86_64-pc-linux-gnu          
arch           x86_64                       
os             linux-gnu                    
system         x86_64, linux-gnu            
status                                      
major          2                            
minor          10.1                         
year           2009                         
month          12                           
day            14                           
svn rev        50720                        
language       R                            
version.string R version 2.10.1 (2009-12-14)

version$os

or `.Platform`

> .Platform
$OS.type
[1] "unix"

$file.sep
[1] "/"

$dynlib.ext
[1] ".so"

$GUI
[1] "X11"

$endian
[1] "little"

$pkgType
[1] "source"

$path.sep
[1] ":"

$r_arch
[1] ""

.Platform$OS.type

or even `Sys.info()`

> Sys.info()

                                     sysname 
                                     "Linux" 
                                     release 
                         "2.6.32-30-generic" 
                                     version 
"#59-Ubuntu SMP Tue Mar 1 21:30:46 UTC 2011" 
                                    nodename 
                                "foo.bar.nl" 
                                     machine 
                                    "x86_64" 
                                       login 
                               "mtersmitten" 
                                        user 
                               "mtersmitten"

In my code I would use this like:

toUtf8 = function(column) 
{
  return (
    switch(class(column), 
      "character" = iconv(column, "LATIN1", "UTF-8"),
      "data.frame" = as.data.frame(apply(column, 2, iconv, "LATIN1", "UTF-8")),
    )
  );
}

if (Sys.info()["sysname"] == "Linux")
{
  items15["question"] = toUtf8(items15["question"])
}

I know, it’s not pretty but it works…

PS: am I the only one having encoding issues working on multiple OSses by the way?

No Comment.

Add Your Comment