| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/devmode.R |
| 3 |
\name{devmode} |
| 4 |
\alias{devmode} |
| 5 |
\alias{in_devmode} |
| 6 |
\alias{with_devmode} |
| 7 |
\alias{devmode_inform} |
| 8 |
\alias{register_devmode_option} |
| 9 |
\alias{get_devmode_option} |
| 10 |
\title{Shiny Developer Mode} |
| 11 |
\usage{ |
| 12 |
devmode( |
| 13 |
devmode = getOption("shiny.devmode", TRUE), |
| 14 |
verbose = getOption("shiny.devmode.verbose", TRUE) |
| 15 |
) |
| 16 |
|
| 17 |
in_devmode() |
| 18 |
|
| 19 |
with_devmode(devmode, code, verbose = getOption("shiny.devmode.verbose", TRUE)) |
| 20 |
|
| 21 |
devmode_inform( |
| 22 |
message, |
| 23 |
.frequency = "regularly", |
| 24 |
.frequency_id = message, |
| 25 |
.file = stderr(), |
| 26 |
... |
| 27 |
) |
| 28 |
|
| 29 |
register_devmode_option(name, devmode_message = NULL, devmode_default = NULL) |
| 30 |
|
| 31 |
get_devmode_option( |
| 32 |
name, |
| 33 |
default = NULL, |
| 34 |
devmode_default = missing_arg(), |
| 35 |
devmode_message = missing_arg() |
| 36 |
) |
| 37 |
} |
| 38 |
\arguments{ |
| 39 |
\item{devmode}{Logical value which should be set to \code{TRUE} to enable Shiny |
| 40 |
Developer Mode} |
| 41 |
|
| 42 |
\item{verbose}{Logical value which should be set to \code{TRUE} display Shiny |
| 43 |
Developer messages} |
| 44 |
|
| 45 |
\item{code}{Code to execute with the temporary Dev Mode options set} |
| 46 |
|
| 47 |
\item{message}{Developer Mode message to be sent to \code{\link[rlang:abort]{rlang::inform()}}} |
| 48 |
|
| 49 |
\item{.frequency}{Frequency of the Developer Mode message used with |
| 50 |
\code{\link[rlang:abort]{rlang::inform()}}. Defaults to once every 8 hours.} |
| 51 |
|
| 52 |
\item{.frequency_id}{\code{\link[rlang:abort]{rlang::inform()}} message identifier. Defaults to |
| 53 |
\code{message}.} |
| 54 |
|
| 55 |
\item{.file}{Output connection for \code{\link[rlang:abort]{rlang::inform()}}. Defaults to \code{\link[=stderr]{stderr()}}} |
| 56 |
|
| 57 |
\item{...}{Parameters passed to \code{\link[rlang:abort]{rlang::inform()}}} |
| 58 |
|
| 59 |
\item{name}{Name of option to look for in \code{options()}} |
| 60 |
|
| 61 |
\item{devmode_message}{Message to display once every 8 hours when utilizing |
| 62 |
the \code{devmode_default} value. If \code{devmode_message} is missing, the |
| 63 |
registered \code{devmode_message} value be used.} |
| 64 |
|
| 65 |
\item{devmode_default}{Default value to return if \code{in_devmode()} returns |
| 66 |
\code{TRUE} and the specified option is not set in \code{\link[=options]{options()}}. For |
| 67 |
\code{get_devmode_option()}, if \code{devmode_default} is missing, the |
| 68 |
registered \code{devmode_default} value will be used.} |
| 69 |
|
| 70 |
\item{default}{Default value to return if \code{in_devmode()} returns |
| 71 |
\code{TRUE} and the specified option is not set in \code{\link[=options]{options()}}.} |
| 72 |
} |
| 73 |
\description{ |
| 74 |
\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} |
| 75 |
|
| 76 |
Developer Mode enables a number of \code{\link[=options]{options()}} to make a developer's life |
| 77 |
easier, like enabling non-minified JS and printing messages about |
| 78 |
deprecated functions and options. |
| 79 |
|
| 80 |
Shiny Developer Mode can be enabled by calling \code{devmode(TRUE)} and disabled |
| 81 |
by calling \code{devmode(FALSE)}. |
| 82 |
|
| 83 |
Please see the function descriptions for more details. |
| 84 |
} |
| 85 |
\section{Functions}{ |
| 86 |
\itemize{ |
| 87 |
\item \code{devmode()}: Function to set two options to enable/disable Shiny |
| 88 |
Developer Mode and Developer messages |
| 89 |
|
| 90 |
\item \code{in_devmode()}: Determines if Shiny is in Developer Mode. If the |
| 91 |
\code{getOption("shiny.devmode")} is set to \code{TRUE} and not in testing inside |
| 92 |
\code{testthat}, then Shiny Developer Mode is enabled. |
| 93 |
|
| 94 |
\item \code{with_devmode()}: Temporarily set Shiny Developer Mode and Developer |
| 95 |
message verbosity |
| 96 |
|
| 97 |
\item \code{devmode_inform()}: If Shiny Developer Mode and verbosity are enabled, |
| 98 |
displays a message once every 8 hrs (by default) |
| 99 |
|
| 100 |
\item \code{register_devmode_option()}: Registers a Shiny Developer Mode option with an updated |
| 101 |
value and Developer message. This registration method allows package |
| 102 |
authors to write one message in a single location. |
| 103 |
|
| 104 |
For example, the following Shiny Developer Mode options are registered: |
| 105 |
|
| 106 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{# Reload the Shiny app when a sourced R file changes |
| 107 |
register_devmode_option( |
| 108 |
"shiny.autoreload", |
| 109 |
"Turning on shiny autoreload. To disable, call `options(shiny.autoreload = FALSE)`", |
| 110 |
devmode_default = TRUE |
| 111 |
) |
| 112 |
|
| 113 |
# Use the unminified Shiny JavaScript file, `shiny.js` |
| 114 |
register_devmode_option( |
| 115 |
"shiny.minified", |
| 116 |
"Using full shiny javascript file. To use the minified version, call `options(shiny.minified = TRUE)`", |
| 117 |
devmode_default = FALSE |
| 118 |
) |
| 119 |
|
| 120 |
# Display the full stack trace when errors occur during Shiny app execution |
| 121 |
register_devmode_option( |
| 122 |
"shiny.fullstacktrace", |
| 123 |
"Turning on full stack trace. To disable, call `options(shiny.fullstacktrace = FALSE)`", |
| 124 |
devmode_default = TRUE |
| 125 |
) |
| 126 |
}\if{html}{\out{</div>}} |
| 127 |
|
| 128 |
Other known, non-Shiny Developer Mode options: |
| 129 |
\itemize{ |
| 130 |
\item Sass: |
| 131 |
} |
| 132 |
|
| 133 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{# Display the full stack trace when errors occur during Shiny app execution |
| 134 |
register_devmode_option( |
| 135 |
"sass.cache", |
| 136 |
"Turning off sass cache. To use default caching, call `options(sass.cache = TRUE)`", |
| 137 |
devmode_default = FALSE |
| 138 |
) |
| 139 |
}\if{html}{\out{</div>}} |
| 140 |
|
| 141 |
\item \code{get_devmode_option()}: Provides a consistent way to change the expected |
| 142 |
\code{\link[=getOption]{getOption()}} behavior when Developer Mode is enabled. This method is very |
| 143 |
similar to \code{\link[=getOption]{getOption()}} where the globally set option takes precedence. |
| 144 |
See section "Avoiding direct dependency on shiny" for |
| 145 |
\code{get_devmode_option()} implementation details. |
| 146 |
|
| 147 |
\strong{Package developers:} Register your Dev Mode option using |
| 148 |
\code{register_devmode_option()} to avoid supplying the same \code{devmode_default} |
| 149 |
and \code{devmode_message} values throughout your package. (This requires a |
| 150 |
\pkg{shiny} dependency.) |
| 151 |
|
| 152 |
}} |
| 153 |
\section{Avoiding direct dependency on shiny}{ |
| 154 |
|
| 155 |
|
| 156 |
The methods explained in this help file act independently from the rest of |
| 157 |
Shiny but are included to provide blue prints for your own packages. If |
| 158 |
your package already has (or is willing to take) a dependency on Shiny, we |
| 159 |
recommend using the exported Shiny methods for consistent behavior. Note |
| 160 |
that if you use exported Shiny methods, it will cause the Shiny package to |
| 161 |
load. This may be undesirable if your code will be used in (for example) R |
| 162 |
Markdown documents that do not have a Shiny runtime (\code{runtime: shiny}). |
| 163 |
|
| 164 |
If your package can \strong{not} take a dependency on Shiny, we recommending |
| 165 |
re-implementing these two functions: |
| 166 |
|
| 167 |
\enumerate{ |
| 168 |
\item \code{in_devmode()}: |
| 169 |
|
| 170 |
This function should return \code{TRUE} if \code{getOption("shiny.devmode")} is set. |
| 171 |
In addition, we strongly recommend that it also checks to make sure |
| 172 |
\code{testthat} is not testing. |
| 173 |
|
| 174 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{in_devmode <- function() \{ |
| 175 |
isTRUE(getOption("shiny.devmode", FALSE)) && |
| 176 |
!identical(Sys.getenv("TESTTHAT"), "true") |
| 177 |
\} |
| 178 |
}\if{html}{\out{</div>}} |
| 179 |
|
| 180 |
\item \code{get_devmode_option(name, default, devmode_default, devmode_message)}: |
| 181 |
|
| 182 |
This function is similar to \code{getOption(name, default)}, but when the option |
| 183 |
is not set, the default value changes depending on the Dev Mode. |
| 184 |
\code{get_devmode_option()} should be implemented as follows: |
| 185 |
\itemize{ |
| 186 |
\item If not in Dev Mode: |
| 187 |
\itemize{ |
| 188 |
\item Return \code{getOption(name, default)}. |
| 189 |
} |
| 190 |
\item If in Dev Mode: |
| 191 |
\itemize{ |
| 192 |
\item Get the global option \code{getOption(name)} value. |
| 193 |
\item If the global option value is set: |
| 194 |
\itemize{ |
| 195 |
\item Return the value. |
| 196 |
} |
| 197 |
\item If the global option value is not set: |
| 198 |
\itemize{ |
| 199 |
\item Notify the developer that the Dev Mode default value will be used. |
| 200 |
\item Return the Dev Mode default value. |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
When notifying the developer that the default value has changed, we strongly |
| 206 |
recommend displaying a message (\code{devmode_message}) to \code{stderr()} once every 8 |
| 207 |
hours using \code{\link[rlang:abort]{rlang::inform()}}. This will keep the author up to date as to |
| 208 |
which Dev Mode options are being altered. To allow developers a chance to |
| 209 |
disable Dev Mode messages, the message should be skipped if |
| 210 |
\code{getOption("shiny.devmode.verbose", TRUE)} is not \code{TRUE}. |
| 211 |
|
| 212 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{get_devmode_option <- function(name, default = NULL, devmode_default, devmode_message) \{ |
| 213 |
if (!in_devmode()) \{ |
| 214 |
# Dev Mode disabled, act like `getOption()` |
| 215 |
return(getOption(name, default = default)) |
| 216 |
\} |
| 217 |
|
| 218 |
# Dev Mode enabled, update the default value for `getOption()` |
| 219 |
getOption(name, default = \{ |
| 220 |
# Notify developer |
| 221 |
if ( |
| 222 |
!missing(devmode_message) && |
| 223 |
!is.null(devmode_message) && |
| 224 |
getOption("shiny.devmode.verbose", TRUE) |
| 225 |
) \{ |
| 226 |
rlang::inform( |
| 227 |
message = devmode_message, |
| 228 |
.frequency = "regularly", |
| 229 |
.frequency_id = devmode_message, |
| 230 |
.file = stderr() |
| 231 |
) |
| 232 |
\} |
| 233 |
|
| 234 |
# Return Dev Mode default value `devmode_default` |
| 235 |
devmode_default |
| 236 |
\}) |
| 237 |
\} |
| 238 |
}\if{html}{\out{</div>}} |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
The remaining functions in this file are used for author convenience and are |
| 243 |
not recommended for all reimplementation situations. |
| 244 |
} |
| 245 |
|
| 246 |
\examples{ |
| 247 |
# Enable Shiny Developer mode |
| 248 |
devmode() |
| 249 |
|
| 250 |
in_devmode() # TRUE/FALSE? |
| 251 |
|
| 252 |
# Execute code in a temporary shiny dev mode |
| 253 |
with_devmode(TRUE, in_devmode()) # TRUE |
| 254 |
|
| 255 |
# Ex: Within shiny, we register the option "shiny.minified" |
| 256 |
# to default to `FALSE` when in Dev Mode |
| 257 |
\dontrun{register_devmode_option( |
| 258 |
"shiny.minified", |
| 259 |
devmode_message = paste0( |
| 260 |
"Using full shiny javascript file. ", |
| 261 |
"To use the minified version, call `options(shiny.minified = TRUE)`" |
| 262 |
), |
| 263 |
devmode_default = FALSE |
| 264 |
)} |
| 265 |
|
| 266 |
# Used within `shiny::runApp(launch.browser)` |
| 267 |
get_devmode_option("shiny.minified", TRUE) # TRUE if Dev mode is off |
| 268 |
is_minified <- with_devmode(TRUE, { |
| 269 |
get_devmode_option("shiny.minified", TRUE) |
| 270 |
}) |
| 271 |
is_minified # FALSE |
| 272 |
|
| 273 |
} |