我正在尝试使用可选参数在R中创建用户定义的函数,以将绘图另存为pdf.我有必要的参数默认为FALSE.如果为TRUE,则使用filename.pdf保存为pdf.我有一些语法错误:
seeplot <-function (save=FALSE) {
x <- seq(1,10,1)
y <- x^2
plot (x,y,type="l")
if (save==TRUE) pdf(file="save")
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
我认为这是因为没有?pdf仔细阅读,你遇到了麻烦.我会让你挣扎一下(因为斗争很好,我常常和R战斗)但我想也许逻辑上的保存方法不是最好的,所以我会插话.我看到的是3个错误:
dev.off这是你的功能修复:
seeplot <-function (save=FALSE) {
x <- seq(1,10,1)
y <- x^2
plot (x,y,type="l")
if (save) {
pdf(file="save.pdf")
plot (x,y,type="l")
dev.off()
}
}
Run Code Online (Sandbox Code Playgroud)
但我可以建议提供文件名而不是逻辑保存.这允许用户根据需要命名文件:
seeplot <-function (file=NULL) {
x <- seq(1,10,1)
y <- x^2
plot (x,y,type="l")
if (!is.null(file)) {
pdf(file=file)
plot (x,y,type="l")
dev.off()
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
321 次 |
| 最近记录: |