Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rmarkdown error with ggplot and png

I have tried (in vain) to produce a chart with ggplot in Rmarkdown. The code is the following:

```{r,echo=FALSE}
#fig.width=12,fig.height=6
panel2$PlotSize<-round(log(panel2$BSFA0200),0)- min(round(log(panel2$BSFA0200),0))+1# set size of dots
panel2$PlotSize[panel2$PlotSize==-Inf]<-NA
panel2$PlotColour<-ifelse(panel2$PlotSize<7,1,panel2$PlotSize)
panel2$PlotSize<-as.factor(panel2$PlotSize)
panel2$PlotColour<-as.factor(panel2$PlotColour)

g1<-ggplot(data=panel2,aes(x=NFR,y=PROF7*100,size=PlotSize,colour=PlotSize))+ geom_point()

g1

```

Out of knits this works fine, however when executed within a Rmd file (either as html or pdf) I always get this error message

processing file: 1Profti_model.Rmd
  |..                                                               |   4%
  ordinary text without R code

  |.....                                                            |   8%
label: setup (with options) 
List of 1
 $ include: logi FALSE

  |........                                                         |  12%
  ordinary text without R code

  |..........                                                       |  15%
label: unnamed-chunk-1 (with options) 
List of 3
 $ echo   : logi FALSE
 $ warning: logi FALSE
 $ message: logi FALSE


Attaching package: 'dplyr'

The following objects are masked from 'package:stats':

    filter, lag

The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

Loading required package: zoo

Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric

  |............                                                     |  19%
   inline R code fragments

  |...............                                                  |  23%
label: unnamed-chunk-2 (with options) 
List of 1
 $ echo: logi FALSE

  |..................                                               |  27%
  ordinary text without R code

  |....................                                             |  31%
label: unnamed-chunk-3 (with options) 
List of 1
 $ echo: logi FALSE

Quitting from lines 98-109 (1Profti_model.Rmd) 
Error in png(..., res = dpi, units = "in") : unable to start png() device
Calls: <Anonymous> ... in_dir -> plot2dev -> do.call -> <Anonymous> -> png
In addition: Warning messages:
1: Removed 55 rows containing missing values (geom_point). 
2: In png(..., res = dpi, units = "in") :
  unable to open file '1Profti_model_files/figure-html/unnamed-chunk-3-1.png' for writing
3: In png(..., res = dpi, units = "in") : opening device failed

Execution halted

I also tried to work around the problem by saving the chart in a png and load it as picture afterwards. Also no results (see Error with loading png in Rmd file)

Thanks for your help

UPDATE:

Following the suggestions from some of you guys I added a different chunk name and I replicated the code from Davit on my data (see updated code). Unfortunately the error is still there. Interestingly, knitr cannot write a png but can write a csv in the same folder where the code is (I tested it).

Finally, i tested running this very same code onto my C drive and (surprise!) it works. However, this is for me not very efficient as I don't want to be dependent on a specific machine and I need to share this work with others (so network drive is a must). Moreover, all other package/code work fine in the network drive, only this png() seems to be an issue.

Thanks in advance for you help! --- title: New Document author: Me output: html_document ---

```{r prova,echo=FALSE, results='asis', message = FALSE, error = FALSE, warning= FALSE}
#.libPaths("D:/xxxx/packages")
require(ggplot2)

 panel2 <- data.frame(BSFA0200 = rnorm(100),
                        NFR = rnorm(100),
                        PROF7 = rnorm(100))

panel2$PlotSize<-round(log(panel2$BSFA0200),0)- min(round(log(panel2$BSFA0200),0))+1# set size of dots
panel2$PlotSize[panel2$PlotSize==-Inf]<-NA
panel2$PlotColour<-ifelse(panel2$PlotSize<7,2,panel2$PlotSize)

write.csv(panel2[1:100,c('BSFA0200',"NFR","PROF7")],file="test.csv")

g1 <- ggplot(data = panel2,
             aes(x = NFR,
                 y = PROF7 * 100,
                 size = factor(PlotSize),
                 colour = factor(PlotSize)
                 ))

g1 + geom_point()

```

Error output:

Loading required package: ggplot2

Quitting from lines 9-32 (test.Rmd) 
Error in png(..., res = dpi, units = "in") : unable to start png() device
Calls: <Anonymous> ... in_dir -> plot2dev -> do.call -> <Anonymous> -> png
In addition: Warning messages:
1: Removed 35 rows containing missing values (geom_point). 
2: In png(..., res = dpi, units = "in") :
  unable to open file 'test_files/figure-html/prova-1.png' for writing
3: In png(..., res = dpi, units = "in") : opening device failed
Execution halted

My knitr version is 1.11 (it should be the latest) and R version is 3.2.2

> R.Version()
$platform
[1] "i386-w64-mingw32"

$arch
[1] "i386"

$os
[1] "mingw32"

$system
[1] "i386, mingw32"

$status
[1] ""

$major
[1] "3"

$minor
[1] "2.2"

$year
[1] "2015"

$month
[1] "08"

$day
[1] "14"

$`svn rev`
[1] "69053"

$language
[1] "R"

$version.string
[1] "R version 3.2.2 (2015-08-14)"

$nickname
[1] "Fire Safety"
like image 407
Dani Avatar asked Dec 07 '15 17:12

Dani


2 Answers

I got this message as well. The trouble with mine was that the file path was too long. I had my R markdown file in too many sub folders and the name of my R markdown file was too long. Once I reduced the length of the file path, the problem was resolved. I hope that works for you.

like image 71
Justin Mahoney Avatar answered Nov 11 '22 06:11

Justin Mahoney


I had this same issue once. The code below works. You either had bad header or didn't call the packages: it's hard to tell since you did not provide that information. Also, please post example data next time.

Here is the full code that works (at least for me). If it doesn't run on your machine, post your data and full Rmd script and I'll try to help.

---
title: New Document
author: Me
output:
  html_document
---

```{r,echo=FALSE, results='asis', message = FALSE, error = FALSE, warning= FALSE}
require(ggplot2)

panel2 <- data.frame(BSFA0200 = rnorm(100),
                      NFR = rnorm(100),
                      PROF7 = rnorm(100),
                      PlotSize = factor(rep(1:10, 10)),
                      PlotColour = factor(1:100))

g1 <- ggplot(data = panel2,
             aes(x = NFR,
                 y = PROF7 * 100,
                 size = PlotSize,
                 colour = PlotSize))

g1 + geom_point()
```

enter image description here

like image 2
Davit Sargsyan Avatar answered Nov 11 '22 05:11

Davit Sargsyan