Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to view an HTML table in the viewer pane?

Tags:

r

rstudio

I would like to know if there is any function which makes it easy to visualize an html object in the RStudio's viewer pane. For instance, I would like to know if it would be possible to view an html table in the viewer pane.

library("Quandl")
library("knitr")
df  <-  Quandl("FBI_UCR/USCRIME_TYPE_VIOLENTCRIMERATE")
kable(head(df[,1:9]), format = 'html', table.attr = "class=nofluid")
like image 204
PAC Avatar asked Apr 04 '14 13:04

PAC


People also ask

What does the viewer do in RStudio?

Introduction. The RStudio IDE includes a data viewer that allows you to look inside data frames and other rectangular data structures. The viewer also allows includes some simple exploratory data analysis (EDA) features that can help you understand the data as you manipulate it with R.

How do you customize a table in HTML?

To create table in HTML, use the <table> tag. A table consist of rows and columns, which can be set using one or more <tr>, <th>, and <td> elements. A table row is defined by the <tr> tag. To set table header, use the <th> tag.


3 Answers

I have a solution that works for kable tables.

kable(iris) %>% kableExtra::kable_styling()

This is automatically displayed in the viewer pane. No need for tempfile.

like image 157
mzuba Avatar answered Oct 17 '22 03:10

mzuba


I have this functionality in my htmlTable package and the function is rather 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 the getOption("viewer") instead of @Ramnath's suggestion, the raw RStudio::viewer(). My solution also adds the utils::browserURL() in case you are not using RStudio. I got the idea from this blog post.

like image 30
Max Gordon Avatar answered Oct 17 '22 03:10

Max Gordon


Here is a quick way to do this 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 one could rename view_kable as print.kable in which case merely calling the kable function would open the table in the viewer. If you think this is useful, please go ahead and file a feature request on the knitr github page.

like image 9
Ramnath Avatar answered Oct 17 '22 02:10

Ramnath