首页 > 解决方案 > 如何在 Jupyter 笔记本中键入“是”或“否”

问题描述

我已经提到了这篇文章和这篇文章。但这并不能解决我的问题。请不要将其标记为重复

我正在尝试Jupyter Notebook使用R kernel.

model_predictors <- buildModel(flag, fv_full_data, outcomeName, folder)

我收到如下错误消息

Model about to be built   # this is log message and not error

1 package is needed for this model and is not installed. (randomForest). Would you like to try to install it now?    # here I see that it asks for a question but without my input it selects `No`
Error: Required package is missing
Traceback:

1. buildModel(flag, fv_full_data, outcomeName, folder)
2. train(x = trainDF[, predictorsNames], y = factor(trainLabels), 
 .     method = "rf", metric = "Fscore", trControl = objControl, 
 .     tuneGrid = rf_grid, preProcess = c("center", "scale"))
3. train.default(x = trainDF[, predictorsNames], y = factor(trainLabels), 
 .     method = "rf", metric = "Fscore", trControl = objControl, 
 .     tuneGrid = rf_grid, preProcess = c("center", "scale"))
4. checkInstall(models$library)
5. stop("Required package is missing", call. = FALSE)

如何避免此错误并防止 jupyter 选择No作为动态提示的默认值?

标签: rjupyter-notebookjupyterjupyter-irkernel

解决方案


在您的示例中,您实际上从未到达处理输入的代码部分。这些输入只发生在交互式会话中——Jupyter 不是。你可以检查

interactive()
#> FALSE

menu(c("yes", "no"))
#> Error in menu(c("yes", "no")): menu() cannot be used non-interactively
#> Traceback:
#> 
#> 1. menu(c("yes", "no"))
#> 2. stop("menu() cannot be used non-interactively")

caret::checkInstall打印出消息“你想现在尝试安装 x 吗?”,但它只接受交互式会话的输入。

这是caret::checkInstall我的评论的代码。

function (pkg) 
{
    good <- rep(TRUE, length(pkg))
    for (i in seq(along = pkg)) {
        tested <- try(find.package(pkg[i]), silent = TRUE)
        if (inherits(tested, "try-error")) 
            good[i] <- FALSE
    }
    if (any(!good)) {
        pkList <- paste(pkg[!good], collapse = ", ")
        msg <- paste(sum(!good), ifelse(sum(!good) > 1, " packages are", 
            " package is"), " needed for this model and", 
            ifelse(sum(!good) > 1, " are", " is"), 
            " not installed. (", pkList, "). Would you like to try to install", 
            ifelse(sum(!good) > 1, " them", " it"), 
            " now?", sep = "")
        cat(msg) # Print the message
        if (interactive()) { # In Jupyter, `interactive()` is `FALSE`.
            bioc <- c("affy", "logicFS", "gpls", 
                "vbmp")
            installChoice <- menu(c("yes", "no")) # This is where it gets input
            if (installChoice == 1) {
                hasBioc <- any(pkg[!good] %in% bioc)
                if (!hasBioc) {
                  install.packages(pkg[!good])
                }
                else {
                  inst <- pkg[!good]
                  instC <- inst[!(inst %in% bioc)]
                  instB <- inst[inst %in% bioc]
                  if (length(instC) > 0) 
                    install.packages(instC)
                  biocLite <- NULL
                  source("http://bioconductor.org/biocLite.R")
                  biocLite(instB)
                }
            }
            else stop("Required package is missing", call. = FALSE)
        }
        else stop("Required package is missing", call. = FALSE) # Because `interactive()` is `FALSE`, this `stop` is called
    }
}

对于您的情况,install.packages("randomForest")是最好的选择。

如果你在RStudio,你可以使用rstudioapi这样的

f <- function() {
  rstudioapi::sendToConsole("1")
  menu(c("yes", "no"))
}
f()
#> 1: yes
#> 2: no
#> 
#> Selection: 1
#> [1] 1

推荐阅读