Can I view an HTML table in a view pane?

I would like to know if there is any function that simplifies the visualization of the html object in the RStudio viewing panel. For example, I would like to know if it is possible to view the html table in the view pane.

library("Quandl") library("knitr") df <- Quandl("FBI_UCR/USCRIME_TYPE_VIOLENTCRIMERATE") kable(head(df[,1:9]), format = 'html', table.attr = "class=nofluid") 
+5
source share
3 answers

Here is a quick way to do it in RStudio

 view_kable <- function(x, ...){ tab <- paste(capture.output(kable(x, ...)), collapse = '\n') tf <- tempfile(fileext = ".html") writeLines(tab, tf) rstudio::viewer(tf) } view_kable(head(df[,1:9]), format = 'html', table.attr = "class=nofluid") 

If the kable function can return an object of class kable , then it would be possible to rename view_kable as print.kable , in which case just calling the function kable would open the table in the viewer. If you think this is useful, please continue and specify the function request on the knitr github page.

+6
source

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.

+4
source

As explained on this RStudio support page , the key should use tempfile() :

Please note that the view pane can only be used for local web content. This content can be either static HTML files written to a temporary directory session (i.e. files with paths generated by a temporary function file) or a local web application.

See my answer to this question for an example with blue bones.

+1
source

All Articles