[R] Latex or html output for freq() in prettyR

Jim Lemon jim at bitwrit.com.au
Fri Feb 13 09:32:41 CET 2009


christiaan pauw wrote:
> Hi Everybody
> I need to create a lot of frequency tables with frequencies and percentages
> (and cumilative freq and % as well) for a report. freq() in prettyR give
> more or less what I need.
> I am trying to export the result of freq() to html but the html doesn't look
> look the console output.
>
> See the following example
>
> library(prettyR)
>
> library(Hmisc)
>
> x <- matrix(sample(1:3, 12, replace=TRUE), nrow=3, dimnames=list(c('A','B',
> 'C'),letters[1:4])) # create a 3x4 matrix with random numbers beteen 1 and 3
>
> x # see what it looks like
>
> fx=freq(x) # create frequency table with frequencies and % per column
>
> fx # looks pretty good, now I want to export it to latex or html
>
> html(fx) # I expected this to look like console output but it doesn't. Can
> anything be done?
>
> fm=as.matrix(fx) #It can't be coerced into a matrix or table (as far as i
> can see)
>
> class(fm) # it's class remains freq
>
>
> Does anyone now what to do?
>   
Hi Christiaan,
One way to get the same output in HTML as in the console is to use the 
htmlize function in the prettyR package. htmlize runs an R script (so 
that you can edit the script until it does just what you want) and 
formats the output of the script as an HTML page. So if you had a script 
that contained:

library(prettyR)
x <- matrix(sample(1:3, 12, replace=TRUE), nrow=3,
 dimnames=list(c('A','B','C'),letters[1:4])) beteen 1 and 3
x # see what it looks like
fx<-freq(x) # create frequency table with frequencies and % per column
fx # looks pretty good, now I want to export it to latex or html

and you named it "testfreq.R". You could get the output you want with:

htmlize("testfreq.R",title="My HTML output")

the output will appear in a file named, "testfreq.html" unless you name 
it something else with the HTMLbase argument to htmlize. The "html" 
function from the Hmisc package is probably expecting a data frame and 
not the output of the freq function. However, it does the best it can 
and tries to make a data.frame out of a pig's ear, constructing and 
displaying the result in an HTML table. Hats off to Professor Harrell 
for writing the function so that it does anything more than barf in the 
situation.

Jim



More information about the R-help mailing list