how to make a multiple line table with xtable, longtable - r

I am trying to to use xtable() and the longtable environment to display a multi=page table.
Currently when you run the code it will show "\scalebox " at the top and then it will cutoff columns of the table.
How do you remove the "\scalebox" at the top and show all the column in a readable manner?
Thank you.
here is the code:
---
title: "test"
output: pdf_document
keep_tex: TRUE
setspace: singlespacing
geometry: margin=1.1cm
header-includes:
- \usepackage{color}
- \usepackage{amsmath}
- \usepackage{xcolor}
- \usepackage{colortbl}
- \usepackage{tabulary}
- \usepackage{tabularx}
- \usepackage{longtable}
- \usepackage{ltxtable}
fig.lp: ('';character)
fig.pos: "H"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE , comment = NA, message= FALSE, warning = FALSE)
```
```{r one, include= FALSE}
n=600
table = data.frame(name = rep(" dakjf aslkj dklsfj aslj ldjs",n),
NUMERICFIELDNUMBERa = rep(10000,n),
NUMERICFIELDNUMBERb = rep(10000,n),
NUMERICFIELDNUMBERc = rep(10000,n),
NUMERICFIELDNUMBERd = rep("1250 + 52.33",n),
NUMERICFIELDNUMBERe = rep("25.6 + 36.25",n),
NUMERICFIELDNUMBERf = rep(10000,n),
NUMERICFIELDNUMBERg = rep(10000,n)
)
head(table)
colnames(table) = c("GROUP NAME",
"Numeric Field Number1",
"Numeric Field Number2",
"Numeric Field Number3",
"Numeric Field Number4",
"Numeric Field Number5",
"Numeric Field Number6",
"Numeric Field Number7"
)
rownames(table) = NULL
```
```{r echo= FALSE, comment = FALSE, message= FALSE, warning = FALSE, results='asis'}
library(xtable)
options(xtable.include.rownames=F)
options(xtable.scalebox=.7)
#options(xtable.tabular.environment = "tabulary")
#options(xtable.width = "7in")
options(xtable.comment=F)
options(xtable.floating = F)
t = xtable(table)
#align(t)= "cccccccc"#"cp{2cm}cp{2cm}cp{2cm}cp{2cm}cp{2cm}cp{2cm}cp{2cm}cp{2cm}" #rep("p{2cm}C",8)#"p{1cm}cccccccc"
#align(t) = "cp{3cm}p{3cm}p{3cm}p{3cm}p{3cm}p{3cm}p{3cm}"
print(t, sanitize.text.function = identity)
```

Related

How to knit markdown to PDF using R command?

Trying to knit an Rmarkdown document to PDF from another R script. When I use the Rstudio interface, and hit Knit to PDF, it works perfectly fine and generates the PDF. But when I use the rmarkdown::render command, it gives me an error:
Markdown:
---
geometry: margin=0.75in
output:
pdf_document: default
params:
set_title:
value: "Sample"
editor_options:
chunk_output_type: console
header-includes:
\usepackage{fancyhdr}
\usepackage{graphicx}
---
---
title: `r params$set_title`
---
```{r load_libraries, include=FALSE, echo=FALSE}
suppressPackageStartupMessages({
library(knitr)
library(tidyverse)
library(kableExtra)
})
```
\addtolength{\headheight}{2.0cm}
\fancypagestyle{plain}{}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
```{r set_options, include = FALSE, echo = FALSE, warning = FALSE}
knitr::opts_chunk$set(comment = NA)
`%>%` <- dplyr::`%>%`
```
# 1. Table
```{r, echo = FALSE, message = FALSE, warning = FALSE}
# sample data frame
df <- data.frame(x = c("a", "b", "c"),
y = c("a", "b", "c"))
# print table
kableExtra::kable(df, booktabs = T) %>%
kable_styling(full_width = TRUE, font_size = 8) %>%
row_spec(0, bold = T) %>%
column_spec(1, width = "1cm") %>%
column_spec(2, width = "2cm")
```
This is how I am running it:
rmarkdown::render(input = "~/Projects/test.Rmd",
params = list(set_title = "TEMP"),
output_dir = "~/Desktop/",
output_file = "test.pdf")
Error:
! LaTeX Error: Environment tabu undefined.
Error: LaTeX failed to compile /Users/rathik/Projects/test.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See test.log for more info.
The error is due to using kableExtra::kable because when I remove that function call, the error goes away. Any ideas how to resolve it?
Thank you #Dirk, based on your suggestion and the LaTeX documentation, I added the following in the header and it seems to be working as expected:
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{array}
\usepackage{multirow}
\usepackage{wrapfig}
\usepackage{float}
\usepackage{colortbl}
\usepackage{pdflscape}
\usepackage{tabu}
\usepackage{threeparttable}
\usepackage{threeparttablex}
\usepackage[normalem]{ulem}
\usepackage{makecell}
\usepackage{xcolor}

RMarkdown Change numbering for tables in PDF document

I want to change the numbering of the tables in the RMarkdown document so that all tables in the appendix have an "A-" in front of the number, thus: "Table A-2".
Only in the appendix. Otherwise with normal numbering ("Table 1").
However, I am not really getting anywhere.
Here is my reproducible example:\
---
title: "This is my title"
date: "`r Sys.setlocale(locale = 'English') ; format(Sys.time(), '%B %d, %Y')`"
output: pdf_document
---
```{r echo = F, message = F, warning = F}
library(tidyverse)
library(knitr)
``` #The hash mark must be removed!
# Results
```{r echo = F, message = F, warning = F}
tribble(~column1, ~column2,
"value1", 2,
"value2", 5
)%>%
kable(booktabs=T, caption = "This is the caption of the first table")
```
# Appendix
```{r echo = F, message = F, warning = F}
tribble(~column1, ~column2,
"value1", 6,
"value2", 8
)%>%
kable(booktabs=T, caption = "This is the caption of the second table")
```
This is really a LaTeX question, and I found the answer here.
You add these LaTeX lines after your Appendix title:
\setcounter{table}{0}
\renewcommand{\thetable}{A\arabic{table}}

Citing inside the cell of flextable

I'm trying to create a table with rmarkdown and flextable where I am citing various works.
my Rmakrdown file:
---
title: "Innovative title"
author: "Vag Vaf"
date: '2021-12-29'
bibliography: references.bib
csl: apa-6th-edition.csl
output:
bookdown::word_document2:
fig_caption: yes
pdf_document:
toc: true
toc_depth: 2
citation_package: natbib
keep_tex: true
extra_dependencies: rotating, bookmark
fontsize: 12pt
geometry: margin=1in
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(fig.retina = 3, warning = FALSE, message = FALSE)
```
```{r studies-table}
studies.table <- tibble (
Authors = c("#Author1",
"#Author2"),
Area = c("Area1",
"Area2")
)
ft <- flextable(studies.table)
```
in my references.bib:
#article{Author1,
author = {Author, Solo},
journal = {Journal of Reproducible Examples},
pages = {1--18},
title = {{Yet another Test Title}},
volume = {1},
year = {2022}
}
#article{Author2,
author = {Author, Two and Author, Four},
journal = {Journal of Reproducible Examples},
pages = {75--82},
title = {{Awesome title}},
volume = {1},
year = {2022}
}
I am trying with this code, but #Author1 and #Author2 are not converted to the actual citation. They are displayed as #Aurthor1 and #Author2 in the table. Is there any way to indicate that this should be converted to a citation?
The package ftExtra is required for markdown syntaxes work in flextable cells:
```{r setup, include=FALSE}
library(easypackages)
packages(
"tidyverse",
"flextable",
"ftExtra"
)
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(fig.retina = 3, warning = FALSE, message = FALSE)
```
```{r studies-table}
studies.table <- tibble(
Authors = c(
"#Author1",
"#Author2"
),
Area = c(
"Area1",
"Area2"
)
)
flextable(studies.table) %>%
ftExtra::colformat_md()
```

