Reputation: 53
I have data for 40 buildings. Each building contains folders like CO2, Temperature, heat. then inside each folder there is data for different regions of house. For instance, for CO2, I have data (time series collection) for hallway, bedroom, living room etc. I need heatmaps for all these data file. So far, what i could do is, I wrote a code to go inside each directory, then each folder and then access files and draw heatmap inside Rstudi. Code is:
setwd("C:/Users/...")
folders <- list.dirs(full.names = TRUE)
result <- sapply(folders[-1], function(x){
res<-lapply(files, function (x) {
[some heatmap function]
P1<- ggplot(df, aes(Time, Date, fill =reading)) + geom_tile(colour = "grey") + scale_fill_gradientn(colours=c("darkblue", "red", "yellow"), values=rescale(c(0, 1000, 2000)), guide="colorbar")+scale_x_discrete(breaks = lab1)
plot(P1)
})})
what i actually want is, to store the heatmap results of each file in the same folder with the name of the folder+name of the file (like CO2.hallway.jpeg etc). I am trying various codes since last 2 days but none is working for me. can anyone please help me with this. I'll very grateful to you.
Upvotes: 1
Views: 4318
Reputation: 3828
Loop through the names and use the dir.create()
function to create the folders you need and then access them. Super simple: http://rfunction.com/archives/2432
Upvotes: 0
Reputation: 10162
What you want to do is change the working directory or the path to the file inside the lapply
-loop.
Something like this
setwd("...")
folders <- list.dirs(full.names = T)
res <- sapply(folders[-1], function(dir){
# INSERT OTHER CODE PARTS HERE
P1 <- ggplot(...) + geom_x()
# Option 1
setwd(dir)
ggsave(P1, filename = paste0("CO2.", dir, ".jpg"))
# Or Option2
ggsave(P1, filename = paste(dir, paste0("CO2.", dir, ".jpg"), sep = "/"))
# I encourage you to use pdf (best quality, can be included in LaTeX and Markdown), otherwise png (better quality)
})
A MWE to save plots in all sub-directions looks like this:
folders <- list.dirs(full.names = T)
lapply(folders[-1], function(dir){
dat <- data.frame(x = 1:10, y = cumsum(rnorm(10)))
P1 <- ggplot(dat, aes(x = x, y = y)) + geom_line()
ggsave(P1, filename = paste(dir, paste0("plot.png"), sep = "/"))
})
lapply(folders[-1], function(dir2){
# read the data
files <- list.files(dir2, pattern = "*.csv", recursive = F)
# finds the last "./" and takes everything afterwards
# aka, it returns the up-most folder
folder <- substr(dir2,
start = regexpr("\\./[^\\./]*$", dir2) + 2,
stop = nchar(dir2))
lapply(files, function(file, folder){
# find the filename, aka. exclude .csv
f.name <- substr(file, start = 1,
stop = regexpr(".csv", file) - 1)
# load each file to the loadeddat-data.frame
loadeddat <- read.table(paste(folder, file, sep = "/"))
# plot the data as you wish
P1 <- ggplot(loadeddat, aes(x = x, y = y)) + geom_line()
# create the name for the plot
nam <- paste(folder, # i.e., folder1
paste0(folder, "-", f.name, ".png"), # i.e., folder1-file1.png
sep = "/") # whole name/path looks like this:
# "folder1/folder1-file1.png"
# save it
ggsave(P1, filename = nam)
}, folder = folder)
})
Does that help?
Upvotes: 2