knitr generating errors in document but generates figures correctly regardless Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How to use Cairo PNGs in R MarkdownI get “LaTeX Error: File `figure/minimal-boring-plots1' not found.” running knitr-minimal.lyxUsing knit2pdf with Rmd filesAchieving consistent figure font sizes with knitr, HTML/markdown and PDF/LatexPlot animation in knitr rmarkdownSize of font in ggplot plot changes in relation to plot using knitrrmarkdown error with ggplot and pngSuppress any emission of a particular warning messagePlots not displayed in pdf when rendering Rmarkdown from bash scriptKnitr/Pandoc conversion to PDF fails with “could not find image” errorHow to use Cairo PNGs in R Markdown

Special flights

Asymptotics question

If Windows 7 doesn't support WSL, then what is "Subsystem for UNIX-based Applications"?

Where is the Next Backup Size entry on iOS 12?

Is there hard evidence that the grant peer review system performs significantly better than random?

What is the origin of 落第?

Tannaka duality for semisimple groups

What order were files/directories output in dir?

Does the Mueller report show a conspiracy between Russia and the Trump Campaign?

How can I prevent/balance waiting and turtling as a response to cooldown mechanics

What does 丫 mean? 丫是什么意思?

AppleTVs create a chatty alternate WiFi network

How to ternary Plot3D a function

RSA find public exponent

What are the main differences between the original Stargate SG-1 and the Final Cut edition?

After Sam didn't return home in the end, were he and Al still friends?

Rationale for describing kurtosis as "peakedness"?

Why datecode is SO IMPORTANT to chip manufacturers?

Why is it faster to reheat something than it is to cook it?

Can two people see the same photon?

The test team as an enemy of development? And how can this be avoided?

Co-worker has annoying ringtone

In musical terms, what properties are varied by the human voice to produce different words / syllables?

Why weren't discrete x86 CPUs ever used in game hardware?



knitr generating errors in document but generates figures correctly regardless



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How to use Cairo PNGs in R MarkdownI get “LaTeX Error: File `figure/minimal-boring-plots1' not found.” running knitr-minimal.lyxUsing knit2pdf with Rmd filesAchieving consistent figure font sizes with knitr, HTML/markdown and PDF/LatexPlot animation in knitr rmarkdownSize of font in ggplot plot changes in relation to plot using knitrrmarkdown error with ggplot and pngSuppress any emission of a particular warning messagePlots not displayed in pdf when rendering Rmarkdown from bash scriptKnitr/Pandoc conversion to PDF fails with “could not find image” errorHow to use Cairo PNGs in R Markdown



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








4















I'm knitting an R Markdown file on macOS and I'm using knitr::opts_chunk$set(dev = c("png", "cairo_pdf")) to save the output of plots as PNG and PDF files simultaneously. I'm also using the Cairo PDF library since it can embed fonts correctly by default (see here)



When I knit and create a plot that uses a custom font, knitr correctly saves both the PNG and PDF files using Cairo:



figure output



However, in the actual knitted R Markdown document, R complains about missing fonts and provides dozens of warnings. This is odd, since it is working just fine behind the scenes.



Here's a MWE:



---
title: "So many warnings?"
---

```r setup, include=FALSE
knitr::opts_chunk$set(fig.path = "fig/", # Save images to a subdirectory
echo = FALSE, # Hide code for now
dpi = 300, # High resolution PNGs
# Save all figures as Cairo PDFs and PNGs
dev = c("png", "cairo_pdf"),
dev.args = list(png = list(type = "cairo")))
```

```r load-libraries
library(ggplot2)
```

```r warningless-plot
# This will save two files in the fig/ folder, both saved using Cairo:
# - fig/warningless-plot-1.png
# - fig/warningless-plot-1.pdf

ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point()
```

```r warningful-plot
# This will save two files in the fig/ folder, both saved *correctly* using Cairo:
# - fig/warningful-plot-1.png
# - fig/warningful-plot-1.pdf

# However, rmarkdown or knitr or something in the pipeline gets mad and throws
# a ton of warnings.

ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
theme_grey(base_family = "Comic Sans MS")
```


The figures themselves are saved correctly, but the HTML output is full of these warnings:



## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Comic Sans MS' not found in PostScript font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Comic Sans MS' not found in PostScript font database


Right now my solution is to add warning=FALSE to the chunk options for warningful-plot and all other chunks that generate plots with custom fonts. I'd like to know why these extra warnings are happening, though, and if there's a way to avoid getting warnings in the first place.










share|improve this question






















  • It seems that this may be a more widespread issue? github.com/yihui/knitr/issues/729

    – Andrew
    Mar 8 at 23:21











  • And another place where it crops up: github.com/hrbrmstr/hrbrthemes/issues/2

    – Andrew
    Mar 8 at 23:32

















