Ed_Gravy
Ed_Gravy

Reputation: 2033

Knitting R markdown (with plot() function) to PDF gives error

While knitting an RMD to PDF, I get the following error:

Quitting from lines 47-76 (ABC.Rmd) 
Error in (function (file = if (onefile) "Rplots.pdf" else "Rplot%03d.pdf",  : 
  cannot open file '~/Read and visualize the "governorate" shapefile-1.pdf'

If I try to see it works for an HTML, then I get the following error:

Quitting from lines 47-76 (ABC.Rmd) 
Error in png(..., res = dpi, units = "in") : unable to start png() device
Calls: <Anonymous> ... in_dir -> plot2dev -> do.call -> <Anonymous> -> png
In addition: Warning messages:
1: In png(..., res = dpi, units = "in") :
  unable to open file '~/Read and visualize the "governorate" shapefile-1.png' for writing
2: In png(..., res = dpi, units = "in") : opening device failed

Now I don't know if it has to do with the plot function, as I never get this error with "ggplot"

```{r Read and visualize the "governorate" shapefile, echo = FALSE}
# Import the Iraqi regions shapefile
Iraq_Regions =  readOGR(dsn = "~/governorates.shp")
# Import the Baghdad neighborhood shapefile 
Baghdad_Neigh = readOGR(dsn = "~/baghdad_neighborhoods.shp")
# Plot the Wiki Leaks data frame
plot(x=Wiki_Leaks$Longitude, y=Wiki_Leaks$Latitude, pch=20, col='red', cex=0.4)
plot(Iraq_Regions, border='blue', add=T)
plot(Baghdad_Neigh, border='yellow', add=T)

# Merge neighborhoods to make the Baghdad city outline
Greater_Baghdad <- gUnaryUnion(Baghdad_Neigh)

# Plot the shapefiles 
plot(x=Wiki_Leaks$Longitude, y=Wiki_Leaks$Latitude, pch=20, col='red', cex=0.4)
plot(Iraq_Regions, border='blue', add=T)
plot(Greater_Baghdad, border='yellow', add=T)

# view the names of different regions and the numbers of records in each region
#table(Wiki_Leaks$Region)

# Retrieve all events near Baghdad
Wiki_Leaks_MND_N_df = Wiki_Leaks[Wiki_Leaks$Region == "MND-N", ]

# Now make a basic plot
plot(x = Wiki_Leaks_MND_N_df$Long, y = Wiki_Leaks_MND_N_df$Lat,
     pch = 20, col = 'red', cex = 0.4,
     ylab = "Longitude", xlab = "Latitude", 
     main = "Death Events in Baghdad Neihborhood MND-N")

```

Upvotes: 1

Views: 639

Answers (1)

bttomio
bttomio

Reputation: 2306

You need to remove the punctuation marks while naming your code chunks, which will be used to name your files (more information here, for example).

In your case, removing "governorate" would solve the problem.

Upvotes: 1

Related Questions