如何在Jupyter笔记本中输入“是”或“否”

问题描述 投票:0回答:2

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

我正在尝试使用

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
作为动态提示的默认值?

r jupyter-notebook jupyter jupyter-irkernel
2个回答
2
投票

在您的示例中,您实际上从未到达处理输入的代码部分。这些输入仅发生在交互式会话中,而 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
打印出消息“Would you like to try to install x now?”,但它只接受交互式会话的输入。

这是

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

0
投票

我没有使用 R 内核对此进行测试,但这可能仍然对某人有帮助。

示例:Python 包管理器 conda

您可以将

-y
添加到安装命令中,以便任何
yes/no
提示都默认为“是”。

这不起作用:

!conda install matplotlib

输出:

Channels:
 - defaults
Platform: linux-64
Collecting package metadata (repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /opt/conda/envs/anaconda-panel-2023.05-py310

  added / updated specs:
    - matplotlib


The following packages will be UPDATED:

  certifi                         2023.7.22-py311h06a4308_0 --> 2023.11.17-py311h06a4308_0 
  openssl                                 3.0.10-h7f8727e_2 --> 3.0.12-h7f8727e_0 


Proceed ([y]/n)? 

我无法输入,因为 Jupyter Notebook 中没有交互式用户提示。

另一方面,

!conda install matplotlib -y

不再要求是/否

示例:apt-get install

您可以使用

避免用户提示
apt-get -y install [packagename]`

请参阅使用 apt-get install 时自动回答“是”。或者检查那里的其他答案,例如:

!DEBIAN_FRONTEND=noninteractive
!apt-get --yes --force-yes install $something
© www.soinside.com 2019 - 2024. All rights reserved.