R - Error writing csv to file - "Error in file(file, ifelse(append, "a","w"... Windows 7. Works until a certain point
<p>I've been using R (Rstudio and R version 3.2.4) to automate my analysis. Worked all day on one location dataset. Read and written with no issues to the same drive as this one (just a different folder titled by the location name). It reads and writes multiple times throughout the code with progressive steps being dependent on the output of the other.</p>
<p>Just changed to another dataset - Changed working directory and changed all save directory locations to the correct.</p>
<p>Reads and writes the first part of the code fine (I've checked user permissions as I've read that this can be a problem with Windows 7 users here: <a href="https://stackoverflow.com/questions/19120450/r-command-line-write-to-file-fails">R, command line, write to file fails</a>).</p>
<p>Progresses to generate the next output and get the error</p>
<blockquote>
<p>&quot;Error in file(file, ifelse(append, &quot;a&quot;, &quot;w&quot;)) :
cannot open the connection
In addition: Warning message:
In file(file, ifelse(append, &quot;a&quot;, &quot;w&quot;)) :
cannot open file 'F:/PhD/Working Projects/Chapter 1 Experiments/Current Data (MIDAS)/Dodoma/ Stat Outputs/Dod.Cor.Temp.8595.csv': No such file or directory&quot;</p>
</blockquote>
<p>Subsequently the associated ggplot doesn't appear to be generated and gives the error (Doesn't appear in the plot window? could this suggest :</p>
<blockquote>
<p>&quot;Saving 22.6 x 16.1 cm image
Error in grDevices::png(..., res = dpi, units = &quot;in&quot;) :
unable to start png() device
In addition: Warning messages:
1: In grDevices::png(..., res = dpi, units = &quot;in&quot;) :
unable to open file 'F:/PhD/Working Projects/Chapter 1 Experiments/Current Data (MIDAS)/Dodoma/ Graph Outputs//Dod.Climatology.Corrected.8595.png' for writing
2: In grDevices::png(..., res = dpi, units = &quot;in&quot;) : opening device failed&quot;</p>
</blockquote>
<p>Section of code is this:</p>
<pre><code># load the data file, hit tab for selections
    dar_baseline &lt;- read.csv(file = &quot;Dodoma_Baseline_Clean.csv&quot;,header = TRUE)


#Calculating stats for Temperature (Gather code commented out)
#GGPlot2 - Individual columns code. Check stage 1 to make sure data is OK. NOT FINAL OUTPUT
temp.stats.wide &lt;- dar_baseline %&gt;% group_by(Month) %&gt;%    summarise(meantemp=mean(AIR_TEMPERATURE,na.rm = TRUE),
                                                              sdtemp = sd(AIR_TEMPERATURE,na.rm = TRUE),
                                                              mintemp = min(AIR_TEMPERATURE,na.rm = TRUE),
                                                              maxtemp = max(AIR_TEMPERATURE,na.rm = TRUE),
                                                              sd4temp = sdtemp*4,
                                                              HighSD4temp = meantemp + sd4temp,
                                                              LowSD4temp =  meantemp - sd4temp)

#Save file Temp.stats.wide as ORIGINAL uncorrected dataset (Before SD4 correction).
write.csv(temp.stats.wide, &quot;F:/PhD/Working Projects/Chapter 1    Experiments/Current Data (MIDAS)/Dodoma/Stat Outputs/Dod.Uncor.Temp.8595.csv&quot;) 

# Join Temperature stats to the raw data. Nicknamed JoinT (Joining of data frames) 
dar_joinT &lt;- merge(x = dar_baseline,y = temp.stats.wide,by = &quot;Month&quot;)

#After joining data frames - dplyr alternative to for loop  
# filter selects row that meet the criteria, hence two filter commands for  high and low SD range
clean_data &lt;- dar_joinT %&gt;% filter(AIR_TEMPERATURE &lt; HighSD4temp) %&gt;%    filter(AIR_TEMPERATURE &gt; LowSD4temp)

#check data with plot - Temperature. Outliers have been left in for demonstration. 
ggplot(data = clean_data,aes(Month, meantemp))+
  geom_ribbon(aes(ymin = LowSD4temp, ymax = HighSD4temp), fill = &quot;gray80&quot;) +
  geom_line(size=1.5,aes(col=&quot;Mean Temperature&quot;))+
  geom_point(data = dar_baseline,aes(Month,AIR_TEMPERATURE, col=&quot;Outliers&quot;))+
  geom_point(data = clean_data,aes(Month,AIR_TEMPERATURE, col=&quot;Monthly Spread&quot;)) +
  scale_colour_manual(name = &quot;Key&quot;, values=c(&quot;#009E73&quot;,&quot;#D55E00&quot;,&quot;black&quot;)) +
  ggtitle(&quot;Dodoma Temperature Climatology (1985-1995)
      With Outliers&quot;) +
  xlab(&quot;Month&quot;) +
  ylab (&quot;Temperature °C&quot;) +
  scale_x_continuous(breaks =1:12) +
  scale_y_continuous(breaks = c(0,5,10,15,20,25,30,35,40,45))+
  theme_minimal() 

#Graph saved to demonstrate outliers prior to removal.
ggsave(&quot;Dod.Outliers.Plot.8595.png&quot;, plot=last_plot(), path =   &quot;F:/PhD/Working Projects/Chapter 1 Experiments/Current Data (MIDAS)/Dodoma/Graph Outputs/&quot;, scale = 1, width = NA, height = NA, units = c(&quot;cm&quot;), dpi = 300, limitsize = TRUE)

#Clean stats wide (To correct altered mean values given the removal of data   outside of 4 standard deviations)
clean.stats.wide &lt;- clean_data %&gt;% group_by(Month) %&gt;%    summarise(meantemp=mean(AIR_TEMPERATURE,na.rm = TRUE),
                                                              sdtemp = sd(AIR_TEMPERATURE,na.rm = TRUE),
                                                              mintemp = min(AIR_TEMPERATURE,na.rm = TRUE),
                                                              maxtemp = max(AIR_TEMPERATURE,na.rm = TRUE)
                                                             )

#Save clean stats wide as the mean corrected to reflect the data without SD4 errors included. 
write.csv(clean.stats.wide, &quot;F:/PhD/Working Projects/Chapter 1    Experiments/Current Data (MIDAS)/Dodoma/ Stat Outputs/Dod.Cor.Temp.8595.csv&quot;) 
</code></pre>
<p>Saving - Clean stats wide is where the error appears.
clean.stats.wide looks like this:</p>
<p><a href="https://i.stack.imgur.com/gwkpb.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gwkpb.jpg" alt="enter image description here" /></a></p>
<p>As I said, it's been working fine and works on the first write.csv (temp.stats.wide) and first plot but I can't see the difference between this and the second and it works fine on another data set of the exact same setup.</p>
<p>Help would be greatly appreciated.</p>
<p>**SOLVED - Spacing issues in directory paths (See comments).</p>