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.
Just changed to another dataset - Changed working directory and changed all save directory locations to the correct.
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: R, command line, write to file fails).
Progresses to generate the next output and get the error
"Error in file(file, ifelse(append, "a", "w")) : cannot open the connection In addition: Warning message: In file(file, ifelse(append, "a", "w")) : 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"
Subsequently the associated ggplot doesn't appear to be generated and gives the error (Doesn't appear in the plot window? could this suggest :
"Saving 22.6 x 16.1 cm image Error in grDevices::png(..., res = dpi, units = "in") : unable to start png() device In addition: Warning messages: 1: In grDevices::png(..., res = dpi, units = "in") : 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 = "in") : opening device failed"
Section of code is this:
# load the data file, hit tab for selections
dar_baseline <- read.csv(file = "Dodoma_Baseline_Clean.csv",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 <- dar_baseline %>% group_by(Month) %>% 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, "F:/PhD/Working Projects/Chapter 1 Experiments/Current Data (MIDAS)/Dodoma/Stat Outputs/Dod.Uncor.Temp.8595.csv")
# Join Temperature stats to the raw data. Nicknamed JoinT (Joining of data frames)
dar_joinT <- merge(x = dar_baseline,y = temp.stats.wide,by = "Month")
#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 <- dar_joinT %>% filter(AIR_TEMPERATURE < HighSD4temp) %>% filter(AIR_TEMPERATURE > 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 = "gray80") +
geom_line(size=1.5,aes(col="Mean Temperature"))+
geom_point(data = dar_baseline,aes(Month,AIR_TEMPERATURE, col="Outliers"))+
geom_point(data = clean_data,aes(Month,AIR_TEMPERATURE, col="Monthly Spread")) +
scale_colour_manual(name = "Key", values=c("#009E73","#D55E00","black")) +
ggtitle("Dodoma Temperature Climatology (1985-1995)
With Outliers") +
xlab("Month") +
ylab ("Temperature °C") +
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("Dod.Outliers.Plot.8595.png", plot=last_plot(), path = "F:/PhD/Working Projects/Chapter 1 Experiments/Current Data (MIDAS)/Dodoma/Graph Outputs/", scale = 1, width = NA, height = NA, units = c("cm"), 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 <- clean_data %>% group_by(Month) %>% 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, "F:/PhD/Working Projects/Chapter 1 Experiments/Current Data (MIDAS)/Dodoma/ Stat Outputs/Dod.Cor.Temp.8595.csv")
Saving - Clean stats wide is where the error appears. clean.stats.wide looks like this:
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.
Help would be greatly appreciated.
**SOLVED - Spacing issues in directory paths (See comments).