Using the following code, I get a pdf with the plot in desired place.
---
title: "Stackquestion"
author: "Author"
date: "`r Sys.Date()`"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
library(kableExtra)
```
## R Markdown
Whatever text
```{r cars}
dt <- mtcars[1:5, 1:6]
kbl(dt, booktabs = T)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
But when adding kable_styling the plot is placed in wrong position. Is there a way to get around this?
---
title: "Stackquestion"
author: "Author"
date: "`r Sys.Date()`"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
library(kableExtra)
```
## R Markdown
Whatever text
```{r cars}
dt <- mtcars[1:5, 1:6]
kbl(dt, booktabs = T) %>%
kable_styling(full_width = F) %>%
add_header_above(c("Some text also goes here"))
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
You can use the latex_options in kable_styling:
kable_styling(full_width = F, latex_options = "HOLD_position")
Related
I knit code below. and this is the output. long in kbl(0 for html, I want to have the term "continued" in flextable for docx too. what is the trick?
---
title: ''
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r echo=FALSE, message=FALSE, warning=FALSE}
library(flextable)
library(dplyr)
library(ggplot2)
mpg[, 1:4] %>%
flextable() %>%
set_caption(caption = "my caption") %>%
set_table_properties(width = 1, layout = "autofit")
```
I am writing a report by Rmarkdown. I want to have three images next to each other with different caption labels .
this is my code :
library(png)
library(grid)
library(gridExtra)
img1 <- rasterGrob(as.raster(readPNG("~/Desktop/GIS/project/jpg/yek.png")), interpolate = FALSE)
img2 <- rasterGrob(as.raster(readPNG("~/Desktop/GIS/project/jpg/do.png")), interpolate = FALSE)
grid.arrange(img1, img2, ncol = 2)
the problem is that there is no empty space between images and I don't have separate captions for each of them
thank you in advance for your help
Using latex package subfig, you can do this.
---
title: "Untitled"
author: "None"
date: '2022-07-26'
output:
pdf_document:
extra_dependencies: "subfig"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
#| fig.cap = "",
#| fig.subcap = c('Image 1', 'Image 2', 'Image 3'),
#| fig.ncol = 3,
#| out.width = "32%",
#| fig.align = "center"
library(png)
library(grid)
img1 <- rasterGrob(as.raster(readPNG("test.png")), interpolate = FALSE)
img2 <- rasterGrob(as.raster(readPNG("test.png")), interpolate = FALSE)
img3 <- rasterGrob(as.raster(readPNG("test.png")), interpolate = FALSE)
plot(img1)
plot(img2)
plot(img3)
```
I'm unable to scale a table using scale_down option from kableExtra if previously I modify the page size.
EDIT: I've rewritten the markdown file to make it more reproducible as suggested:
---
title: "test"
documentclass: article
output:
pdf_document:
fig_caption: yes
keep_tex: true
pandoc_args: --listings
latex_engine: xelatex
header-includes:
- \usepackage{float}
- \usepackage{graphicx}
- \usepackage{subfig}
- \usepackage{xcolor}
---
```{r setup, include=F}
library(knitr)
library(kableExtra)
library(tidyverse)
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, include = T, results='hide')
```
\clearpage
\eject \pdfpagewidth=297mm \pdfpageheight=210mm
```{r test, fig.show='asis', results='asis'}
do.call(cbind.data.frame, rep(mtcars, 10)) %>%
as.data.frame %>%
head(n = 10) %>%
kable(format = 'latex', booktabs = TRUE,
caption = 'mycaption') %>%
# kableExtra::landscape() %>%
kable_styling(latex_options = c('hold_position', 'scale_down'))
```
Consider this simple example
---
title: "Untitled"
output:
pdf_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
options(knitr.table.format = "latex")
```
## Slide with R Output
```{r , echo = FALSE ,warning = FALSE}
library(knitr)
library(kableExtra)
library(dplyr)
cars %>%
filter(dist < 5) %>%
kable('latex', booktabs = T, escape = F, col.names = c( "$\\alpha$" , "$\\beta$" ) ) %>%
kable_styling(latex_options = c("striped", "hold_position"),
full_width = T)
```
Now this correcly generates the following output
The issue is that the renaming in col.names is manual and very tedious when my dataframe has many columns.
I would like to be able to able to say " if you see this variable dist in the dataframe, then map it to $\alpha$. Otherwise leave as is.
How can this be done?
Note, I am rendering the file using rmarkdown::render()
Thanks!!
I need to create multiple tables in RMarkdown and style it with the kableExtra package. As an example, I have the iris dataset. My first table displays the first 20 rows, my second table the next 20 rows, and my third table next 20 rows... Below is the code:
---
title: ""
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r}
library(knitr)
library(kableExtra)
landscape(kable_styling(kable(iris[1:20, ], format = "latex", align = "c",
row.names = FALSE), latex_options = c("striped"), full_width = T))
landscape(kable_styling(kable(iris[21:40, ], format = "latex", align = "c",
row.names = FALSE), latex_options = c("striped"), full_width = T))
landscape(kable_styling(kable(iris[41:60, ], format = "latex", align = "c",
row.names = FALSE), latex_options = c("striped"), full_width = T))
```
It works well and it returns three tables, each one in a different sheet. In reality I have more than just three tables so I thought it would be a lot wiser to use a for loop and I made use of the answer given in this link R: why kable doesn't print inside a for loop?. Simple I put a line break after each print call as advised.
---
title: "untitled"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r}
library(knitr)
library(kableExtra)
for (i in 1:3) {
print(landscape(kable_styling(
kable(iris[20*(i-1)+1:20*i, ], format = "latex", align = "c",
row.names = FALSE), latex_options = c("striped"), full_width = T)))
cat("\n")
}
```
But it does not work. I guess that is because I encapsulated the kable command with the commands from the kableExtra package.
Is there who can make it work? I mean is there a way that can save me from typing?
Your existing code was nearly there. I think the only change needed is to add results='asis' to the chunk option (I don't think the extra newline is necessary). Here's the full RMarkdown content which works for me
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r results='asis'}
library(knitr)
library(kableExtra)
for (i in 1:3) {
print(landscape(kable_styling(
kable(iris[20*(i-1)+1:20*i, ], format = "latex", align = "c",
row.names = FALSE), latex_options = c("striped"), full_width = T)))
}
```