我是一個使用 R 編程的完全新手,并且堅持我認為實際上是一個非常簡單的問題。我借了一些代碼片段并將它們放在一起,一切似乎都可以正常作業,只是列印必須從 GitHub 安裝的包名稱不會..
如何在 askyesno 函式中列印一個與字串一起列出的變數。我嘗試了 {},[] 并將它們加倍,嘗試了 "" 和 .format 就像在 python 中一樣,沒有任何效果。在下面我的代碼中,請幫助:)
not_installed = my_packages[!(my_packages %in% installed.packages()[ , "Package"])]
if(length(not_installed)) install.packages(not_installed)
if(length(not_installed != installed.packages()))
still_not_installed = list(not_installed)
Ask = askYesNo("$still_not_installed cannot be install from CRAN. \n Load from GitHub?",
default = TRUE, prompts = getOption("askYesNo"), gettext(c("Yes", "No", "Cancel")))
if(Ask == TRUE)
p_load_gh("muschellij2/aal", "taiyun/corrplot/blob/master/R/corrplot-package.R",
install = TRUE, dependencies = TRUE)
您認為這是搜索未安裝的軟體包并加載它們的正確解決方案嗎?
uj5u.com熱心網友回復:
您檢查安裝的方法本身并不理想。例如,它不會檢測包是否缺少依賴項。我們可以使用requirewhich 自動檢查包是否實際可用。然后我們可以用粘貼來構建訊息。我假設您還將包粘貼到您的p_load_gh函式中,但我不知道那個特定的語法。
my_packages <- c("test","test2")
for(p in my_packages)
{
tryCatch(test <- require(p,character.only=T),
warning=function(w) return())
if(!test)
{
print(paste("Package", p, "not found. Installing Package!"))
install.packages(p)
}
tryCatch(test <- require(p,character.only=T),
warning=function(w) return())
if(!test)
{
Ask = askYesNo(paste("Package", p," not installable from CRAN. \n Load from GitHub?", default = TRUE, prompts = getOption("askYesNo"), gettext(c("Yes", "No", "Cancel")))
if(Ask) p_load_gh("muschellij2/aal", "taiyun/corrplot/blob/master/R/corrplot-package.R",
install = TRUE, dependencies = TRUE)
}
}
uj5u.com熱心網友回復:
您可以使用 構建訊息字串paste。toString將很好地連接并用逗號分隔一個向量,然后我們可以paste繼續您的訊息的其余部分:
Ask = askYesNo(
msg = paste(toString(still_not_installed),
"cannot be install from CRAN. \n Load from GitHub?"),
default = TRUE,
prompts = getOption("askYesNo"),
gettext(c("Yes", "No", "Cancel"))
)
我認為你有一個更大的問題。第 1 行:您得到了my_packages未安裝的子集,很好。第 2 行:您嘗試安裝它們,很好。第 3 行:這很糟糕。!=進行元素比較 - 您正在測驗第一個not_installed包是否不等于第一個安裝的包(按字母順序),然??后將第二個與第二個進行比較,等等。然后您正在測驗生成的布爾向量是否有任何長度---它會的。相反,我建議更新not_installed串列,只需重復第 1 行即可更新已卸載軟體包的串列。而且您不需要list()它們,將它們保留為字符向量。
此外,我們應該將嘗試if(Ask == TRUE)嵌套在if()需要的 github 包中。
not_installed = my_packages[!(my_packages %in% installed.packages()[ , "Package"])]
if(length(not_installed)) install.packages(not_installed)
still_not_installed = my_packages[!(my_packages %in% installed.packages()[, "Package"])]
if(length(still_not_installed)) {
Ask = askYesNo(
msg = paste(toString(still_not_installed),
"cannot be install from CRAN. \n Load from GitHub?"),
default = TRUE,
prompts = getOption("askYesNo"),
gettext(c("Yes", "No", "Cancel"))
)
if(Ask == TRUE) {
# This code could still be improved, it assumes if we get to
# this point that both packages are missing, but it might
# only be one of them...
p_load_gh(
"muschellij2/aal",
"taiyun/corrplot/blob/master/R/corrplot-package.R",
install = TRUE, dependencies = TRUE)
)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/409632.html
標籤:
下一篇:從自定義深度的多維陣列中提取專案
