I also want to transform an R markdown into both an html and a .docx/.odt with figures at the good size and resolution. Until now, I found that the best way to do this is define explicitly the resolution and size of the graphs in the .md document (dpi, fig.width and fig.height options). If you do this you have good graphs usable for publication and the odt/docx is ok. The problem if you use dpi much higher than the default 72 dpi, is that the graphs will look too big in the html file. Here are 3 approaches I have used to handle this (NB I use R scripts with spin() syntax):
1) use out.extra ='WIDTH="75%"' in knitr options. This will force all graphs of the html to occupy 75% of the window width. This is a quick solution but not optimal if you have plots with very different sizes. (NB I prefer working with centimetres rather than inches, hence the /2.54 everywhere)
library(knitr)
opts_chunk$set(echo = FALSE, dev = c("png", "pdf"), dpi = 400,
fig.width = 8/2.54, fig.height = 8/2.54,
out.extra ='WIDTH="75%"'
)
data(iris)
#' # Iris datatset
summary(iris)
boxplot(iris[,1:4])
#+ fig.width=14/2.54, fig.height=10/2.54
par(mar = c(2,2,2,2))
pairs(iris[,-5])
2) use out.width and out.height to specify the size of the graphs in pixels into the html file. I use a constant "sc" to scale down the size of the plot into the html output. This is the more precise approach but the problem is that for each graph you have to define both fig.witdth/height and out.width/height and this is really boaring ! Ideally you should be able to specify in the global options that e.g. out.width = 150*fig.width (where fig.width changes from chunk to chunk). Maybe something like that is possible but I don't know how.
#+ echo = FALSE
library(knitr)
sc <- 150
opts_chunk$set(echo = FALSE, dev = c("png", "pdf"), dpi = 400,
fig.width = 8/2.54, fig.height = 8/2.54,
out.width = sc*8/2.54, out.height = sc*8/2.54
)
data(iris)
#' # Iris datatset
summary(iris)
boxplot(iris[,1:4])
#+ fig.width=14/2.54, fig.height=10/2.54, out.width= sc * 14/2.54, out.height= sc * 10/2.54
par(mar = c(2,2,2,2))
pairs(iris[,-5])
Note that for these two solution, I think that you can't transform directly your md file into odt with pandoc (the figures are not included). I transform the md into html and then the html into odt (didn't tried for docx).
Something like that (if the previous R scripts is names "figsize1.R") :
library(knitr)
setwd("/home/gilles/")
spin("figsize1.R")
system("pandoc figsize1.md -o figsize1.html")
system("pandoc figsize1.html -o figsize1.odt")
3) Simply compile your document twice, once with low dpi value (~96) for the html output and once with high resolution (~300) for the odt/docx output. This is my preferred way now. The main disadvantage is that you must compile twice but this is not reallya problem to me since I generally need the odt file only at the very end of the job to provide to end users. I compile regularly the html during the work with the html notebook button in Rstudio.
#+ echo = FALSE
library(knitr)
opts_chunk$set(echo = FALSE, dev = c("png", "pdf"),
fig.width = 8/2.54, fig.height = 8/2.54
)
data(iris)
#' # Iris datatset
summary(iris)
boxplot(iris[,1:4])
#+ fig.width=14/2.54, fig.height=10/2.54
par(mar = c(2,2,2,2))
pairs(iris[,-5])
Then compile the 2 outputs with the following script (NB here you can directly transform the md file into html):
library(knitr)
setwd("/home/gilles")
opts_chunk$set(dpi=96)
spin("figsize3.R", knit=FALSE)
knit2html("figsize3.Rmd")
opts_chunk$set(dpi=400)
spin("figsize3.R")
system("pandoc figsize3.md -o figsize3.odt")
knitr
. – daroczigknitr
because the sizes are well in the output html file. – Stéphane Laurent