在目录中创建文件夹,然后将文件保存在新文件夹中
我有相当复杂的代码来创建热图,但一个简单的目标。使用循环,而不是将所有热图文件打印到一个文件夹中新创建的文件夹(打印映射“ a” to png and Map“ in in in in in in in infold” a“ a”等...)?
以下代码通过对象“ splist”中的所有唯一名称,为每个名称创建热图,然后将所有热图打印到同一文件夹。我将如何纠正它,以便将每个热图文件放入自己的文件夹中?如果我的代码不起作用,任何热图样式都可以使用任何示例数据,我只希望使用我制作的循环为每个文件创建单独的文件夹。
GetMatrix=function(data,spToSum) {
newdat<-filter(data,common_name %in% spToSum)
matrixdat<-dcast(newdat, site ~ year_season, value.var="num", fun.aggregate = sum)
matrixdat$site <- as.character(matrixdat$site)
matrixdat <- matrixdat[order(nchar(matrixdat$site), matrixdat$site)]
matrixdat <- matrixdat %>% remove_rownames %>% column_to_rownames(var="site") # col to row names
as.matrix(matrixdat)
}
PlotHeatMap <- Heatmap(as.matrix(round(log(data+1), digits = 1)),
cluster_rows = FALSE,
cluster_columns = FALSE,
na_col = "black",
column_title = "Year/Season",
row_title = "Site",
heatmap_legend_param = list(title = "ln(x+1)"))
dataList<-list()
filenameVal<-paste0(1:length(splist),splist," - NotClustered.png")
doClusterVal<-FALSE
setwd('C:/...')
# Prints the maps
for(run in 1:length(splist)) {
dataList[[run]]<-GetMatrix(filter(dat,CYR>=2005),splist[run])
PlotHeatMap(data=dataList[[run]],splist[run],fileName=filenameVal[run],doCluster=doClusterVal)
dir.create(file.path(splist[run]), recursive = TRUE)
print(paste(run,splist[run]))
}
# common_name = fish/shrimp/crab name
# num = count
# site = location #
# CYR = calendar year
# Season = wet/dry season
随机选择10行数据:
> dput(dat)
structure(list(common_name = c("Mojarra spp", "Chub spp", "Glass Shrimp",
"Lined Sole", "Pink Shrimp", "Iridescent Shrimp", "Giant Decorator crab",
"Longnose Spider Crab", "Pearl Blenny", "Banded Killifish"),
num = c(4L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), site = c(4L,
1L, 47L, 5L, 10L, 18L, 22L, 3L, 42L, 19L), CYR = c(2017L,
2012L, 2013L, 2019L, 2008L, 2019L, 2009L, 2009L, 2014L, 2015L
), Season = c("WET", "WET", "WET", "DRY", "WET", "DRY", "DRY",
"DRY", "DRY", "DRY"), year_season = c("2017_WET", "2012_WET",
"2013_WET", "2019_DRY", "2008_WET", "2019_DRY", "2009_DRY",
"2009_DRY", "2014_DRY", "2015_DRY")), row.names = c(175919L,
114959L, 124359L, 204205L, 53943L, 207868L, 61123L, 64762L, 130625L,
148966L), class = "data.frame")
I have fairly complicated code for creating a heatmap, but a simple objective. Using a for-loop, instead of R printing all heatmap files to a single folder, how would I tell it to create a new folder each time I tell it to also print a heatmap, and put the heatmap that matches the folder name inside that newly created folder (Print map "A" to PNG and map "A" goes in folder "A", etc...)?
The following code goes through all unique names in the object "splist", creates a heatmap for each name, then prints all heatmaps to the same folder. How would I correct it so that each heatmap file is put in it's own folder? If my code doesn't work, any heatmap style will do with any example data, I just hope to create separate folders for each file using the for-loop I've made.
GetMatrix=function(data,spToSum) {
newdat<-filter(data,common_name %in% spToSum)
matrixdat<-dcast(newdat, site ~ year_season, value.var="num", fun.aggregate = sum)
matrixdat$site <- as.character(matrixdat$site)
matrixdat <- matrixdat[order(nchar(matrixdat$site), matrixdat$site)]
matrixdat <- matrixdat %>% remove_rownames %>% column_to_rownames(var="site") # col to row names
as.matrix(matrixdat)
}
PlotHeatMap <- Heatmap(as.matrix(round(log(data+1), digits = 1)),
cluster_rows = FALSE,
cluster_columns = FALSE,
na_col = "black",
column_title = "Year/Season",
row_title = "Site",
heatmap_legend_param = list(title = "ln(x+1)"))
dataList<-list()
filenameVal<-paste0(1:length(splist),splist," - NotClustered.png")
doClusterVal<-FALSE
setwd('C:/...')
# Prints the maps
for(run in 1:length(splist)) {
dataList[[run]]<-GetMatrix(filter(dat,CYR>=2005),splist[run])
PlotHeatMap(data=dataList[[run]],splist[run],fileName=filenameVal[run],doCluster=doClusterVal)
dir.create(file.path(splist[run]), recursive = TRUE)
print(paste(run,splist[run]))
}
# common_name = fish/shrimp/crab name
# num = count
# site = location #
# CYR = calendar year
# Season = wet/dry season
Random selection of 10 rows of data:
> dput(dat)
structure(list(common_name = c("Mojarra spp", "Chub spp", "Glass Shrimp",
"Lined Sole", "Pink Shrimp", "Iridescent Shrimp", "Giant Decorator crab",
"Longnose Spider Crab", "Pearl Blenny", "Banded Killifish"),
num = c(4L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), site = c(4L,
1L, 47L, 5L, 10L, 18L, 22L, 3L, 42L, 19L), CYR = c(2017L,
2012L, 2013L, 2019L, 2008L, 2019L, 2009L, 2009L, 2014L, 2015L
), Season = c("WET", "WET", "WET", "DRY", "WET", "DRY", "DRY",
"DRY", "DRY", "DRY"), year_season = c("2017_WET", "2012_WET",
"2013_WET", "2019_DRY", "2008_WET", "2019_DRY", "2009_DRY",
"2009_DRY", "2014_DRY", "2015_DRY")), row.names = c(175919L,
114959L, 124359L, 204205L, 53943L, 207868L, 61123L, 64762L, 130625L,
148966L), class = "data.frame")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论