所以我在 Github 上有一个 R/shiny 项目需要一些包,例如 shinyjs、V8 和 dplyr,我已经指定了 required(shinyjs)
和 library(shinyjs)
在代码中。
在我的电脑上它工作得很好,如果我从 Github 下载一个副本也可以,但如果我从另一台电脑上做,我必须手动下载所需的包。
有没有办法让 Rstudio 在有人尝试运行应用程序时自动安装所需的包?
最佳答案
这样做。在此处获得功能:malonypatr's install_load function
截图来自 RTVS,但我也在 R-Studio 中测试过。
library(shiny)
install_load <- function (package1, ...) {
# convert arguments to vector
packages <- c(package1, ...)
# start loop to determine if each package is installed
for(package in packages){
# if package is installed locally, load
if(package %in% rownames(installed.packages()))
do.call('library', list(package))
# if package is not installed locally, download, then load
else {
install.packages(package)
do.call("library", list(package))
}
}
}
install_load("shinyjs")
shinyApp(
ui = fluidPage(
useShinyjs(), # Set up shinyjs
# Add a CSS class for red text colour
inlineCSS(list(.red = "background: red")),
actionButton("btn", "Click me"),
p(id = "element", "Watch what happens to me")
),
server = function(input, output) {
observeEvent(input$btn, {
# Change the following line for more examples
toggleClass("element", "red")
})
}
)
正在加载:
产量:
关于R Shiny 所需的包和 Github,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41261429/