Adding icons to text unstable

I would like do add an icon to the text in one of the slides.
I've tried a few things but the end result is unstable. The icon are being printed in the html file, but If I add a slide or some text the the icons dissapear. Even if I revert what I've done with Ctrl+Z to previous working code the icons don't get printed...
I might be missing something, but the bookdown documentation didn't have anything on the subject...
---
title: 'Title'
author: "Author"
date: '`r format(Sys.time(), "%d %B, %Y")`'
output:
ioslides_presentation:
self_contained: true
incremental: false
---
```{r knitr_init, echo=FALSE, message=FALSE, warning=FALSE, cache=FALSE}
## Global options
library(knitr)
library(shiny)
opts_chunk$set(
cache = FALSE,
prompt = FALSE,
tidy = FALSE,
comment = NA,
message = FALSE,
warning = FALSE
)
library(tidyverse)
library(plotly)
```
## S1
`r shiny::tags$i(class = "fa fa-arrow-down",style = "color: rgb(0,166,90)")`
item 3
`r shiny::tags$i(class = "fa fa-arrow-down",style = "color: rgb(0,166,90)")`
item 2
## S2
```{r, echo=F}
data.frame(a=1:10, b=1:10) %>%
plot_ly(x=~a,y=~b)
```
Writing the icons as <i class="fa fa-arrow-down" style="color: rgb(0,166,90)"></i> didn't seem to work either.
One option is to use the package icon (more information here).
---
title: 'Title'
author: "Author"
date: '`r format(Sys.time(), "%d %B, %Y")`'
output:
ioslides_presentation:
self_contained: true
incremental: false
---
```{r knitr_init, echo=FALSE, message=FALSE, warning=FALSE, cache=FALSE}
## Global options
library(knitr)
library(shiny)
opts_chunk$set(
cache = FALSE,
prompt = FALSE,
tidy = FALSE,
comment = NA,
message = FALSE,
warning = FALSE,
echo=FALSE
)
library(tidyverse)
library(plotly)
library(icon)
```
## S1
```{r icon-style1}
icon_style(fontawesome("arrow-down", style = "solid"), scale = 2, fill = "#00A65A")
```
item 3
```{r icon-style2}
icon_style(fontawesome("arrow-down", style = "solid"), scale = 2, fill = "#00A65A")
```
item 2
## S2
```{r, echo=F}
data.frame(a=1:10, b=1:10) %>%
plot_ly(x=~a,y=~b)
```
-output
I just added two code chunks, one for each icon. Additionally, in your first r chunk, I added echo=FALSE to the opts_chunk$set and library(icon). In order to achieve the RGB color you chose, the HEX code was needed (#00A65A).

Default custom options

I would like to create a default chunk option for my documents, so that I can choose which chunks are included in the appendix. Here is my MWE:
```{r setup, include=FALSE, appendix=FALSE}
knitr::opts_chunk$set(echo=FALSE)
knitr::opts_chunk$set(appendix=TRUE)
```
```{r}
# some code included in the appendix
```
```{r appendix=FALSE}
# some code not included in the appendix
```
# Appendix
```{r, ref.label=knitr::all_labels(appendix), echo = T, eval = F}
```
I would like to include all the chunks that do not explicitly say appendix=TRUE. If I add that, it works as expected, but the default knitr::opts_chunk$set(appendix=TRUE) does not seem to work.
I am probably missing something in my setup. Any help is greatly appreciated.
You need to explicitly set:
Appendix <- TRUE in setOptions
you then set -
knitr::opts_chunk$set(appendix = TRUE)
now any chunk without Appendix = FALSE is included in the Appendix.
Note: It is important to include Appendix = FALSE in the Appendix chunk.
I hope the above helps, it is how I do this.
---
title: "47085866-2"
author: "Technophobe1"
date: "11/5/2017"
output:
html_document:
keep_md: yes
---
```{r setOptions, echo = FALSE, Appendix = FALSE}
Appendix <- TRUE
knitr::opts_chunk$set(echo = FALSE, message = FALSE, error = FALSE, warning = FALSE, results = 'hide')
knitr::opts_chunk$set(appendix = TRUE)
```
## Appendix = TRUE
```{r code}
# some code included in the appendix
setClass(
"CStruct",
slots = list(
powerLevel = "numeric",
size = "numeric"
)
)
CStructure <- new("CStruct", powerLevel = 5, size = 10)
CStructure
str(CStructure)
CStructure#powerLevel
CStructure#size
```
## Appendix = FALSE
```{r ref.label='code', Appendix = FALSE}
# some code not included in the appendix
setClass(
"CStruct2",
slots = list(
n = "numeric",
s = "character",
b = "logical"
)
)
CStructure2 <- new("CStruct2", n = c(2, 3, 5),
s = c("aa", "bb"),
b = c(TRUE, FALSE, TRUE) )
str(CStructure2)
CStructure2#n
CStructure2#s
CStructure2#b
```
# Code Appendix
```{r, ref.label=knitr::all_labels(Appendix), Appendix = FALSE, echo=TRUE, eval=FALSE}
## Code Appendix
```
Output:

Resources