首页 > 解决方案 > 在 pycharm 中调试 R 代码:无法运行用户定义的函数?

问题描述

我有一个非常简单的 R 脚本(实际上只是从“真实”脚本中提取的几行......)

suppressPackageStartupMessages(library("klaR"))
suppressPackageStartupMessages(library("caret"))
# The diabetes data has been modified with a header line
wdat <- read.csv('/git/uni/data/pima-indians-diabetes.csv', header=TRUE)

X <- wdat[,-c(9)]
Y <- wdat[,9]
set.seed(1)

xsquared = function(x) { x * x}

for (wi in 1:2) {
  ttSplitx <- createDataPartition(y=Y, p=.8, list=FALSE)
  Xx <- X
  nxTrain <- Xx[ttSplitx, ]
  nyTrain <- Y[ttSplitx]
  labelTrue <- nyTrain>0
  posCnt <- nxTrain[labelTrue, ]
  negCnt <- nxTrain[!labelTrue,]
  posMean <- sapply(posCnt, mean, na.rm=TRUE)
  posSd <- sapply(posCnt, sd, na.rm=TRUE)
  posOffsets <- t(t(nxTrain)-posMean)
  posScaled <- t(t(posOffsets)/posSd)
  posLogs <- -(1/2)*rowSums(apply(posScaled,c(1, 2),
    function(x) { x * x} ), na.rm=TRUE)-sum(log(posSd))   # Line 25
    # function(x) { xsquared(x)  }), na.rm=TRUE)-sum(log(posSd)) # Line 26
}
print('done')

上面的代码在启用调试的情况下运行大约三秒钟。

但是当line26使用而不是line25通过交换评论时 - 它会运行但在调试模式下会无限期挂起。

  posLogs <- -(1/2)*rowSums(apply(posScaled,c(1, 2),
    #function(x) { x * x} ), na.rm=TRUE)-sum(log(posSd))   # Line 25
    function(x) { xsquared(x)  }), na.rm=TRUE)-sum(log(posSd)) # Line 26

这明确表明pycharm无法调试到

xsquared = function(x) { x * x}

对于我尝试定义的任何函数都是如此。

那么..为什么不能通过pycharm R调试器调用用户函数?

标签: rpycharm

解决方案


插件开发人员提出了一个问题,他们回应说该功能目前不可用。

https://github.com/holgerbrandl/r4intellij/issues/174


推荐阅读