So I have a R/shiny project on Github that require some packages, for example shinyjs, V8 and dplyr, and I have specified required(shinyjs)
and library(shinyjs)
in the code.
On my computer it works just fine and if I download a copy from Github that works too, but if I do it from a different computer I have to download the packages required manually.
Is there a way to make Rstudio install the required packages automatically when someone tries to run the application?
This does that. Got the function here: malonypatr's install_load function
The screenshots are from RTVS, but I tested it in R-Studio too.
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")
})
}
)
Loading:
Yielding:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.