I recently added this functionality to my htmlTable () in the Gmisc-package and the function is pretty simple:
print.htmlTable<- function(x, useViewer = TRUE, ...){ # Don't use viewer if in knitr if (useViewer && !"package:knitr" %in% search()){ htmlFile <- tempfile(fileext=".html") htmlPage <- paste("<html>", "<head>", "<meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\">", "</head>", "<body>", "<div style=\"margin: 0 auto; display: table; margin-top: 1em;\">", x, "</div>", "</body>", "</html>", sep="\n") cat(htmlPage, file=htmlFile) viewer <- getOption("viewer") if (!is.null(viewer) && is.function(viewer)){ # (code to write some content to the file) viewer(htmlFile) }else{ utils::browseURL(htmlFile) } }else{ cat(x) } }
RStudio recommends that you use getOption ("viewer") instead of the @Ramnath clause, raw RStudio :: viewer (). My solution also adds utils :: browserURL () if you are not using RStudio. I got an idea from this blog post.
source share