1

I am trying to create .pdf and .doc to generate report from the following code. there are two problems in total:

  1. with .pdf output R is working out but once i knit with pdf then it run whole code and generate .pdf file with 0 kb. I am not able to open this file.

  2. .doc output In .doc file i want to change layout of pages and want to use pagebreak but its not working out. I also want to write code for page layout(landscape or portrait), but its not working. As i can only create the portrait page but table which i created with flextable is not autofitting in page.

I installed MIKTEX but then table and graph is floating in documents. so i uninstalled it. I dont understand what is "cairo out of memory" in error.

---
title: "Bla bla bla"
author: "Kishan"
date: "`r format(Sys.time(), '%d.%m.%Y')`"
output:
  pdf_document: default
  word_document: default
  html_document: default
---

# Global option --------

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = FALSE, messAgegr = FALSE,
                      comment=FALSE, warning=FALSE, 
                      results = "asis", dpi=600,
                      fig.width= 15, fig.height=8  ,
                       dev = "cairo_pdf"
                      )

options(kableExtra.latex.load_packages=TRUE,
        kableExtra.auto_format=FALSE)

library(tidyr)
library(dplyr)
library(janitor)
library(ggplot2)
library(readxl)
library(knitr)
library(kableExtra)
library(tinytex)

library(flextable)


options(tinytex.verbose = TRUE)


ggpt <- theme_bw() +
  theme(#text=element_text(size=rel(1.7)),
    axis.title.x=element_text(size=rel(1.5)),
    axis.title.y=element_text(size=rel(1.5)),
    axis.text.x=element_text(size=rel (1.5)),
    axis.text.y=element_text(size=rel (1.5)),
    strip.text.x=element_text(size=rel(1.2)),
    strip.text.y=element_text(size=rel(1.2)),
    legend.text=element_text(size=rel (1.3)),
    legend.title=element_text(size=rel(1.3)),
    plot.title=element_text(size=rel  (1.5)))


doc_type <- knitr::opts_knit$get("rmarkdown.pandoc.to")

if(is.null(doc_type)) {
  doc_type <- "raw"
}

```

```{r}

stdtabl <- function(data, caption=NULL, format="raw") {
    if(format %in% c("latex", "html")) {
        data %>%
            knitr::kable(booktabs=TRUE,
                         format=format,
                         digits=0,
                         caption=caption) %>%
            kable_styling(bootstrap_options=c("striped", "hover", "condensed", "responsive"),
                          full_width=FALSE,
                          font_size=12,
                          fixed_thead=TRUE,
                          position="center") %>%
            column_spec(column=1,
                        bold=TRUE)
    } else if(format == "docx") {
        ## use flextable as kableExtra does not handle Word documents
        data %>%
            flextable() %>%
            theme_zebra() %>%
            autofit() %>%
            font(fontname="Calibri")

    } else {  "raw"
        data %>%
            knitr::kable()
    }
}

```
```{r}

mobitab <- EQBase %>% 
  tabyl(Zeitpunkt, eq5d5lMobil) %>%
  adorn_percentages("row") %>%
  adorn_pct_formatting(digits = 0 ) %>%
  adorn_ns(position = "front") %>% 
  select(Zeitpunkt,'mob_1','mob_2','mob_3','mob_4','mob_5','V1')

stdtabl(mobitab, caption= 'sdsdyfwerfew', format=doc_type)

```


```{r}

barplotmob <- EQBase %>% 
  mutate(eq5d5lMobil= factor(eq5d5lMobil, levels = c("","mob_5", "mob_4", "mob_3", "mob_2", "mob_1"))) %>%
  group_by(VisitID, eq5d5lMobil) %>% 
  summarise(Count= n()) %>% 
  mutate(Percentage = Count/sum(Count)*100, round(Percentage, digits=0)) %>% 
  mutate(lab_ypos = 100-(cumsum(Percentage) - 0.5 * Percentage))

  ggplot(barplotmob, aes(x=factor(VisitID),  y=Percentage, fill = factor(eq5d5lMobil))) + 
  geom_bar(stat="identity", width = 0.7)+
  geom_text (aes(y = lab_ypos, label = paste0(round(Percentage, digits=0)), group =eq5d5lMobil), 
             color = "black",  size = 3)


```

>Quitting from lines 238-274 (EQ5D_1.rmd) 
Fehler in (function (filename = if (onefile) "Rplots.pdf" else "Rplot%03d.pdf",  : 
  unable to start device 'cairo_pdf'
Ruft auf: <Anonymous> ... in_base_dir -> in_dir -> plot2dev -> do.call -> <Anonymous>
Zusätzlich: Warnmeldungen:
1: Calling `as_tibble()` on a vector is discouraged, because the behavior is likely to change in the future. Use `enframe(name = NULL)` instead.
This warning is displayed once per session. 
2: In (function (filename = if (onefile) "Rplots.pdf" else "Rplot%03d.pdf",  :
  cairo error 'out of memory'
Ausführung angehalten
r2evans
  • 141,215
  • 6
  • 77
  • 149
kishan
  • 11
  • 2
  • 1
    The `"unable to start device 'cairo_pdf'"` could be several things, some related to a specific OS, some not. In this case, the `'out of memory'` is also revealing. https://stackoverflow.com/a/55945632/3358272 suggests it could be a transient error (upgrading R fixed it), but that does not seem satisfying to me. Unfortunately we cannot reproduce the behavior since we do not have your data. If you reduce your data size, do you still get the error? – r2evans May 20 '19 at 14:31
  • 1
    @r2evans I'm the author of [the thread you mentioned](https://stackoverflow.com/a/55945632/10215301) and what solved me most is adding `dev: cairo_pdf` when producing PDF. – Carlos Luis Rivera May 21 '19 at 07:17
  • 1
    @kishan What will happen if you add `dev: cairo_pdf` in your YAML section when you want to get PDF? Moreover, you may need to put one or more blank line(s) between code chunks. I mean, you would have to put blank lines before the chunk where `mobitab` is defined. – Carlos Luis Rivera May 21 '19 at 07:21
  • Thanks for piping in, @CarlosLuisRivera, I see that I glossed over that portion of the fix. kishan, what luck have you had? – r2evans May 21 '19 at 14:30

0 Answers0