error in rmarkdown when plotting

2019-07-29 11:20发布

问题:

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.

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 

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:

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

looking at the traceback it shows the following:

  1. stop("invalid 'filename'")
  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) 

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

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

Please help me

回答1:

I had the same issue and I just realized this was due to the filename of my Rmd file--I used a % in the name. The issue disappeared after removing the symbol. What's the filename of your Rmd file? Maybe you should try to rename your file.