Potato
Potato

Reputation: 35

Knitr not displaying graph when out.width or out.height set

I've already posted this question, but it lacked a MWE, so I've decided to delete the old question and post a new one, since the old one had to be almost completely rewritten.

I'm trying to typeset an .Rmd research report into pdf using knitr. I'd like to include a graph created in ggplot.

If I include the out.width or out.height chunk options, the graph isn't present in the output pdf document.

Here's the MWE:

1) data creation:

datas <- data.frame(x = rep(1:30), y = rep(40:69))
save(datas, file = 'datas.RData')

2) Main file - file.Rmd:

---
author: "NN"
title: "Title"
fontsize: 14pt
geometry: margin=2cm
output: pdf_document
header-includes: |
    \usepackage{caption}
    \usepackage{bookman}
    \usepackage{multirow}
    \usepackage{array}
    \usepackage[htt]{hyphenat}
    \usepackage{booktabs}
    \usepackage{longtable}
    \usepackage[table]{xcolor}
    \usepackage{wrapfig}
    \usepackage{float}
    \usepackage{colortbl}
    \usepackage{pdflscape}
    \usepackage{tabu}
    \usepackage{threeparttable}
    \usepackage{threeparttablex}
    \usepackage[normalem]{ulem}
    \usepackage{makecell}
    \captionsetup[table]{width=\textwidth}
    \renewcommand{\tablename}{Tablica}
    \renewcommand{\figurename}{Slika}
---
```{r setup, echo = F, include = F}
library(knitr)
library(kableExtra)
# knitr setup
opts_chunk$set(prompt = T, background = '#E5E8E8', dpi = 600, fig.width = 12, dev = 'pdf')
options(digits = 3)

# packages
library(tidyverse)
library(data.table)
library(wrapr)
library(readxl)
library(magrittr)
library(psych)
library(ggpubr)

# loading the data
load('datas.RData')
```
sample text

```{r desk_stat, child = 'file_d.Rmd'}
```

3) Sub-file (the one containing the plot) - file_d.Rmd:

```{r, fig.cap = '\\label{bar-znanje-visina}Some label.', cache = T, out.width = '.49\\linewidth'}
ggplot(datas,
       aes(x = x)) +
    geom_bar()
```

If I typeset it with these options, the image in the .md file is included with:

<embed src="figure/unnamed-chunk-1-1.pdf" title="\label{bar-znanje-visina}Some label." alt="\label{bar-znanje-visina}Some label." width=".49\linewidth" type="application/pdf" />

An the output pdf looks like this:

output with out.width

If I typeset the output without the out.width option, the image in the .md file is included with:

![\label{bar-znanje-visina}Some label.](figure/unnamed-chunk-1-1.pdf)

And the output pdf looks like this:

output without out.width

I'm knitting the .Rmd file with

Rscript -e 'library(knitr); opts_knit$set(out.format = "latex"); knit("file.Rmd")'

Session info:

R version 3.4.3 (2017-11-30)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Manjaro Linux

Matrix products: default
BLAS: /usr/local/lib64/R/lib/libRblas.so
LAPACK: /usr/local/lib64/R/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_US.utf8        LC_NUMERIC=C              
 [3] LC_TIME=hr_HR.UTF-8        LC_COLLATE=en_US.utf8     
 [5] LC_MONETARY=hr_HR.UTF-8    LC_MESSAGES=en_US.utf8    
 [7] LC_PAPER=hr_HR.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=hr_HR.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] kableExtra_0.9.0 knitr_1.20       nvimcom_0.9-72  

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.16      rstudioapi_0.7    xml2_1.2.0        magrittr_1.5     
 [5] hms_0.4.2         rvest_0.3.2       munsell_0.4.3     viridisLite_0.3.0
 [9] colorspace_1.3-2  R6_2.2.2          rlang_0.2.0       plyr_1.8.4       
[13] stringr_1.3.0     httr_1.3.1        tools_3.4.3       htmltools_0.3.6  
[17] rprojroot_1.3-2   digest_0.6.15     tibble_1.4.2      readr_1.1.1      
[21] evaluate_0.10.1   rmarkdown_1.9     stringi_1.1.7     compiler_3.4.3   
[25] pillar_1.2.1      scales_0.5.0      backports_1.1.2   pkgconfig_2.0.1  

EDIT: Update

I tried typesetting Yihui's minimal .Rnw example and it works just fine. I also tried modifying it so that it display the graph from the MWE, and it also works. I've also noticed that setting keep_tex = true doesn't do anything when compiling an .Rmd file. No tex file is created.

Upvotes: 2

Views: 508

Answers (2)

Ralf Stubner
Ralf Stubner

Reputation: 26823

Instead of using knitr::knit() it is easier to use rmarkdown::render(). You can either call it directly

Rscript -e 'rmarkdown::render("file.Rmd")'

or indirectly using an example script from the littler package

render.r file.Rmd

on the command line.

Upvotes: 1

Ajjit Narayanan
Ajjit Narayanan

Reputation: 692

If you're always knittng to a pdf, you can try setting out.width = '49%' in the code chunk settings. You can also try adjusting the fig.width = setting in the code chunk instead, which takes in the size of the figure width in inches. If all that doesn't work I would also take a look at the grid.arrange() function from the gridExtra package

Also your problem is not reproducible. When I run your code on my Win10 machine, I get the following output.

reproduced_example

Upvotes: 1

Related Questions