anitasgiraldo
anitasgiraldo

Reputation: 21

error in rmarkdown when plotting

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

Upvotes: 2

Views: 3527

Answers (2)

Eden
Eden

Reputation: 458

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.

Upvotes: 2

convertibile IkE
convertibile IkE

Reputation: 143

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.

Upvotes: 1

Related Questions