How to View an HTML Table in the Viewer Pane

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

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.

How to view html in R-Studio?

One option is to create an RMD file (Create a new file from Rstudio with the option File -> New File -> R Markdown...)

Sample Image

and then specify the results = 'asis' in the chunk. Click on Knit -> Knit to HTML

-RMD file content

---
title: "Ztable"
author: "akrun"
date: "01/11/2020"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

```{r ztable, results = 'asis', echo = FALSE}
suppressPackageStartupMessages(library(moonBook))
x=table(acs$Dx,acs$smoking)
suppressPackageStartupMessages(library(ztable))
suppressPackageStartupMessages(library(magrittr))
options(ztable.type="html")
z=ztable(x)

print(z,caption="Table 1. Basic Table")

```

-output

Sample Image


Or another option if we don't want to create an RMD file is to create a function similar to the one showed here

view_ztable <- function(x, ...){
tab <- paste(capture.output(ztable(x, ...)), collapse = '\n')
tf <- tempfile(fileext = ".html")
writeLines(tab, tf)
rstudioapi::viewer(tf)
}
view_ztable(x, format = 'html', table.attr = "class=nofluid")

-output in viewer

Sample Image



Related Topics



Leave a reply



Submit