[R] unwanted as.integer

Sundar Dorai-Raj sundar.dorai-raj at PDF.COM
Fri Jul 30 00:06:41 CEST 2004



Jack Tanner wrote:
>  > a <- sqlQuery(irrdb, "select count(field) from mytable where field = 1")
>  > print(a)
>  count(field)
> 1            8
>  > paste(a)
> [1] "as.integer(8)"
> 
> Why the as.integer() representation? I later pass the result into this 
> write.html.table(), and what I get is rows of as.integer()... when all I 
> want is the integer itself.
> 
> as.integer(31)     as.integer(21)     as.integer(34)     as.integer(86)
> as.integer(7)     as.integer(6)     as.integer(15)     as.integer(28)
> 
> 
> write.html.table <- function(t, file= "", capt= "The Default Title",
>                            append = FALSE) {
>  head <- paste("<table>\n<caption>", capt, "</caption>\n")
>  cat(head, file= file, append= append)
>  if (is.null(rownames(t))) {
>    rownames(t) <- rep("", nrow(t))
>  }
>  tp <- rbind(c("<tr><td></td>", colnames(t)),
>              cbind(unlist(lapply(rownames(t), function(x) {
>                paste("<tr><td>", x)})), t))
>  write.table(tp, sep= "<td>", file= file, , eol= "</tr>\n",
>              row.names=FALSE,
>              col.names=FALSE,
>              append=TRUE, quote=FALSE)
>  cat("</tr>\n</table>\n", file= file, append= TRUE)
> }
> 

Not sure about what sqlQuery is doing but you can wrap your return value 
in a eval(parse(text = x)) to evaluate the "as.integer(.)" string. As in,

a <- eval(parse(text = a))


HTH,
--sundar




More information about the R-help mailing list