我已经尝试(徒劳)在Rmarkdown中使用ggplot生成图表。 代码如下:
```{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)
感谢您的帮助
更新:
根据你们中的一些人的建议,我添加了一个不同的块名称,我从Davit复制了我的数据代码(请参阅更新的代码)。 不幸的是,错误仍然存在。有趣的是,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"
答案 0 :(得分:3)
我也收到了这条消息。我的麻烦是文件路径太长了。我在太多的子文件夹中有我的R markdown文件,我的R markdown文件的名称太长了。一旦我减少了文件路径的长度,问题就解决了。我希望这对你有用。
答案 1 :(得分: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()
```
答案 2 :(得分:0)
我也收到此错误;我通过移动代码以将有问题的图形生成到自己的块中来解决了这个问题。
答案 3 :(得分:0)
我最近遇到了类似的问题。就我而言,问题是 防病毒 软件在尝试处理文件时阻止了 Rscript.exe
,而未创建 PNG 文件。因此,有时暂时禁用防病毒软件并检查是否可以解决问题会很有用。