简体   繁体   English

错误:无法启动 png() 设备

[英]Error : Unable to start png() device

I am using Windows. When trying to plot a graph on png device, it fails with the error.我正在使用 Windows。当尝试 plot png 设备上的图形时,它失败并出现错误。

My code:我的代码:

png("C:\\plot1.png", width = 480, height = 480, units = "px", bg = "white")

par(mar= c(4, 4, 2, 1))

hist(pwrcon$Global_active_power,col = "red", main = "Global Active Power", xlab = "Global Active Power (kilowatts)")

dev.off()

Error:错误:

Error in png("C:\\plot1.png", width = 480, height = 480, units = "px",  : 
  unable to start png() device
In addition: Warning messages:
1: In png("C:\\plot1.png", width = 480, height = 480, units = "px",  :
  unable to open file 'C:\plot1.png' for writing
2: In png("C:\\plot1.png", width = 480, height = 480, units = "px",  :
  opening device failed

Can anyone help me in getting this resolved?谁能帮我解决这个问题?

Thanks in advance提前致谢

I cannot explain why, but I once found that when the folder path in which my RStudio project was saved was a very long character string, the png device would fail.我无法解释为什么,但我曾经发现当我的 RStudio 项目保存的文件夹路径是一个很长的字符串时,png 设备会失败。 When I shortened the folder path it worked.当我缩短文件夹路径时,它起作用了。

I had the same issue while working in an r-markdown document.我在处理 r-markdown 文档时遇到了同样的问题。

The issue in my case had something to do with viewing the Chunk Output Inline.我的问题与查看块输出内联有关。 When I switched to viewing the Chunk Output in Console, it worked just fine.当我切换到在控制台中查看块输出时,它工作得很好。

I got also this error: "Error in (function (filename = "Rplot%03d.png", width = 480, height = 480, : unable to start png() device"我也得到了这个错误:“(函数中的错误(文件名=“Rplot%03d.png”,宽度= 480,高度= 480,:无法启动png()设备)

The name of the .Rmd file that I've been working on was containing some nonenglish characters, so removing them has been helpful in my case.我一直在处理的 .Rmd 文件的名称包含一些非英语字符,因此删除它们对我来说很有帮助。

Had the same problem on a PC.在PC上遇到了同样的问题。 The problem was that there was an antivirus program with "Safe files" enabled, which blocked Rstudio from creating graphics files.问题是有一个启用了“安全文件”的防病毒程序,它阻止了 Rstudio 创建图形文件。 The antivirus didn't display any information when blocking, so it doesn't give you any clues really.杀毒软件在拦截时没有显示任何信息,所以它并没有真正给你任何线索。

The filename C:\plot1.png contains a backslash ( \ ) which is an escape character .文件名C:\plot1.png包含一个反斜杠 ( \ ),它是一个转义字符。 This causes the error you are getting.这会导致您遇到的错误。

Change it to a slash ( / )将其更改为斜线 ( / )

png("C:/plot1.png", width = 480, height = 480, units = "px", bg = "white")

Or double the backslash ( \\ ):或者加倍反斜杠( \\ ):

png("C:\\plot1.png", width = 480, height = 480, units = "px", bg = "white")

Also met the similar issue in Windows 10, my R script is put in the same folder as RScript.exe , using the package ggplot2 .在 Windows 10 中也遇到了类似的问题,我的 R 脚本使用包ggplot2放在与RScript.exe相同的文件夹中。 However I got the message could not open file 'Rplot001.png' .但是我收到消息could not open file 'Rplot001.png'

Finally found two ways to solve the problem:终于找到了两种解决问题的方法:

  1. Move the R script to any other folder except the folder RScript.exe located.将 R 脚本移动到除RScript.exe所在文件夹之外的任何其他文件夹。
  2. Set work directory using the command setwd("YourPath") first, then do other things.首先使用命令setwd("YourPath")设置工作目录,然后执行其他操作。

I came across the same type of error, neither png nor jpg device could not open from ggplot2's ggsave command.我遇到了相同类型的错误,无论是 png 还是 jpg 设备都无法从 ggplot2 的 ggsave 命令打开。 The path contained an Å symbol which was stored as \305 when inserted in a variable describing the directory.该路径包含一个 Å 符号,该符号在插入描述目录的变量时存储为 \305。

setwd(path) followed by ggsave(filename) for this directory variable did work in this case, but ggsave(paste(path, filename, sep="/") did not.在这种情况下,此目录变量的setwd(path)后跟ggsave(filename)确实有效,但ggsave(paste(path, filename, sep="/")没有。

Replacing the Å with an A in my case could resolve the error.在我的情况下,用 A 替换 Å 可以解决该错误。

I had the same error message.我有同样的错误信息。 Turns out there was a typo in the path name.原来路径名有错别字。 Besides these problems, reinstalling ggplot and tydiverse from CRAN seemed to have worked for some, see here除了这些问题之外,从 CRAN 重新安装 ggplot 和 tydiverse 似乎对某些人有用,请参见此处

Long path names can also cause this error. 长路径名也可能导致此错误。 I was able to overcome this by defining the long path as a separate drive in Windows. 通过将长路径定义为Windows中的单独驱动器,我能够克服这一问题。 https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/subst https://docs.microsoft.com/zh-cn/windows-server/administration/windows-commands/subst

I received the error mentioned above我收到了上面提到的错误

Error in (function (filename = "Rplot%03d.png", width = 480, height = 480, : unable to start png() device Calls: <Anonymous> ...

My issue was with the fig.width option in one of my R-code chunks of my R-markdown document and when the output was rendered as an html document.我的问题是我的 R-markdown 文档的一个 R 代码块中的 fig.width 选项以及当输出呈现为 html 文档时。 The fig.width was too large compared to the other fig.width options in other code-chunks.与其他代码块中的其他 fig.width 选项相比,fig.width 太大。 Again this was observed only when I attempted to render it as an html document, not as a powerpoint presentation.再次,仅当我尝试将其呈现为 html 文档而不是作为 powerpoint 演示文稿时才观察到这一点。

One other problem could be that your Rstudio may have updated.另一个问题可能是您的 Rstudio 可能已更新。 I have encountered this problem while working inside R-markdown.我在 R-markdown 中工作时遇到了这个问题。 Trying the code in a regular R script still works.在常规 R 脚本中尝试代码仍然有效。 Try saving the markdown as a new file.尝试将降价保存为新文件。 This should fix the problem temporarily.这应该可以暂时解决问题。

Do not know of a long term solution.不知道长期解决方案。

I also had this error today when working in an RMarkdown notebook (it was fine yesterday).我今天在使用 RMarkdown 笔记本时也遇到了这个错误(昨天还不错)。 If I edit a chunk then try to run it I get this error.如果我编辑一个块然后尝试运行它,我会收到此错误。 If I then save the notebook and try the chunk again it works.如果我然后保存笔记本并再次尝试该块它可以工作。 My working directory is a OneDrive folder.我的工作目录是 OneDrive 文件夹。 I wonder whether that may be an issue.我想知道这是否可能是一个问题。

Having to save every after edit is not ideal, but a workaround.每次编辑后都必须保存并不理想,而是一种解决方法。

I added "dev.off() "before plot, the problem solved.我在绘图之前添加了“dev.off()”,问题就解决了。 The reason may be because of the previous device has not turn off yet.原因可能是之前的设备还没有关闭。

This has been solved, but I thought I might add my answer if it makes someone's life easier.这已经解决了,但我想如果它能让某人的生活更轻松,我可能会添加我的答案。 Of course you can have your wd set to some short path (or path with no special characters): setwd("c/Users/John/My_r_project) But I use R at work, thus my R project is saved on a common drive with super long path, and my working directory has to be long. A workaround was:当然,您可以将wd设置为一些短路径(或没有特殊字符的路径): setwd("c/Users/John/My_r_project)但我在工作中使用 R,因此我的 R 项目保存在一个公共驱动器上超长的路径,我的工作目录必须很长。解决方法是:

```{r setup, include=FALSE}
knitr::opts_chunk$set(
    fig.path = "c/Users/John/My_r_project/figures" #make sure you create the folder first in Windows
)
```

And you can of course add other options there as well, such as: dpi = 300 , echo = FALSE , ...当然,您也可以在那里添加其他选项,例如: dpi = 300echo = FALSE ,...

I had the same problem while running R in Jupyter notebook.我在 Jupyter notebook 中运行 R 时遇到了同样的问题。 I did a lot of Google searches and tried everything possible.我做了很多谷歌搜索并尝试了所有可能的方法。 The only way it worked for me was by restarting the kernel.它对我有用的唯一方法是重新启动内核。 But, restarting the kernel is not a good solution if you already have trained your models which took you a long time.但是,如果您已经训练了需要很长时间的模型,那么重新启动内核并不是一个好的解决方案。

This started happening to my while using RMarkdown.这开始发生在我使用 RMarkdown 的时候。 My solution is to restart R and clear the output, and re-run.我的解决方案是重新启动 R 并清除输出,然后重新运行。 I don´t know why it happens.我不知道为什么会这样。

If you are trying to save the PNG image, make sure you have permission to create files in the destination folder.如果您尝试保存 PNG 图像,请确保您有权在目标文件夹中创建文件。 I've seen this error come up on a corporate network where full access had not been granted to the user.我在未授予用户完全访问权限的公司网络上看到此错误。

The error usually means that the file cannot over accessed or overwritten, meaning that the file is in use or the file path is not writeable .该错误通常意味着文件不能被过度访问或覆盖,这意味着文件正在使用文件路径不可写

In the first case, just close the files you have open (eg, in an image preview) and try again.在第一种情况下,只需关闭您打开的文件(例如,在图像预览中)并重试。

interestingly, also observed the error when in the UI 'tableOutput' was used and on the server side 'renderPlot'有趣的是,还观察到在 UI 中使用“tableOutput”和在服务器端使用“renderPlot”时的错误

I have seen this message, and find this is because the png file is occupied by another software or process.我看过这个消息,发现这是因为png文件被另一个软件或进程占用了。

So close the software or process, then restart rstudio.所以关闭软件或进程,然后重启rstudio。

I once also run into this problem.我曾经也遇到过这个问题。 For me the first solution worked, but you might also want to check the other two options.对我来说,第一个解决方案有效,但您可能还想检查其他两个选项。

  1. Restart R session.重新启动 R 会话。 Tab Session > Restart R (Ctrl + Shift + F10)选项卡会话 > 重新启动 R(Ctrl + Shift + F10)
  2. Check working directory using getwd() and change it is necessary using setwd(path)使用 getwd() 检查工作目录并使用 setwd(path) 进行更改
  3. Don't forget to close the device using dev.off()不要忘记使用 dev.off() 关闭设备

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 绘制编织错误:“无法启动 png() 设备” - Plot knitting error : “unable to start png() device” ggsave() 失败并在 png_dev(..., res = dpi, units = "in") 中出现错误:无法启动 png() 设备 - ggsave() fails with Error in png_dev(..., res = dpi, units = "in") : unable to start png() device jupyter 中的 r 图形 - 无法启动 png() 设备 - r graphs in jupyter - unable to start png() device 无法导出绘图:R 代码执行错误/无法启动 png() 设备 - Unable to export plot: R code execution error/ unable to start png() device R无法启动设备PNG - 功能()对于PNG是否为TRUE? - R unable to start device PNG - capabilities() has TRUE for PNG? 这是什么错误; “(函数(文件名 =“Rplot%03d.png”,宽度 = 480,高度 = 480,:无法启动 png() 设备)中的错误? - What is this error; "Error in (function (filename = "Rplot%03d.png", width = 480, height = 480, : unable to start png() device"? “ plotGoogleMaps启用启动设备png”错误 - “plotGoogleMaps enable to start device png” error R Markdown使用动态字符串扩展:无法启动png()设备时出现错误,没有明确的原因有时 - R Markdown Expanding with Dynamic String: Error unable to start png() device Shows Up Occasionally Without Clear Reason R Shiny 运行几个小时后无法启动 png 设备 - R Shiny unable to start png device after running a few hours 无法启动 plot 名称的 png() 设备使用“粘贴”function 创建 - unable to start png() device for plot names create with "paste" function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM