I am trying to make a RMarkdown report using bookdown::html_document2 to create numbered Fig. 1, Fig. 2, ... across the whole document. Here, I am using both the R-generated and the external figures. I have found that using include_graphics() will help to generate a proper Fig. X numbers, also including in numbering the external figures.
To get the script to work, I am declaring the root.dir = rprojroot::find_rstudio_root_file('C:/myRproject')) while my external figures are located within C:/myRproject/inImg. But in this case, R cannot find my external images anymore? Why is this and how can I properly claim the paths for my R Markdown input, and for external figures? Thank you!
Example:
---
title: "My awesome title"
author: "me"
date: "`r Sys.Date()`"
output:
bookdown::html_document2:
toc: true
toc_depth: 3
knit: (function(input, ...) {
rmarkdown::render(
input,
output_dir = "../outReports",
output_file = file.path("../outReports", glue::glue('test_{Sys.Date()}'
)))
})
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, tidy = TRUE, tidy.opts = list(comment = FALSE), message = F, warning = F)
knitr::opts_chunk$set(fig.width=6, fig.height=3)
library(knitr)
library(png)
```
```{css, echo=FALSE}
p.caption {
font-size: 0.8em;
}
```
```{r setup-root}
knitr::opts_knit$set(root.dir = rprojroot::find_rstudio_root_file('C:/myRproject'))
```
```{r read-libs, eval = TRUE, echo=FALSE, include = FALSE, warning = FALSE, error = FALSE }
# rm(list=ls())
getwd()
#### Source paths and functions -----------------------------------------------
source('myPaths.R') # already this one I can't find within the directory?
# Read pictures as part of teh R chunks
library(knitr)
library(png)
# Read Input data -------------------------------------------------------------------
#getwd()
load(file = "outData/dat1.Rdata")
```
## Including Plots
You can also embed plots, for example:
```{r, out.width = "50%", fig.cap = 'Add fig from internet'}
include_graphics("https://upload.wikimedia.org/wikipedia/commons/thumb/2/2e/MC_Drei-Finger-Faultier.jpg/330px-MC_Drei-Finger-Faultier.jpg")
```
```{r add-extern-plot2, fig.cap = 'my numbered caption'}
# All defaults
img1_path <- "C:/myRproject/inImg/my_extern_fig.png"
img1 <- readPNG(img1_path, native = TRUE, info = TRUE)
attr(img1, "info")
include_graphics(img1_path)
```
Related
library(summarytools)
library(stargazer)
view(dfSummary(DataV2,graph.col = TRUE), method = "render")
I don't think summarytools has a way to produce LaTeX directly, but it can produce Markdown output, and the rmarkdown package can convert that to LaTeX. For example:
---
title: "Untitled"
date: "17/02/2022"
output:
pdf_document:
keep_tex: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r results='asis'}
library(summarytools)
library(stargazer)
dfSummary(tobacco,
plain.ascii = FALSE,
style = 'grid',
graph.magnif = 0.85,
varnumbers = FALSE,
valid.col = FALSE,
tmp.img.dir = "/tmp")
```
Because I used the keep_tex: true option in the YAML, it outputs the .tex file as well as the PDF, and you could theoretically extract the LaTeX from there if you weren't using R Markdown for the rest of the document. It might not be easy, because of all the embedded figures.
I want to rotate table output by 90 degrees on pdf. I am using markdown to generate a report and kable to display the tables in a loop. If possible, I would like to continue using kable since there are lot of other things which are dependent on it that I haven't included in this MWE.
This is a simple example using iris dataset. I tried using landscape function from this post Rotate a table from R markdown in pdf
---
output: pdf_document
header-includes:
\usepackage{lscape}
\usepackage{pdfpages}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
Report page -
```{r results='asis'}
library(knitr)
library(kableExtra)
for (i in 1:3) {
print(landscape(kable_styling(
kable(iris[i:(i+5), ], format = "latex", align = "c", booktabs = TRUE,
longtable = TRUE, row.names = FALSE), latex_options = c("striped"), full_width = T)))
}
```
But this only rotates the page number keeping the table as it is.
I am actually looking for a solution which provides me the output in this way -
To clarify, all the pages with table data in it (3 for this example) should be rotated whereas rest of them should remain as it is. Also, I need longtable = TRUE in kable since in my actual example I am printing lot of rows.
Use package rotating
I added a simple example for you.
---
title: "test"
header-includes: \usepackage[figuresright]{rotating}
#or \usepackage[figuresleft]{rotating}
output:
pdf_document:
latex_engine: xelatex
---
```{r setup, include = FALSE}
library(flextable)
ft <- flextable(head(mtcars))
```
\begin{sidewaysfigure}
`r ft`
\end{sidewaysfigure}
```
Further you can modify it for your tasks ;)
I found another way using rotatebox.
---
output: pdf_document
header-includes:
\usepackage{lscape}
\usepackage{pdfpages}
\usepackage{graphicx}
\usepackage[figuresright]{rotating}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
Report page -
```{r results='asis', warning=FALSE, message=FALSE}
library(knitr)
library(kableExtra)
for (i in 1:3) {
cat('\\rotatebox{90}{')
print(kable(iris[i:(i+5), ], format = "latex", align = "c", booktabs = TRUE,
row.names = FALSE))
cat('}')
cat("\n\\newpage\n")
}
```
HOW get rendered openxml chunks inside cells of tables in RMD to DOCx ?
I just get same openxml text (mdt) inside tables instead of content ..
any suggestions please?
xml_test.RMD:
Updated Test RMD code :
---
output:
word_document:
reference_docx: ./template.docx
keep_md: yes
md_extensions: +raw_tex
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r, setuplib, include=FALSE}
#library(tidyverse)
library(knitr)
#library(rmarkdown)
#library(dplyr)
#library(stringr)
library(huxtable)
library(kableExtra)
#library(ftExtra)
```
```{r, xml-test, echo=FALSE, collapse = FALSE}
# XML CONTENT
text_xml <- "<w:p><w:r><w:t>Example text.</w:t></w:r></w:p>"
sanitize(text_xml, type = "rtf")
knitr::asis_output(" \n")
sanitize(text_xml, type = "html")
knitr::asis_output(" \n")
#sanitize(text_xml, type = "latex")
#knitr::asis_output(" \n")
#to_ht <- c(knitr::asis_output(mdt), knitr::asis_output(mdt))
to_ht <- c(text_xml)
#ht <- huxtable::as_hux(to_ht, add_colnames = TRUE, add_rownames = TRUE, escape_contents=FALSE, autoformat = FALSE)
ht <- huxtable::as_hux(text_xml, add_colnames = FALSE, add_rownames = FALSE, escape_contents=FALSE, autoformat = FALSE)
#markdown(ht) <- TRUE
#ht <- set_markdown(ht)
# BAD XML OUTPUT INSIDE CELLS
theme_grey(ht)
knitr::asis_output(" \n")
#knitr::asis_output(ht)
knitr::asis_output(" \n")
# NORMAL XML OUTPUT HERE:
knitr::asis_output(as.character(text_xml))
```
I found as_hux() has incorrect sanitize text in: " <w:p><w:r><w:t>Example text.</w:t></w:r></w:p>
"
instead of RAW or WORD in md:
"<w:p><w:r><w:t>Example text.</w:t></w:r></w:p>"
as result I have this issue with wrong output in WORD huxtable cell
QUESTION: how i can avoid this conversion in MD file ?
When I knit an Rmarkdown to Word and include a table saved as an image using flextable::save_as_image(), the function seems to also paste the path to my image above the table. How do I prevent the path from being included in the Word doc?
Here's a MWE:
---
title: "MWE"
output: word_document
---
```{r include=TRUE, message=FALSE, warning=FALSE, echo=FALSE, results='asis'}
library(flextable, quietly = TRUE)
library(webshot, quietly = TRUE)
ft <- flextable( head( mtcars ) )
ft <- autofit(ft)
save_as_image(x = ft, path = "myimage.png")
knitr::include_graphics("myimage.png")
```
Here's the output I get:
save_as_image returns the filename that you saved the image to. You can hide it with invisible().
invisible(save_as_image(x = ft, path = "myimage.png"))
I'm using the bookdown package with RMarkdown to generate web-based book similar to this, likewise with the option to download a pdf-version of the book.
I've included plotly graphs in my "book" which work nicely in the html-version of the book. Being interactive, the button "build book" throws an error when including pdf output in the YAML-header.
Based on this description I've found a workaround with a regular RMarkdown File to create pdfs with plotly graphs outputs. A minimal solution (outside bookdown) looks like this:
---
title: "test"
output:
pdf_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(plotly)
Sys.setenv("plotly_username" = "username")
Sys.setenv("plotly_api_key" = "API")
```
```{r cars}
library(plotly)
p <- plot_ly(x = c(1,2,3,4), y = c(2,4,1,3), type = 'scatter', mode = 'lines')
plotly_IMAGE(p, format = "png", out_file = "output.png")
```
![Caption for the picture.](output.png)
Is there a way to include this solution within bookdown, so that the graphs are automagically interactive in the html output and static (png) in the pdf output?
Based on this blogpost I was able to come up with a solution. Note, this only works
with the "knitr" button (see this post for a workaround)
with an internet connection (1) and Plotly Account (2)
(1) See this link to export static images locally (which I didn't get to work since I failed installing PhantomJS)
(2) Plotly has a user Quota of currently 100 plots / grids per user per day
---
title: "test"
output:
html_document: default
pdf_document: default
word_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(plotly)
library(pander)
Sys.setenv("plotly_username" = "YOUR PLOTLY USERNAME")
Sys.setenv("plotly_api_key" = "API KEY")
output <- knitr::opts_knit$get("rmarkdown.pandoc.to") # html / latex / docx
```
```{r, results="asis"}
print(output)
p <- plot_ly(x = c(1,2,3,4), y = c(2,4,1,3), type = 'scatter', mode = 'lines')
filename <- "output.png"
if(output %in% c("latex","docx")){
plotly_IMAGE(p, format = "png", out_file = filename)
pandoc.image(filename)
} else if(output == "html"){
p
} else(print("No format defined for this output filetype"))
```