2015-12-07 114 views
6

我試過(徒勞)在Rmarkdown中用ggplot生成圖表。 代碼如下:ggplot和png的rmarkdown錯誤

```{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 

``` 

出針織的這工作得很好,但是當一個RMD文件中執行(無論是作爲HTML或PDF),我總是收到此錯誤信息

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 

我也嘗試通過將圖表保存爲PNG並將其作爲圖片加載後解決該問題。還沒有結果(見Error with loading png in Rmd file

感謝您的幫助

UPDATE:

以下一些你們的我加了一個不同的塊名稱的建議,我複製從達維特的代碼在我的數據(看到更新的代碼)。 不幸的是,錯誤仍然存​​在。有趣的是,knitr不能編寫png,但可以在代碼所在的同一個文件夾中編寫csv(我測試過它)。

最後,我測試了運行這個非常相同的代碼到我的C驅動器和(驚喜!)它的工作原理。但是,這對我來說效率不高,因爲我不想依賴特定的機器,因此我需要與其他人共享此項工作(因此必須使用網絡驅動器)。而且,所有其他軟件包/代碼在網絡驅動器中都能正常工作,只有這個png()似乎是個問題。

在此先感謝您的幫助! --- 標題:新文件 作者:我 輸出: 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() 

``` 

錯誤輸出:

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 

我knitr版本1.11(它應該是最新的)和R版本是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" 
+1

在右邊的rmarkdown選項卡中有兩個較小的選項卡:輸出和問題。在問題中說什麼?你有什麼樣的文件權限? – rawr

+1

特別是'ggplot'是否導致問題?你能編織一個基本情節嗎?你可能沒有寫入文件夾的權限? – Axeman

+0

@rawr問題中的消息是:錯誤在PNG(...,res = dpi,units =「in」):無法啓動png()設備調用: ... in_dir - > plot2dev - > do.call - > - > png。此外:警告消息:1:在png(...,res = dpi,units =「in」)中:無法打開文件'XXXX/figure-html/unnamed-chunk-3-1.png'進行寫入。 2:在png(...,res = dpi,units =「in」)中:打開設備失敗 – Dani

回答

1

我有這個山姆問題一次。下面的代碼工作。您的頭文件不好或者沒有打包:由於您沒有提供該信息,因此很難說清楚。另外,請在下次發佈示例數據。

下面是可用的完整代碼(至少對我而言)。如果它沒有在你的機器上運行,發佈你的數據和完整的Rmd腳本,我會盡力幫忙。

--- 
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

1

我得到這個消息也是如此。我的麻煩是文件路徑太長。我有太多的子文件夾裏有我的R markdown文件,我的R markdown文件的名字太長了。一旦我縮短了文件路徑的長度,問題就解決了。我希望這對你有用。