使用R保存具有不同文件名的打印

使用R保存具有不同文件名的打印,r,plot,filenames,R,Plot,Filenames,在微调绘图参数时,我希望将所有测试运行保存在不同的文件中,这样它们就不会丢失。到目前为止,我使用以下代码成功地做到了这一点: # Save the plot as WMF file - using random numbers to avoid overwriting number <- sample(1:20,1) filename <- paste("dummy", number, sep="-") fullname <- paste(filename, ".w

在微调绘图参数时,我希望将所有测试运行保存在不同的文件中,这样它们就不会丢失。到目前为止,我使用以下代码成功地做到了这一点:

# Save the plot as WMF file - using random numbers to avoid overwriting
  number <- sample(1:20,1)
  filename <- paste("dummy", number, sep="-")
  fullname <- paste(filename, ".wmf", sep="")
  # Next line actually creates the file
  dev.copy(win.metafile, fullname)
  dev.off() # Turn off the device
#将绘图另存为WMF文件-使用随机数字以避免覆盖

数字如果要打印多个绘图,可以执行以下操作

png("plot-%02d.png")
plot(1)
plot(1)
plot(1)
dev.off()
这将创建三个文件“plot-01.png”、“plot-02.png”、“plot-03.png”

指定的文件名可以采用类似于sprintf的
格式,其中输入了绘图索引。请注意,当您打开新的图形设备时,计数会重置,因此在调用
dev.off()
之前,需要完成对plot()的所有调用


注意:但是使用此方法,它不会查看哪些文件已经存在。它将始终将计数重置为1。此外,无法将第一个索引更改为除1以外的任何内容。

如果要打印多个绘图,可以执行以下操作:

png("plot-%02d.png")
plot(1)
plot(1)
plot(1)
dev.off()
这将创建三个文件“plot-01.png”、“plot-02.png”、“plot-03.png”

指定的文件名可以采用类似于sprintf的
格式,其中输入了绘图索引。请注意,当您打开新的图形设备时,计数会重置,因此在调用
dev.off()
之前,需要完成对plot()的所有调用


注意:但是使用此方法,它不会查看哪些文件已经存在。它将始终将计数重置为1。此外,无法将第一个索引更改为除1以外的任何内容。

如果要打印多个绘图,可以执行以下操作:

png("plot-%02d.png")
plot(1)
plot(1)
plot(1)
dev.off()
这将创建三个文件“plot-01.png”、“plot-02.png”、“plot-03.png”

指定的文件名可以采用类似于sprintf的
格式,其中输入了绘图索引。请注意,当您打开新的图形设备时,计数会重置,因此在调用
dev.off()
之前,需要完成对plot()的所有调用


注意:但是使用此方法,它不会查看哪些文件已经存在。它将始终将计数重置为1。此外,无法将第一个索引更改为除1以外的任何内容。

如果要打印多个绘图,可以执行以下操作:

png("plot-%02d.png")
plot(1)
plot(1)
plot(1)
dev.off()
这将创建三个文件“plot-01.png”、“plot-02.png”、“plot-03.png”

指定的文件名可以采用类似于sprintf的
格式,其中输入了绘图索引。请注意,当您打开新的图形设备时,计数会重置,因此在调用
dev.off()
之前,需要完成对plot()的所有调用


注意:但是使用此方法,它不会查看哪些文件已经存在。它将始终将计数重置为1。此外,无法将第一个索引更改为除1以外的任何内容。

如果您真的想增加(以避免覆盖已存在的文件),您可以创建一个类似以下的小函数:

createNewFileName = function(path  = getwd(), pattern = "plot_of_something", extension=".png") {
  myExistingFiles = list.files(path = path, pattern = pattern)
  print(myExistingFiles)
  completePattern = paste0("^(",pattern,")([0-9]*)(",extension,")$")
  existingNumbers  = gsub(pattern = completePattern, replacement = "\\2", x = myExistingFiles)

  if (identical(existingNumbers, character(0)))
    existingNumbers = 0

  return(paste0(pattern,max(as.numeric(existingNumbers))+1,extension))
}

# will create the file myplot1.png
png(filename = createNewFileName(pattern="myplot"))
hist(rnorm(100))
dev.off()

# will create the file myplot2.png
png(filename = createNewFileName(pattern="myplot"))
hist(rnorm(100))
dev.off()

如果您确实想增加(以避免覆盖已存在的文件),您可以创建一个小函数,如下所示:

createNewFileName = function(path  = getwd(), pattern = "plot_of_something", extension=".png") {
  myExistingFiles = list.files(path = path, pattern = pattern)
  print(myExistingFiles)
  completePattern = paste0("^(",pattern,")([0-9]*)(",extension,")$")
  existingNumbers  = gsub(pattern = completePattern, replacement = "\\2", x = myExistingFiles)

  if (identical(existingNumbers, character(0)))
    existingNumbers = 0

  return(paste0(pattern,max(as.numeric(existingNumbers))+1,extension))
}

# will create the file myplot1.png
png(filename = createNewFileName(pattern="myplot"))
hist(rnorm(100))
dev.off()

# will create the file myplot2.png
png(filename = createNewFileName(pattern="myplot"))
hist(rnorm(100))
dev.off()

如果您确实想增加(以避免覆盖已存在的文件),您可以创建一个小函数,如下所示:

createNewFileName = function(path  = getwd(), pattern = "plot_of_something", extension=".png") {
  myExistingFiles = list.files(path = path, pattern = pattern)
  print(myExistingFiles)
  completePattern = paste0("^(",pattern,")([0-9]*)(",extension,")$")
  existingNumbers  = gsub(pattern = completePattern, replacement = "\\2", x = myExistingFiles)

  if (identical(existingNumbers, character(0)))
    existingNumbers = 0

  return(paste0(pattern,max(as.numeric(existingNumbers))+1,extension))
}

# will create the file myplot1.png
png(filename = createNewFileName(pattern="myplot"))
hist(rnorm(100))
dev.off()

# will create the file myplot2.png
png(filename = createNewFileName(pattern="myplot"))
hist(rnorm(100))
dev.off()

如果您确实想增加(以避免覆盖已存在的文件),您可以创建一个小函数,如下所示:

createNewFileName = function(path  = getwd(), pattern = "plot_of_something", extension=".png") {
  myExistingFiles = list.files(path = path, pattern = pattern)
  print(myExistingFiles)
  completePattern = paste0("^(",pattern,")([0-9]*)(",extension,")$")
  existingNumbers  = gsub(pattern = completePattern, replacement = "\\2", x = myExistingFiles)

  if (identical(existingNumbers, character(0)))
    existingNumbers = 0

  return(paste0(pattern,max(as.numeric(existingNumbers))+1,extension))
}

# will create the file myplot1.png
png(filename = createNewFileName(pattern="myplot"))
hist(rnorm(100))
dev.off()

# will create the file myplot2.png
png(filename = createNewFileName(pattern="myplot"))
hist(rnorm(100))
dev.off()

谢谢,这是一个非常完整的功能为我的情况。毕竟我可以看出我设计的方法还不错。谢谢,这是一个非常完整的函数,适合我的情况。毕竟我可以看出我设计的方法还不错。谢谢,这是一个非常完整的函数,适合我的情况。毕竟我可以看出我设计的方法还不错。谢谢,这是一个非常完整的函数,适合我的情况。毕竟我能看出我设计的方法还不错。