r语言 - 使用命令行进行脚本调试



我想知道是否有人知道如何使用命令行在 linux 环境中调试 R 脚本。例如,在python中,我们可以使用pdb。我们首先设置一个断点(pdb.set_trace(((,然后可以使用"c","n","s","b"等命令来调试linux环境中的python代码。我一直在搜索很多关于R调试的信息,但到目前为止,我没有在R中找到类似的功能。 非常感谢您的帮助!

没有本机方法可以在命令行中调试 Rscript,但您可以使用我用readLineseval制定的一种笨拙的解决方法。

ipdb.r <- function(){
input <- ""
while(!input %in% c("c","cont","continue"))
{
cat("ipdb.r>")
# stdin connection to work outside interactive session
input <- readLines("stdin",n=1)
if(!input %in% c("c","cont","continue","exit"))
{
# parent.frame() runs command outside function environment
print(eval(parse(text=input),parent.frame()))
}else if(input=="exit")
{
stop("Exiting from ipdb.r...")
}
}
}

要使用 Rscript 调用的 R 文件中的示例用法:

ipdbrtest.R

a <- 3
print(a)
ipdb.r()
print(a)

命令行:

$ Rscript ipdbrtest.R
[1] 3
ipdb.r>a+3
[1] 6
ipdb.r>a+4
[1] 7
ipdb.r>a <- 4
[1] 4
ipdb.r>c
[1] 4

我鼓励任何使用它的人扩展它以获得更好的调试功能。

最新更新