I successfully generate tables using either ztable, xtable or htmlTable packages with no problems. But, I'd like to know whether these tables may be output as pdf documents. When I try to knit a pdf_output, the table is not displayed. Instead the information is displayed as several strings. I've tried changing the latex engine (using ztable) and other methods with no luck.
I've looked here:
http://www.mzan.com/article/29773068-rmarkdown-latex-table-output-difficulties.shtml
I've also looked in the vignettes, etc.
Example (RMarkdown):
---
output: pdf_document
---
```{r, message = F, results = 'asis'}
# will throw out Error: pandoc document conversion failed with error 43
library(ztable)
data(iris)
options(ztable.type="latex")
zt <- ztable(iris[1:5,], caption = "ztable")
zt <- addcgroup(zt,
cgroup = c("group 1", "group 2"),
n.cgroup = c(2,3))
print(zt)
```
```{r, message = F }
# since it's html, will produce text only
library(htmlTable)
data(mtcars)
colnames(mtcars) <- NULL
htmlTable(mtcars[1:5,], caption = "htmlTable",
cgroup = c("group 1", "group 2"),
n.cgroup = c(5,6))
```
Adding header-includes: \usepackage{colortbl} resolves the error for ztable. I don't think htmlTable has a latex engine.
full code:
---
output: pdf_document
header-includes: \usepackage{colortbl}
---
```{r, message = F, results = 'asis'}
library(ztable)
data(iris)
options(ztable.type="latex")
zt <- ztable(iris[1:5,], caption = "ztable")
zt <- addcgroup(zt,
cgroup = c("group 1", "group 2"),
n.cgroup = c(2,3))
print(zt)
```
```{r, message = F }
# since it's html, will produce text only
library(htmlTable)
data(mtcars)
colnames(mtcars) <- NULL
htmlTable(mtcars[1:5,], caption = "htmlTable",
cgroup = c("group 1", "group 2"),
n.cgroup = c(5,6))
```
Related
I am using the connectwidgets R package within a Quatro document to build a catalog page. I plan is to use the grid layout, however it results in a "grid list" rather than a 4x4 grid layout. I am hoping there is a small syntax issue that can be done to resolve my problem.
Here is a reproducable example:
---
title: "Home"
output: html_document
---
```{r message = FALSE, warning = FALSE, echo = FALSE}
library(connectwidgets)
library(dplyr, warn.conflicts = FALSE)
library(readr)
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
content <- readr::read_csv("./data/dataset-metadata.csv")
```
```{r}
df <- data.frame(
guid = c("abc100", "abc101", "abc102", "abc103"),
url = c("http://google.com", "http://nfl.com", "http://cbc.com", "http://linkedin.com"),
title = c("Dataset 1", "Dataset 2", "Dataset 3", "Dataset 4"),
app_mode = c("static", "static", "static", "static"),
owner_username = c("owner1", "owner1", "owner2", "owner3"),
updated_time = c("2022-12-26 12:00:00", "2022-12-26 15:15:00", "2022-12-27 09:45:00", "2028-12-28 16:20:00")
)
```
```{r}
df %>%
connectwidgets::rsc_grid()
```
The above code produces the following:
I'm super new to R and I'm struggling to create a header above the column names.
colnames(system_table) <- c("System 1", "System 2", "System 3","System 4")
I want to add a title above these that says "All Systems" but I can't seem to find the function to do so.
You can use something like this in Rmarkdown:
---
title: "Test"
output: html_document
---
```{r, echo=FALSE, warning=FALSE, message=FALSE}
library(magrittr)
library(knitr)
library(formattable)
system_table <- data.frame(v1 = c(1,2,3),
v2 = c(3,2,5),
v3 = c(5,3,2),
v4 = c(3,2,1))
colnames(system_table) <- c("System 1", "System 2", "System 3","System 4")
formattable(system_table) %>%
kable("html", escape = F, caption = "All Systems")
```
Output:
I'm trying to render a pander table in RMarkdown that includes superscript. Here is my current RMarkdown code:
```
---
title: "My Table"
output: html_document
---
```{r packages, include = FALSE}
library(tidyverse)
library(pander)
```
```{r my_table}
my_table <-
data.frame(
c("Sector 1",
"Sector 2",
"Sector 3"),
c("100%",
"29%",
"NA"))
pander(my_table,
col.names = c("Sector", "% Coverage"),
split.cells = 40,
keep.line.breaks = TRUE)
```
And here is the knit result:
I would like to add a superscripted letter "c" to the "NA" cell of the table, yielding an output like this:
You are looking to use the notation $$ NA^c $$, but this only works outside of the R chunk at the moment.
I am trying to create a table in RMarkdown that looks similar to the following example:
---
title: "Example"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r cars, echo=FALSE, message=FALSE, warning=FALSE, results='asis'}
library(Hmisc)
latex(mtcars, file = "", cgroup = c("mpg", "cyl"), n.cgroup = c(1,10))
```
I would like to group columns 2 through 10. Any ideas on how I can accomplish this with the Hmisc package or any other R package?
I think just using a blank header name for the first column gives you what you want:
latex(mtcars, file = "", cgroup = c("", "cyl"), n.cgroup = c(1,10))
Result:
Using my package:
library(huxtable)
hux_cars <- as_hux(mtcars, add_colnames = TRUE)
hux_cars <- insert_row(hux_cars, c('mtcars', 'cyl', rep('', 9)))
colspan(hux_cars)[1, 2] <- 10
align(hux_cars)[1, 2] <- 'centre'
bold(hux_cars)[1, ] <- TRUE
position(hux_cars) <- 'left'
quick_pdf(hux_cars)
Which produces:
I've been working with the ztable package for a while and knitting into pdf through RStudio. Upon adding sub column names (addSubColumnNames) that contain the percentage symbol, I get an \hline error. If I don't use the percentage symbol, everything runs fine. I've been trying to see if there is a package necessary for this with no luck. Any help is most welcome as usual.
Below a reproducible example:
---
output: pdf_document
header-includes:
- \usepackage{colortbl}
- \usepackage{multirow}
- \usepackage{graphicx}
- \usepackage{array}
- \usepackage{booktabs}
- \usepackage{tabularx}
- \usepackage{wrapfig}
- \usepackage{amsmath}
graphics: yes
---
```{r, message = F, results = 'asis', echo = F}
# will throw out Error
library(ztable)
data(iris)
options(ztable.type="latex")
zt = ztable(iris[1:5,], caption = "ztable", align = "cccccc", size = 3)
zt = addcgroup(zt,
cgroup = c("group 1", "group 2"),
n.cgroup = c(2,3))
zt = addSubColNames(zt, rep("f3 (C%)", 5))
zt
```
```{r, message = F, results = 'asis', echo = F}
# will be OK
library(ztable)
data(iris)
options(ztable.type="latex")
zt = ztable(iris[1:5,], caption = "ztable", align = "cccccc", size = 3)
zt = addcgroup(zt,
cgroup = c("group 1", "group 2"),
n.cgroup = c(2,3))
zt = addSubColNames(zt, rep("f3 (C)", 5))
zt
```
The error message is this and as I mentioned, it only appears when I add the % as a sub column name in ztable. Any help would be wonderful:
! Misplaced \noalign.
\hline ->\noalign
{\ifnum 0=`}\fi \let \hskip \vskip \let \vrule \hrule \let...
l.104 \hline
pandoc.exe: Error producing PDF from TeX source
Error: pandoc document conversion failed with error 43
Congrats ! The reproducible example is ok and it is clear.
I had this in the output
! LaTeX Error: File `titling.sty' not found.
Please check line 26 in your RMD. This is what works
---
output:
pdf_document:
keep_tex: yes
header-includes:
- \usepackage{colortbl}
- \usepackage{multirow}
- \usepackage{graphicx}
- \usepackage{array}
- \usepackage{booktabs}
- \usepackage{tabularx}
- \usepackage{wrapfig}
- \usepackage{amsmath}
graphics: yes
---
```{r, message = F, results = 'asis', echo = F}
# will throw out Error
library(ztable)
data(iris)
options(ztable.type="latex")
zt = ztable(iris[1:5,], caption = "ztable", align = "cccccc", size = 3)
zt = addcgroup(zt,
cgroup = c("group 1", "group 2"),
n.cgroup = c(2,3))
zt = addSubColNames(zt, rep("f3 (C\\%)", 5))
zt
```
```{r, message = F, results = 'asis', echo = F}
# will be OK
library(ztable)
data(iris)
options(ztable.type="latex")
zt = ztable(iris[1:5,], caption = "ztable", align = "cccccc", size = 3)
zt = addcgroup(zt,
cgroup = c("group 1", "group 2"),
n.cgroup = c(2,3))
zt = addSubColNames(zt, rep("f3 (C)", 5))
zt
```
The magic comes in by using using zt = addSubColNames(zt, rep("f3 (C\\%)", 5)) as in LaTeX '\%' is %
Regards