4















I'm knitting an R Markdown file on macOS and I'm using knitr::opts_chunk$set(dev = c("png", "cairo_pdf")) to save the output of plots as PNG and PDF files simultaneously. I'm also using the Cairo PDF library since it can embed fonts correctly by default (see here)



When I knit and create a plot that uses a custom font, knitr correctly saves both the PNG and PDF files using Cairo:



figure output



However, in the actual knitted R Markdown document, R complains about missing fonts and provides dozens of warnings. This is odd, since it is working just fine behind the scenes.



Here's a MWE:



---
title: "So many warnings?"
---

```r setup, include=FALSE
knitr::opts_chunk$set(fig.path = "fig/", # Save images to a subdirectory
echo = FALSE, # Hide code for now
dpi = 300, # High resolution PNGs
# Save all figures as Cairo PDFs and PNGs
dev = c("png", "cairo_pdf"),
dev.args = list(png = list(type = "cairo")))
```

```r load-libraries
library(ggplot2)
```

```r warningless-plot
# This will save two files in the fig/ folder, both saved using Cairo:
# - fig/warningless-plot-1.png
# - fig/warningless-plot-1.pdf

ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point()
```

```r warningful-plot
# This will save two files in the fig/ folder, both saved *correctly* using Cairo:
# - fig/warningful-plot-1.png
# - fig/warningful-plot-1.pdf

# However, rmarkdown or knitr or something in the pipeline gets mad and throws
# a ton of warnings.

ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
theme_grey(base_family = "Comic Sans MS")
```


The figures themselves are saved correctly, but the HTML output is full of these warnings:



## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Comic Sans MS' not found in PostScript font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Comic Sans MS' not found in PostScript font database


Right now my solution is to add warning=FALSE to the chunk options for warningful-plot and all other chunks that generate plots with custom fonts. I'd like to know why these extra warnings are happening, though, and if there's a way to avoid getting warnings in the first place.










share|improve this question






















  • It seems that this may be a more widespread issue? github.com/yihui/knitr/issues/729

    – Andrew
    Mar 8 at 23:21











  • And another place where it crops up: github.com/hrbrmstr/hrbrthemes/issues/2

    – Andrew
    Mar 8 at 23:32













4












4








4


1






I'm knitting an R Markdown file on macOS and I'm using knitr::opts_chunk$set(dev = c("png", "cairo_pdf")) to save the output of plots as PNG and PDF files simultaneously. I'm also using the Cairo PDF library since it can embed fonts correctly by default (see here)



When I knit and create a plot that uses a custom font, knitr correctly saves both the PNG and PDF files using Cairo:



figure output



However, in the actual knitted R Markdown document, R complains about missing fonts and provides dozens of warnings. This is odd, since it is working just fine behind the scenes.



Here's a MWE:



---
title: "So many warnings?"
---

```r setup, include=FALSE
knitr::opts_chunk$set(fig.path = "fig/", # Save images to a subdirectory
echo = FALSE, # Hide code for now
dpi = 300, # High resolution PNGs
# Save all figures as Cairo PDFs and PNGs
dev = c("png", "cairo_pdf"),
dev.args = list(png = list(type = "cairo")))
```

```r load-libraries
library(ggplot2)
```

```r warningless-plot
# This will save two files in the fig/ folder, both saved using Cairo:
# - fig/warningless-plot-1.png
# - fig/warningless-plot-1.pdf

ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point()
```

```r warningful-plot
# This will save two files in the fig/ folder, both saved *correctly* using Cairo:
# - fig/warningful-plot-1.png
# - fig/warningful-plot-1.pdf

# However, rmarkdown or knitr or something in the pipeline gets mad and throws
# a ton of warnings.

ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
theme_grey(base_family = "Comic Sans MS")
```


The figures themselves are saved correctly, but the HTML output is full of these warnings:



## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Comic Sans MS' not found in PostScript font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Comic Sans MS' not found in PostScript font database


Right now my solution is to add warning=FALSE to the chunk options for warningful-plot and all other chunks that generate plots with custom fonts. I'd like to know why these extra warnings are happening, though, and if there's a way to avoid getting warnings in the first place.










share|improve this question














I'm knitting an R Markdown file on macOS and I'm using knitr::opts_chunk$set(dev = c("png", "cairo_pdf")) to save the output of plots as PNG and PDF files simultaneously. I'm also using the Cairo PDF library since it can embed fonts correctly by default (see here)



When I knit and create a plot that uses a custom font, knitr correctly saves both the PNG and PDF files using Cairo:



