如何调试Rscript调用

问题描述:

有没有办法在RStudio中调试Rscript调用?如何调试Rscript调用

说我从命令行调用RScript,如Rscript prog.R x y z,我想在特定行检查代码。我不能在Rstudio中交互式地运行它,因为我不知道如何传递参数。

+0

Rstudio确实提供调试工具。你可以将整个脚本作为R中的一个函数来写,并将x y z作为参数传递给该函数? –

+0

@ Dinesh.hmn我认为当R脚本通过'source'函数调用时,OP有兴趣调用[tag:rstudio]调试工具。当通过** Cmd + return **执行时,RStudio进入调试模式。当通过'source'函数调用脚本时,不会发生这种情况。 – Konrad

+0

您的脚本是否通过调用'commandArgs()'来获取参数?所以你想要一种在RStudio中用'source'调用脚本时能够“伪造”命令参数的方法? – Spacedman

这就是我所做的 - 这不是一个正式的调试,但它适用于我。

例如prog.R脚本的顶部:

# uncomment this section to run using Rscript from command line: 
userprefs <- commandArgs(trailingOnly = TRUE) 
x <- userprefs[1] 
y <- userprefs[2] 
z <- userprefs[3] 

# uncomment this section to run within RStudio 
cat("you forgot to comment out the troubleshooting part!") 
x <- 1 
y <- 2 
z <- 3 

当你解决你的脚本,注释掉一个或取决于不管你是内RStudio或RSCRIPT命令行采购的其他部分。