Debian Codegraph @

R/publish_site.R

raw
1
2 #' Publish an R Markdown Website
3 #'
4 #' Publish a website to RStudio Connect
5 #'
6 #' @inheritParams rsconnect::deploySite
7 #'
8 #' @param site_dir Directory containing website. Defaults to current working directory.
9 #' @param site_name Name for the site (names must be unique within an account). Defaults
10 #' to the `name` provided by the site generator (or to the name of the site_dir if
11 #' there is no `name` specified).
12 #' @param method Publishing method (currently only "rsconnect" is available)
13 #' @param render `TRUE` to render the site locally before publishing.
14 #' @param launch_browser If `TRUE`, the system's default web browser will be launched
15 #' automatically after the site is deployed. Defaults to `TRUE` in interactive sessions
16 #' only.
17 #'
18 #' @examples
19 #' \dontrun{
20 #' library(rmarkdown)
21 #' publish_site()
22 #' }
23 #'
24 #' @export
25 publish_site <- function(site_dir = ".", site_name = NULL,
26 method = c("rsconnect"), server = NULL, account = NULL,
27 render = TRUE, launch_browser = interactive()) {
28
29 # resolve method
30 method <- match.arg(method)
31
32 if (identical(method, "rsconnect")) {
33
34 # confirm that we have rsconnect
35 if (!requireNamespace("rsconnect", quietly = FALSE)) {
36 stop("The rsconnect package is required to publish websites. ",
37 "Please install rsconnect with install.packages(\"rsconnect\")")
38 }
39
40 # check for non shinyapps.io accounts
41 accounts <- rsconnect::accounts()
42 accounts <- subset(accounts, server != "shinyapps.io")
43
44 # if there is no server or account specified then see if we
45 # can default the account
46 if (is.null(server) && is.null(account)) {
47 if (is.null(accounts) || nrow(accounts) == 0)
48 stop("You must specify a server to publish the website to")
49 else if (nrow(accounts) == 1) {
50 account <- accounts$name
51 server <- accounts$server
52 }
53 }
54
55 # handle server
56 if (!is.null(server) && is.null(account)) {
57
58 # get a version of the server with the protocol (strip trailing slash)
59 if (!grepl("^https?://", server))
60 server_with_protocol <- paste0("https://", server)
61 else
62 server_with_protocol <- server
63 server_with_protocol <- sub("/+$", "", server_with_protocol)
64
65 # now strip the protocol if it's there
66 server <- sub("^https?://", "", server_with_protocol)
67 server_name <- server
68
69 # ensure we have this server available
70 accounts <- rsconnect::accounts()
71 accounts <- subset(accounts, server == server_name)
72 if (nrow(accounts) == 0) {
73
74 # prompt
75 message(sprintf("You do not currently have a %s publishing account ", server),
76 "configured on this system.")
77 result = readline("Would you like to configure one now? [Y/n]: ")
78 if (tolower(result) == "n")
79 return(invisible())
80
81 # create server if we need to
82 servers <- rsconnect::servers()
83 if (nrow(subset(servers, servers$name == server)) == 0) {
84
85 rsconnect::addServer(sprintf("%s/__api__", server_with_protocol), server)
86 }
87
88 # connect user
89 rsconnect::connectUser(server = server)
90
91 }
92 else if (nrow(accounts) == 1) {
93
94 account <- accounts$name
95
96 } else {
97
98 stop("There is more than one account registered for ", server,
99 "\nPlease specify which account you want to publish to.")
100
101 }
102 }
103
104 # deploy site
105 rsconnect::deploySite(
106 siteDir = site_dir,
107 siteName = site_name,
108 account = account,
109 server = server,
110 render = if (render) "local" else "none",
111 launch.browser = launch_browser
112 )
113 }
114 }