绘图时 rmarkdown 中的错误 [英] error in rmarkdown when plotting

查看:250
本文介绍了绘图时 rmarkdown 中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这个论坛的新手,一般来说也是 R 的新手.但最近我被介绍到 Rstudio 中的 rmarkdowns,我已经准备好了一个脚本,它使用一些 csv 文件来运行一些计算,然后创建一些图.

I am new to this forum and new to R in general. But recently I was introduced to rmarkdowns in Rstudio and I have been getting a script ready that uses some csv files to run some calculations and then creates some plots.

脚本如下(附数据):SE_MACover_Abr2014_40m_CP.csv

Scrip as follows (data attached):SE_MACover_Abr2014_40m_CP.csv

```{r prepare the data} 

df <- read.csv(file.choose()) #SE_MACover_Abr2014_40m_CP.csv 

#   call the libraries 

library(ggplot2) 
library(plyr) 
library(reshape2) 

str(df) 
df 

## create factor levels 
df$Stat <-factor(df$Stat, levels = c("SE_Mean", "SE_Min","SE_Max")) 

df$Imgs <- factor(df$Imgs, levels = c("2", "5","10", "20","30", "40", "50",     "60", "70")) 
df$Stat 
df$Imgs 

```{r plot means, mins, and maxs} 
Plot1 <- ggplot(data = df, aes(x = Imgs, y = X, group = Stat)) + 
geom_line(aes(linetype = Stat, colour = Stat), size = 1) + 
geom_point(aes(colour=Stat)) + 
ylab(bquote('Standard Error ')) + 
xlab(bquote('Number of Images')) 
Plot1 

我在 R base 中尝试过这个并且工作正常,但是在 Rstudio 中的 rmarkdown 绘图没有绘制并且它给了我以下错误消息:

I tried this in R base and worked fine, but rmarkdown in Rstudio the plots do not plot and it gives me the following error message:

错误(函数(文件名=Rplot%03d.png",宽度= 480,高度= 480,:无效的'文件名'

Error in (function (filename = "Rplot%03d.png", width = 480, height = 480, : invalid 'filename'

查看回溯它显示以下内容:

looking at the traceback it shows the following:

  1. stop("无效的'文件名'")
  2. (function (filename = "Rplot%03d.png", width = 480, height = 480, units = "px", pointsize = 12, bg = "white", res = NA, family = "sans",restoreConsole = TRUE, type = c("windows", "cairo", "cairo-png"), antialias = c("default", "none", "cleartype", "gray", "subpixel")) ...
  3. do.call(what = png, args = args)
  4. .rs.createNotebookGraphicsDevice(filename, height, width, units, pixelRatio, extraArgs)
  5. (function () { .rs.createNotebookGraphicsDevice(filename, height, width, units, pixelRatio, extraArgs) ...
  6. grid.newpage()
  7. print.ggplot(x)
  8. function (x, ...) UseMethod("print")(x)

我什至尝试用以下代码绘制最简单的图形:

I even tryied plotting the simplest graph with this code:

 x <- c(1,2,3,4,5,6) 
 y <- c(1,2,3,4,5,6) 
 plot(x,y) 

当我试图解决这个问题时,我认为我的脚本有问题,有人建议将用于绘图的脚本直接粘贴到控制台中.我这样做了,它奏效了!它在 rmarkdown 中产生相同的错误,但它在控制台中运行良好..

While I was trying to work this out as I thought there was a problem with my script, someone suggested to paste the piece of script for plotting straight into the console. I did so and it worked! And it produces the same error in rmarkdown, but it runs fine in the console..

我不知道如何解决这个问题,所以我可以运行我的 Markdown 文件,它会创建我需要的图表,

I don't know how to fix this so I can run my markdown file and it will create the graphs I need,

请帮帮我

推荐答案

当临时路径加上 RStudio 在生成 rmarkdown 文档时创建的文件名太长时,经常会出现此问题.在 Windows 系统上,这通常是 260 个字符长,但确切的长度取决于您的磁盘是否使用 FAT、NTFS 等格式.请注意,问题是由 RStudio 创建的临时文件——您通常无法控制这些.

This issue often arises when temporary paths plus filenames created by RStudio when generating the rmarkdown document are too long. On Windows systems, this is generally 260 characters long, but the exact length depends on whether your disk is formatted using FAT, NTFS, etc. Note that the problem is temporary files created by RStudio--you generally can't control these.

但是,您可以控制 rmarkdown 文档的路径长度.如果足够短,就会为 RStudio 创建临时文件名留出空间".

However, you can control the length of the path of your rmarkdown documengt. If it is short enough, it will leave "space" for RStudio to create the temporary file name.

或者,重新启动 RStudio 通常可以工作,尽管在处理 rmarkdown 文档时,如果再次遇到问题,则必须再次重新启动.

Alternatively, restarting RStudio often works, although when working on the rmarkdown document, if you run into the problem again you'll have to restart again.

这篇关于绘图时 rmarkdown 中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