在RStudio中使用调试器......并在正确的位置停止

时间:2016-02-02 19:56:46

标签: r debugging rstudio

我想浏览图片中包含的代码betas <- sapply(1 : nosim, function(i) makelms(x1, x2, x3))函数的rgp1。我放置了两个断点,因为只有顶部的那个没有做到这一点;但是,即使我在控制台顶部菜单上按betas时停在Next,它也会向rgp1功能移动到rgp2功能,跳过第I行想要调试。这是序列:

enter image description here

enter image description here

如何让调试器通过sapply代码行而不跳过它?我试着点击&#34;进入当前的函数调用&#34; ({}图标)没有成功。

属于{swirl}包的代码是:

makelms <- function(x1, x2, x3){
  # Simulate a dependent variable, y, as x1
  # plus a normally distributed error of mean 0 and 
  # standard deviation .3.
  y <- x1 + rnorm(length(x1), sd = .3)
  # Find the coefficient of x1 in 3 nested linear
  # models, the first including only the predictor x1,
  # the second x1 and x2, the third x1, x2, and x3.
  c(coef(lm(y ~ x1))[2], 
    coef(lm(y ~ x1 + x2))[2], 
    coef(lm(y ~ x1 + x2 + x3))[2])
}

# Regressor generation process 1.
rgp1 <- function(){
  print("Processing. Please wait.")
  # number of samples per simulation
  n <- 100
  # number of simulations
  nosim <- 1000
  # set seed for reproducibility
  set.seed(4321)
  # Point A
  x1 <- rnorm(n)
  x2 <- rnorm(n)
  x3 <- rnorm(n)
  # Point B
  betas <- sapply(1 : nosim, function(i)makelms(x1, x2, x3))
  round(apply(betas, 1, var), 5)
}

# Regressor generation process 2.
rgp2 <- function(){
  print("Processing. Please wait.")
  # number of samples per simulation
  n <- 100
  # number of simulations
  nosim <- 1000
  # set seed for reproducibility
  set.seed(4321)
  # Point C
  x1 <- rnorm(n)
  x2 <- x1/sqrt(2) + rnorm(n) /sqrt(2)
  x3 <- x1 * 0.95 + rnorm(n) * sqrt(1 - 0.95^2)
  # Point D
  betas <- sapply(1 : nosim, function(i)makelms(x1, x2, x3))
  round(apply(betas, 1, var), 5)
}

我尝试了以下内容:

debug(rgp1)
rgp1

它完成了所有步骤,而进入sapply部分,这是我真正想要调试的部分。

我还尝试在多个位置插入browser(),以及debugonce()但未成功。

0 个答案:

没有答案