Sunday 14 January 2018

R - Installation, first steps and styling an html table with mso-number-format

A SO question carrying 500 bounty is progressing, it turns out the OP generates an HTML using an R script. So now is a good time to download R and play around to try to win the bounty!

So I will post links to my investigation here.

Installation

So I need to download and install R. First resource is a Youtube R Tutorial #1 - Download, Installation, Setup - Statistical Programming Language R (6 mins) video. One is directed to https://cran.r-project.org/ for the download, I select Windows (oddly the url does not change) and the select base where its says 'This is what you want to install R for the first time.' On the download page click the Download R x.y.z for Windows (ii megabytes, 32/64 bit) link. Grant install rigths, select the language and agree the license and default install directory C:\Program Files\R\R-3.4.3, the default User installation and the default component selection with no customization. Take default Start Menu folder,R, and no additional tasks beyond the defaults. Then it installs and one has two startup icons R i386 3.4.3 and R x64 3.4.3. Upon running one should get the following screenshot

Installing htmltable Package

So after playing around in the console following the tutorials of the same youtube author given above I address myself to the problem in hand, i.e. the SO question that requires an html table to to be styled with mso-number-format:'\@'.

If you took the default directory (as I did) on installation then you will need to run R with admin rights if you want to install extra packages. So, next I run R with admin rights and I enter install.packages("htmlTable"). Then I get asked to select a CRAN mirror. Ensure you typed the library with correct casing or you'll get prompted to try again. Then install happens. If install is good then entering library(htmlTable) will not error.

I build a simple matrix in memory and then use the htmlTable function passing the matrix and the html file is created and a dialog asking me to choose a browser is thrown.


> A=matrix(c(2,4,3,1,5,7),nrow=2,ncol=3,byrow=TRUE)
> A
     [,1] [,2] [,3]
[1,]    2    4    3
[2,]    1    5    7
> htmlTable(A)

I choose Chrome, and voila my HTML table is produced with the matrix sitting there. The file is a random temp file created in a personal workspace temp folder, %USERPROFILE%\AppData\Local\Temp\RtmpQTBXwL\file4c7452f414e2.html

2 4 3
1 5 7

So now how to style the cells in the last column with mso-number-format:'\@'

So I got the matrix wrong first time, they need to have brackets because the problem revolves around Excel opening an html file with bracketed numbers and interpreting them as negatives. So let's try again and also we create a CSS matrix and then set the final column with mso-number-format:'\@' ...


A=matrix(c("(2)","(4)","(3)","(1)","(5)","(7)"),nrow=2,ncol=3,byrow=TRUE)
css_matrix <- matrix(data="",nrow=2,ncol=3)
css_matrix[,3] <- "mso-number-format:\"\\@\""
htmlTable(x=A,css.cell=css_matrix)

Opening the file in Excel shows the final column brackets maintained whilst first two columns the brackets got interpreted as negatives.



-2 -4 (3)
-1 -5 (7)

No comments:

Post a Comment