figure output



However, in the actual knitted R Markdown document, R complains about missing fonts and provides dozens of warnings. This is odd, since it is working just fine behind the scenes.



Here's a MWE:



---
title: "So many warnings?"
---

```r setup, include=FALSE
knitr::opts_chunk$set(fig.path = "fig/", # Save images to a subdirectory
echo = FALSE, # Hide code for now
dpi = 300, # High resolution PNGs
# Save all figures as Cairo PDFs and PNGs
dev = c("png", "cairo_pdf"),
dev.args = list(png = list(type = "cairo")))
```

```r load-libraries
library(ggplot2)
```

```r warningless-plot
# This will save two files in the fig/ folder, both saved using Cairo:
# - fig/warningless-plot-1.png
# - fig/warningless-plot-1.pdf

ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point()
```

```r warningful-plot
# This will save two files in the fig/ folder, both saved *correctly* using Cairo:
# - fig/warningful-plot-1.png
# - fig/warningful-plot-1.pdf

# However, rmarkdown or knitr or something in the pipeline gets mad and throws
# a ton of warnings.

ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
theme_grey(base_family = "Comic Sans MS")
```


The figures themselves are saved correctly, but the HTML output is full of these warnings:



## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Comic Sans MS' not found in PostScript font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Comic Sans MS' not found in PostScript font database


Right now my solution is to add warning=FALSE to the chunk options for warningful-plot and all other chunks that generate plots with custom fonts. I'd like to know why these extra warnings are happening, though, and if there's a way to avoid getting warnings in the first place.







r r-markdown knitr






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 23:13









AndrewAndrew

13.5k105279




13.5k105279












  • It seems that this may be a more widespread issue? github.com/yihui/knitr/issues/729

    – Andrew
    Mar 8 at 23:21











  • And another place where it crops up: github.com/hrbrmstr/hrbrthemes/issues/2

    – Andrew
    Mar 8 at 23:32

















  • It seems that this may be a more widespread issue? github.com/yihui/knitr/issues/729

    – Andrew
    Mar 8 at 23:21











  • And another place where it crops up: github.com/hrbrmstr/hrbrthemes/issues/2

    – Andrew
    Mar 8 at 23:32
















It seems that this may be a more widespread issue? github.com/yihui/knitr/issues/729

– Andrew
Mar 8 at 23:21





It seems that this may be a more widespread issue? github.com/yihui/knitr/issues/729

– Andrew
Mar 8 at 23:21













And another place where it crops up: github.com/hrbrmstr/hrbrthemes/issues/2

– Andrew
Mar 8 at 23:32





And another place where it crops up: github.com/hrbrmstr/hrbrthemes/issues/2

– Andrew
Mar 8 at 23:32












1 Answer
1






active

oldest

votes


















4














Answering my own question here…



According to a couple issues on GitHub (at knitr and hrbrthemes), this happens because knitr invisibly uses a null PDF device (pdf(NULL)) in the background when actually knitting. The default pdf() graphics device in R can't handle custom fonts, though, hence the warnings. Even though none of the visible graphics ever go through the base pdf() device, they still go through it invisibly I guess.



When knitting using dev = 'png', knitr will use an invisible png() device and no warnings will be thrown. It seems that using a cairo_pdf device at the same time breaks this and forces knitr to go back to an invisible, custom-font-less pdf() device.



We can fix this by forcing knitr to use an invisible png() device instead, based on this comment here:



# Use invisible NULL png() device
options(device = function(file, width, height)
png(tempfile(), width = width, height = height)
)

# knit options, including `dev = c("png", "cairo_pdf")`
knitr::opts_chunk$set(fig.path = "fig/", # Save images to a subdirectory
echo = FALSE, # Hide code for now
dpi = 300, # High resolution PNGs
# Save all figures as Cairo PDFs and PNGs
dev = c("png", "cairo_pdf"),
dev.args = list(png = list(type = "cairo")))


