r - 使用 png() 搜索将图片保存在预定义位置的方法,类似于使用 ggsave() 的成功方法
问题描述
我正在使用两种不同的图形:1.) 使用 ggplot2 创建的箱线图和 2.) 相关表。
我想将这两个图形保存在用户可以通过提示选择的位置下,使用:
library(easycsv)
choose_dir = function(){
os = Identify.OS()
if(tolower(os) == "windows"){
directory <- utils::choose.dir()
}
if(tolower(os) == "macosx"){
system("osascript -e 'tell app \"RStudio\" to POSIX path of (choose folder with prompt \"Choose Folder:\")' > /tmp/R_folder",
intern = FALSE, ignore.stderr = TRUE)
directory <- system("cat /tmp/R_folder && rm -f /tmp/R_folder", intern = TRUE)
}
return(directory)
}
现在,我正在使用此代码来选择要保存图形的位置:
folder = choose_dir()
为了保存我的图形,我对使用 ggsave 的箱线图没有任何问题:
ggsave("SL_Boxplot.png", path = folder, width=7, height= 0.7, dpi=500, units = "cm", scale = 5.2)
但是,我无法以与 ggsave 相同的方式保存相关表图片,即使我尝试了许多不同的方法:
png("folder/Correlation_Table.png", width = 30, height = 25, pointsize = 8, res = 700, units = "cm")
没有任何效果。非常感谢任何帮助的人!
解决方案
您需要为png
.
png(paste0(folder,"/Correlation_Table.png"), width = 30, height = 25, pointsize = 8, res = 700, units = "cm")
您也可以更改/
为\\
.
编辑:为了更正确和安全,这是@r2evans 提供的正确代码。
png(file.path(folder, "Correlation_Table.png"), width = 30, height = 25, pointsize = 8, res = 700, units = "cm")
推荐阅读
- xcode - 函数“UnityGetGLView”的隐式声明在 C99 中无效
- ios - 移动到另一个屏幕时保持 CABasicAnimation 运行
- ffmpeg - 使用偏移量调用时 FFMPEG 失败
- php - PHP密码验证问题
- linux-kernel - 如何防止 Yocto 项目 BadValue 错误?
- rest - 无法创建新的融合页面作为另一个页面的子页面
- javascript - 如何停止可重复的javascript函数
- gradle - 在命令提示符下运行 flutter run 命令时出错:[致命错误] aapt2-3.5.0-5435860.pom:2:1: Content is not allowed in prolog
- amazon-web-services - Spark AWS Glue UI - 错误:无法找到或加载主类 $SPARK_HISTORY_OPTS - Windows Docker
- tensorflow - 如何将 .png 文件转换为 TFrecord tensorflow 格式?