2

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

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
anitasgiraldo
  • 21
  • 1
  • 3
  • 1
    Are you closing your r chunks with ```? In your example above you did not do this. And also the commas in the name of the second chunk are critical, because RMarkdown thinks that there are more arguments, separated by commas. – J_F Apr 05 '17 at 06:32
  • @J_F is right, quote your chunk name and you should be fine. E.g. `"plot means, mins, and maxs"`. – Roman Luštrik Apr 05 '17 at 07:15
  • Hello and thank you for your reply. To answer your questions I have closed the r chunks with ```. I also quote the chunk name as: "plot means, mins, and maxs" and also tried removing the comas. It didn't fix the problem. The same error message appears. – anitasgiraldo Apr 06 '17 at 07:02
  • maybe you should provide part of ur data or reproducible example – Peter Chen Apr 25 '17 at 09:15

2 Answers2

2

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.

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.

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.

Eden
  • 335
  • 2
  • 8
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.

  • This is more fit as a comment, please read [how to write a good answer](https://stackoverflow.com/help/how-to-answer). – GGG Jun 26 '17 at 14:31