如何在 Jupyter notebook 中输入 "Yes" 或 "No"
How to key in "Yes" or "No" in Jupyter notebook
我已经提到了这个 and this 。但这并不能解决我的问题。请不要将其标记为重复
我正在尝试 运行 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
作为动态提示的默认值?
在您的示例中,您实际上永远不会到达处理输入的代码部分。这些输入仅发生在交互式会话中——而 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
我已经提到了这个
我正在尝试 运行 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
作为动态提示的默认值?
在您的示例中,您实际上永远不会到达处理输入的代码部分。这些输入仅发生在交互式会话中——而 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