That options(device = ...) incantation makes the warnings go away.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072289%2fknitr-generating-errors-in-document-but-generates-figures-correctly-regardless%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    Answering my own question here…



    According to a couple issues on GitHub (at knitr and hrbrthemes), this happens because knitr invisibly uses a null PDF device (pdf(NULL)) in the background when actually knitting. The default pdf() graphics device in R can't handle custom fonts, though, hence the warnings. Even though none of the visible graphics ever go through the base pdf() device, they still go through it invisibly I guess.



    When knitting using dev = 'png', knitr will use an invisible png() device and no warnings will be thrown. It seems that using a cairo_pdf device at the same time breaks this and forces knitr to go back to an invisible, custom-font-less pdf() device.



    We can fix this by forcing knitr to use an invisible png() device instead, based on this comment here:



    # Use invisible NULL png() device
    options(device = function(file, width, height)
    png(tempfile(), width = width, height = height)
    )

    # knit options, including `dev = c("png", "cairo_pdf")`
    knitr::opts_chunk$set(fig.path = "fig/", # Save images to a subdirectory
    echo = FALSE, # Hide code for now
    dpi = 300, # High resolution PNGs
    # Save all figures as Cairo PDFs and PNGs
    dev = c("png", "cairo_pdf"),
    dev.args = list(png = list(type = "cairo")))


    That options(device = ...) incantation makes the warnings go away.






    share|improve this answer



























      4














      Answering my own question here…



      According to a couple issues on GitHub (at knitr and hrbrthemes), this happens because knitr invisibly uses a null PDF device (pdf(NULL)) in the background when actually knitting. The default pdf() graphics device in R can't handle custom fonts, though, hence the warnings. Even though none of the visible graphics ever go through the base pdf() device, they still go through it invisibly I guess.



      When knitting using dev = 'png', knitr will use an invisible png() device and no warnings will be thrown. It seems that using a cairo_pdf device at the same time breaks this and forces knitr to go back to an invisible, custom-font-less pdf() device.



      We can fix this by forcing knitr to use an invisible png() device instead, based on this comment here:



      # Use invisible NULL png() device
      options(device = function(file, width, height)
      png(tempfile(), width = width, height = height)
      )

      # knit options, including `dev = c("png", "cairo_pdf")`
      knitr::opts_chunk$set(fig.path = "fig/", # Save images to a subdirectory
      echo = FALSE, # Hide code for now
      dpi = 300, # High resolution PNGs
      # Save all figures as Cairo PDFs and PNGs
      dev = c("png", "cairo_pdf"),
      dev.args = list(png = list(type = "cairo")))


      That options(device = ...) incantation makes the warnings go away.






      share|improve this answer

























        4












        4








        4







        Answering my own question here…



        According to a couple issues on GitHub (at knitr and hrbrthemes), this happens because knitr invisibly uses a null PDF device (pdf(NULL)) in the background when actually knitting. The default pdf() graphics device in R can't handle custom fonts, though, hence the warnings. Even though none of the visible graphics ever go through the base pdf() device, they still go through it invisibly I guess.



        When knitting using dev = 'png', knitr will use an invisible png() device and no warnings will be thrown. It seems that using a cairo_pdf device at the same time breaks this and forces knitr to go back to an invisible, custom-font-less pdf() device.



        We can fix this by forcing knitr to use an invisible png() device instead, based on this comment here:



        # Use invisible NULL png() device
        options(device = function(file, width, height)
        png(tempfile(), width = width, height = height)
        )

        # knit options, including `dev = c("png", "cairo_pdf")`
        knitr::opts_chunk$set(fig.path = "fig/", # Save images to a subdirectory
        echo = FALSE, # Hide code for now
        dpi = 300, # High resolution PNGs
        # Save all figures as Cairo PDFs and PNGs
        dev = c("png", "cairo_pdf"),
        dev.args = list(png = list(type = "cairo")))


        That options(device = ...) incantation makes the warnings go away.






        share|improve this answer













        Answering my own question here…



        According to a couple issues on GitHub (at knitr and hrbrthemes), this happens because knitr invisibly uses a null PDF device (pdf(NULL)) in the background when actually knitting. The default pdf() graphics device in R can't handle custom fonts, though, hence the warnings. Even though none of the visible graphics ever go through the base pdf() device, they still go through it invisibly I guess.



        When knitting using dev = 'png', knitr will use an invisible png() device and no warnings will be thrown. It seems that using a cairo_pdf device at the same time breaks this and forces knitr to go back to an invisible, custom-font-less pdf() device.



        We can fix this by forcing knitr to use an invisible png() device instead, based on this comment here:



        # Use invisible NULL png() device
        options(device = function(file, width, height)
        png(tempfile(), width = width, height = height)
        )

        # knit options, including `dev = c("png", "cairo_pdf")`
        knitr::opts_chunk$set(fig.path = "fig/", # Save images to a subdirectory
        echo = FALSE, # Hide code for now
        dpi = 300, # High resolution PNGs
        # Save all figures as Cairo PDFs and PNGs
        dev = c("png", "cairo_pdf"),
        dev.args = list(png = list(type = "cairo")))


        That options(device = ...) incantation makes the warnings go away.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 9 at 0:00









        AndrewAndrew

        13.5k105279




        13.5k105279





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072289%2fknitr-generating-errors-in-document-but-generates-figures-correctly-regardless%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

            Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

            Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved