22# terms of the GNU General Public License as published by the Free Software
33# Foundation; either version 3 of the License, or (at your option) any later
44# version.
5- #
5+ #
66# shinystan is distributed in the hope that it will be useful, but WITHOUT ANY
77# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
88# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9- #
9+ #
1010# You should have received a copy of the GNU General Public License along with
1111# this program; if not, see <http://www.gnu.org/licenses/>.
1212
1313
1414# ' Deploy a ShinyStan app on the web using shinyapps.io by RStudio
15- # '
16- # ' Requires a (free or paid) ShinyApps account. Visit
15+ # '
16+ # ' Requires a (free or paid) ShinyApps account. Visit
1717# ' \url{http://www.shinyapps.io/} to sign up.
18- # '
18+ # '
1919# ' @export
2020# ' @template args-sso
2121# ' @param appName The name to use for the application. Application names must be
2222# ' at least four characters long and may only contain letters, numbers, dashes
2323# ' and underscores.
24- # ' @param account shinyapps.io account username. Only required if more than one
24+ # ' @param account shinyapps.io account username. Only required if more than one
2525# ' account is configured on the system.
2626# ' @param ... Optional arguments. See Details.
27- # ' @param deploy Should the app be deployed? The only reason for this to be
27+ # ' @param deploy Should the app be deployed? The only reason for this to be
2828# ' \code{FALSE} is if you just want to check that the preprocessing before
2929# ' deployment is successful.
30- # '
31- # ' @return \link[=invisible]{Invisibly}, \code{TRUE} if deployment succeeded
32- # ' (did not encounter an error) or, if \code{deploy} argument is set to
33- # ' \code{FALSE}, the path to the temporary directory containing the app ready
30+ # '
31+ # ' @return \link[=invisible]{Invisibly}, \code{TRUE} if deployment succeeded
32+ # ' (did not encounter an error) or, if \code{deploy} argument is set to
33+ # ' \code{FALSE}, the path to the temporary directory containing the app ready
3434# ' for deployment (also invisibly).
35- # '
36- # ' @details In \code{...}, the arguments \code{ppcheck_data} and
35+ # '
36+ # ' @details In \code{...}, the arguments \code{ppcheck_data} and
3737# ' \code{ppcheck_yrep} can be specified. \code{ppcheck_data} should be a
3838# ' vector of observations to use for graphical posterior predictive checking
3939# ' and \code{ppcheck_yrep} should be a character string naming the parameter
4040# ' in \code{sso} containing the posterior predictive simulations/replications.
4141# ' The value of \code{ppcheck_yrep} is only used to preselect the appropriate
42- # ' parameter/generated quantity to use for the posterior predictive checking.
42+ # ' parameter/generated quantity to use for the posterior predictive checking.
4343# ' \code{ppcheck_yrep} (but not \code{ppcheck_data}) can also be set
4444# ' interactively on shinyapps.io when using the app.
45- # '
46- # ' @seealso The example in the \emph{Deploying to shinyapps.io} vignette that
45+ # '
46+ # ' @seealso The example in the \emph{Deploying to shinyapps.io} vignette that
4747# ' comes with this package.
48- # '
48+ # '
4949# ' \url{http://www.shinyapps.io/} to sign up for a free or paid ShinyApps
5050# ' account and for details on how to configure your account on your local
5151# ' system using RStudio's \pkg{\link[rsconnect]{rsconnect}} package.
52- # '
52+ # '
5353# ' @examples
5454# ' \dontrun{
55- # ' # For this example assume sso is the name of the shinystan object for
56- # ' # the model you want to use. Assume also that you want to name your app
57- # ' # 'my-model' and that your shinyapps.io username is 'username'.
55+ # ' # For this example assume sso is the name of the shinystan object for
56+ # ' # the model you want to use. Assume also that you want to name your app
57+ # ' # 'my-model' and that your shinyapps.io username is 'username'.
5858# '
59- # ' deploy_shinystan(sso, appName = "my-model", account = "username")
59+ # ' deploy_shinystan(sso, appName = "my-model", account = "username")
6060# '
61- # ' # If you only have one ShinyApps account configured then you can also omit
62- # ' # the 'account' argument.
61+ # ' # If you only have one ShinyApps account configured then you can also omit
62+ # ' # the 'account' argument.
6363# '
6464# ' deploy_shinystan(sso, appName = "my-model")
6565# ' }
66- # '
66+ # '
6767# ' @importFrom rsconnect deployApp
68- # '
68+ # '
6969deploy_shinystan <- function (sso , appName , account = NULL , ... , deploy = TRUE ) {
7070 sso_check(sso )
7171 if (missing(appName ))
7272 stop(" 'appName' is required." )
73-
73+
7474 # copy contents to temporary directory and write necessary additional lines to
7575 # ui, server, and global
7676 appDir <- tempdir()
7777 deployDir <- file.path(appDir , " ShinyStan" )
7878 contents <- system.file(" ShinyStan" , package = " shinystan" )
7979 file.copy(from = contents , to = appDir , recursive = TRUE )
80-
80+
8181 server_pkgs <- c(
8282 " shiny" ,
8383 " shinyjs" ,
84+ " colourpicker" ,
8485 " markdown" ,
8586 " shinythemes"
8687 )
@@ -99,25 +100,22 @@ deploy_shinystan <- function(sso, appName, account = NULL, ..., deploy = TRUE) {
99100 server_lines <- paste0(" library(" , server_pkgs , " );" )
100101 ui_lines <- paste0(" library(" , ui_pkgs , " );" )
101102 global_lines <- paste(
102- " load('sso.RData');" ,
103+ " load('sso.RData');" ,
103104 " if (file.exists('y.RData')) load('y.RData')"
104105 )
105106 for (ff in c(" ui" , " server" , " global" )) {
106107 file_name <- file.path(deployDir , paste0(ff , " .R" ))
107108 fconn <- file(file_name , ' r+' )
108109 original_content <- readLines(fconn )
109- if (ff %in% c(" ui" , " server" )) {
110- sel <- grep(" .SHINYSTAN_OBJECT" , original_content )
111- original_content <- original_content [- sel ]
112- }
113110 new_lines <- get(paste0(ff , " _lines" ))
114111 writeLines(c(new_lines , original_content ), con = fconn )
115112 close(fconn )
116113 }
117-
114+
118115 # save sso to deployDir
119- object <- sso
120- save(object , file = file.path(deployDir , " sso.RData" ))
116+ .SHINYSTAN_OBJECT <- sso
117+ save(.SHINYSTAN_OBJECT , file = file.path(deployDir , " sso.RData" ))
118+
121119 # save ppcheck_data and set ppcheck defaults
122120 pp <- list (... )
123121 if (" ppcheck_data" %in% names(pp )) {
@@ -130,10 +128,10 @@ deploy_shinystan <- function(sso, appName, account = NULL, ..., deploy = TRUE) {
130128 y_name = " y"
131129 )
132130 }
133-
131+
134132 if (! deploy )
135133 return (invisible (deployDir ))
136-
134+
137135 rsconnect :: deployApp(
138136 appDir = deployDir ,
139137 appName = appName ,
@@ -144,24 +142,17 @@ deploy_shinystan <- function(sso, appName, account = NULL, ..., deploy = TRUE) {
144142
145143
146144
147- # functions to set defaults for ppcheck shiny::selectInput for y and y_rep
145+ # functions to set defaults for ppcheck shiny::selectInput for y and y_rep
148146set_ppcheck_defaults <- function (appDir , yrep_name , y_name = " y" ) {
149- stopifnot(is.character(yrep_name ), is.character(y_name ),
147+ stopifnot(is.character(yrep_name ), is.character(y_name ),
150148 length(yrep_name ) == 1 , length(y_name ) == 1 )
151149 fileDir <- file.path(appDir , " server_files" , " pages" , " diagnose" , " ppcheck" , " ui" )
152- y_file <- file.path(fileDir , " pp_y_from_r.R" )
153- yrep_file <- file.path(fileDir , " pp_yrep_from_sso.R" )
154- for (file in c(" y_file" , " yrep_file" )) {
155- f <- get(file )
156- if (file.exists(f )) {
157- file.remove(f )
158- file.create(f )
159- }
150+ ppc_file <- file.path(fileDir , " pp_get_y_and_yrep.R" )
151+ if (file.exists(ppc_file )) {
152+ file.remove(ppc_file )
153+ file.create(ppc_file )
160154 }
161- .write_files(
162- files = c(y_file , yrep_file ),
163- lines = c(.y_lines(y_name ), .yrep_lines(yrep_name ))
164- )
155+ .write_files(files = ppc_file , lines = .ppc_lines(y_name , yrep_name ))
165156}
166157
167158.write_files <- function (files , lines ) {
@@ -173,25 +164,22 @@ set_ppcheck_defaults <- function(appDir, yrep_name, y_name = "y") {
173164 }
174165}
175166
176- .y_lines <- function (y_name = " y" ) {
167+ .ppc_lines <- function (y_name = " y" , yrep_name ) {
177168 paste0(
178- " output$ui_pp_y_from_r <- renderUI({
169+ " output$ui_pp_get_y <- renderUI({
179170 choices <- objects(envir = .GlobalEnv)
180- selectizeInput('y_name', label = span(style = 'color: #337ab7;', 'y, a vector of observations'),
181- choices = c('', choices),
171+ selectizeInput('y_name', label = span(style = 'color: #337ab7;', 'y, a vector of observations'),
172+ choices = c('', choices),
182173 selected = '" , y_name ," ')
183- })" )
184- }
174+ })
185175
186- .yrep_lines <- function (yrep_name ) {
187- paste0(
188- " output$ui_pp_yrep_from_sso <- renderUI({
189- choices <- param_names
176+ output$ui_pp_get_yrep <- renderUI({
177+ choices <- PARAM_NAMES
190178 choices <- strsplit(choices, split = '[', fixed = TRUE)
191179 choices <- lapply(choices, function(i) return(i[1]))
192180 choices <- unique(unlist(choices))
193- selectizeInput('yrep_name',
194- label = span(style = 'color: #337ab7;', 'y_rep, posterior predictive replications'),
181+ selectizeInput('yrep_name',
182+ label = span(style = 'color: #337ab7;', 'y_rep, posterior predictive replications'),
195183 choices = c('', choices),
196184 selected = '" , yrep_name ," ')
197185 })"
0 commit comments