2
2
# terms of the GNU General Public License as published by the Free Software
3
3
# Foundation; either version 3 of the License, or (at your option) any later
4
4
# version.
5
- #
5
+ #
6
6
# shinystan is distributed in the hope that it will be useful, but WITHOUT ANY
7
7
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
8
8
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9
- #
9
+ #
10
10
# You should have received a copy of the GNU General Public License along with
11
11
# this program; if not, see <http://www.gnu.org/licenses/>.
12
12
13
13
14
14
# ' 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
17
17
# ' \url{http://www.shinyapps.io/} to sign up.
18
- # '
18
+ # '
19
19
# ' @export
20
20
# ' @template args-sso
21
21
# ' @param appName The name to use for the application. Application names must be
22
22
# ' at least four characters long and may only contain letters, numbers, dashes
23
23
# ' 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
25
25
# ' account is configured on the system.
26
26
# ' @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
28
28
# ' \code{FALSE} is if you just want to check that the preprocessing before
29
29
# ' 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
34
34
# ' 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
37
37
# ' \code{ppcheck_yrep} can be specified. \code{ppcheck_data} should be a
38
38
# ' vector of observations to use for graphical posterior predictive checking
39
39
# ' and \code{ppcheck_yrep} should be a character string naming the parameter
40
40
# ' in \code{sso} containing the posterior predictive simulations/replications.
41
41
# ' 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.
43
43
# ' \code{ppcheck_yrep} (but not \code{ppcheck_data}) can also be set
44
44
# ' 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
47
47
# ' comes with this package.
48
- # '
48
+ # '
49
49
# ' \url{http://www.shinyapps.io/} to sign up for a free or paid ShinyApps
50
50
# ' account and for details on how to configure your account on your local
51
51
# ' system using RStudio's \pkg{\link[rsconnect]{rsconnect}} package.
52
- # '
52
+ # '
53
53
# ' @examples
54
54
# ' \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'.
58
58
# '
59
- # ' deploy_shinystan(sso, appName = "my-model", account = "username")
59
+ # ' deploy_shinystan(sso, appName = "my-model", account = "username")
60
60
# '
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.
63
63
# '
64
64
# ' deploy_shinystan(sso, appName = "my-model")
65
65
# ' }
66
- # '
66
+ # '
67
67
# ' @importFrom rsconnect deployApp
68
- # '
68
+ # '
69
69
deploy_shinystan <- function (sso , appName , account = NULL , ... , deploy = TRUE ) {
70
70
sso_check(sso )
71
71
if (missing(appName ))
72
72
stop(" 'appName' is required." )
73
-
73
+
74
74
# copy contents to temporary directory and write necessary additional lines to
75
75
# ui, server, and global
76
76
appDir <- tempdir()
77
77
deployDir <- file.path(appDir , " ShinyStan" )
78
78
contents <- system.file(" ShinyStan" , package = " shinystan" )
79
79
file.copy(from = contents , to = appDir , recursive = TRUE )
80
-
80
+
81
81
server_pkgs <- c(
82
82
" shiny" ,
83
83
" shinyjs" ,
84
+ " colourpicker" ,
84
85
" markdown" ,
85
86
" shinythemes"
86
87
)
@@ -99,25 +100,22 @@ deploy_shinystan <- function(sso, appName, account = NULL, ..., deploy = TRUE) {
99
100
server_lines <- paste0(" library(" , server_pkgs , " );" )
100
101
ui_lines <- paste0(" library(" , ui_pkgs , " );" )
101
102
global_lines <- paste(
102
- " load('sso.RData');" ,
103
+ " load('sso.RData');" ,
103
104
" if (file.exists('y.RData')) load('y.RData')"
104
105
)
105
106
for (ff in c(" ui" , " server" , " global" )) {
106
107
file_name <- file.path(deployDir , paste0(ff , " .R" ))
107
108
fconn <- file(file_name , ' r+' )
108
109
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
- }
113
110
new_lines <- get(paste0(ff , " _lines" ))
114
111
writeLines(c(new_lines , original_content ), con = fconn )
115
112
close(fconn )
116
113
}
117
-
114
+
118
115
# 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
+
121
119
# save ppcheck_data and set ppcheck defaults
122
120
pp <- list (... )
123
121
if (" ppcheck_data" %in% names(pp )) {
@@ -130,10 +128,10 @@ deploy_shinystan <- function(sso, appName, account = NULL, ..., deploy = TRUE) {
130
128
y_name = " y"
131
129
)
132
130
}
133
-
131
+
134
132
if (! deploy )
135
133
return (invisible (deployDir ))
136
-
134
+
137
135
rsconnect :: deployApp(
138
136
appDir = deployDir ,
139
137
appName = appName ,
@@ -144,24 +142,17 @@ deploy_shinystan <- function(sso, appName, account = NULL, ..., deploy = TRUE) {
144
142
145
143
146
144
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
148
146
set_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 ),
150
148
length(yrep_name ) == 1 , length(y_name ) == 1 )
151
149
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 )
160
154
}
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 ))
165
156
}
166
157
167
158
.write_files <- function (files , lines ) {
@@ -173,25 +164,22 @@ set_ppcheck_defaults <- function(appDir, yrep_name, y_name = "y") {
173
164
}
174
165
}
175
166
176
- .y_lines <- function (y_name = " y" ) {
167
+ .ppc_lines <- function (y_name = " y" , yrep_name ) {
177
168
paste0(
178
- " output$ui_pp_y_from_r <- renderUI({
169
+ " output$ui_pp_get_y <- renderUI({
179
170
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),
182
173
selected = '" , y_name ," ')
183
- })" )
184
- }
174
+ })
185
175
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
190
178
choices <- strsplit(choices, split = '[', fixed = TRUE)
191
179
choices <- lapply(choices, function(i) return(i[1]))
192
180
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'),
195
183
choices = c('', choices),
196
184
selected = '" , yrep_name ," ')
197
185
})"
0 commit comments