File: dialogs.R

package info (click to toggle)
r-cran-rstudioapi 0.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 956 kB
  • sloc: makefile: 2
file content (136 lines) | stat: -rw-r--r-- 3,995 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#' Show Dialog Box
#'
#' Shows a dialog box with a given title and contents.
#'
#' @param title The title to display in the dialog box.
#'
#' @param message A character vector with the contents to display in the main
#'   dialog area. Contents can contain the following HTML tags: "p", "em",
#'   "strong", "b" and "i".
#'
#' @param url An optional URL to display under the \code{message}.
#'
#' @param timeout A timeout (in seconds). When set, if the user takes
#'   longer than this timeout to provide a response, the request will be aborted.
#'
#' @note The \code{showDialog} function was added in version 1.1.67 of RStudio.
#'
#' @examples
#' if (rstudioapi::isAvailable()) {
#'   rstudioapi::showDialog("Example Dialog", "This is an <b>example</b> dialog.")
#' }
#'
#' @export
showDialog <- function(title, message, url = "", timeout = 60) {
  opts <- options(rstudioapi.remote.timeout = timeout)
  on.exit(options(opts), add = TRUE)
  callFun("showDialog", title, message, url)
}



#' Updates a Dialog Box
#'
#' Updates specific properties from the current dialog box.
#'
#' Currently, the only dialog with support for this action is the New
#' Connection dialog in which the code preview can be updated through this API.
#'
#' \preformatted{ updateDialog(code = "con <- NULL") }
#'
#' @param ... Named parameters and values to update a dialog box.
#' @note The \code{updateDialog} function was added in version 1.1.67 of
#' RStudio.
#' @export updateDialog
updateDialog <- function(...) {
  callFun("updateDialog", ...)
}



#' Show Prompt Dialog Box
#'
#' Shows a dialog box with a prompt field.
#'
#'
#' @param title The title to display in the dialog box.
#'
#' @param message A character vector with the contents to display in the main
#'   dialog area.
#'
#' @param default An optional character vector that fills the prompt field with
#'   a default value.
#'
#' @param timeout A timeout (in seconds). When set, if the user takes
#'   longer than this timeout to provide a response, the request will be aborted.
#'
#' @note The \code{showPrompt} function was added in version 1.1.67 of RStudio.
#'
#' @export showPrompt
showPrompt <- function(title, message, default = NULL, timeout = 60) {
  opts <- options(rstudioapi.remote.timeout = timeout)
  on.exit(options(opts), add = TRUE)
  callFun("showPrompt", title, message, default)
}



#' Show Question Dialog Box
#'
#' Shows a dialog box asking a question.
#'
#' @param title The title to display in the dialog box.
#'
#' @param message A character vector with the contents to display in the main
#'   dialog area.
#'
#' @param ok And optional character vector that overrides the caption for the
#'   OK button.
#'
#' @param cancel An optional character vector that overrides the caption for
#'   the Cancel button.
#'
#' @param timeout A timeout (in seconds). When set, if the user takes
#'   longer than this timeout to provide a response, the request will be aborted.
#'
#' @note The \code{showQuestion} function was added in version 1.1.67 of
#'   RStudio.
#'
#' @export showQuestion
showQuestion <- function(title, message, ok = NULL, cancel = NULL, timeout = 60) {
  opts <- options(rstudioapi.remote.timeout = timeout)
  on.exit(options(opts), add = TRUE)
  callFun("showQuestion", title, message, ok, cancel)
}



#' Prompt user for secret
#'
#' Request a secret from the user. If the `keyring` package is installed, it
#' will be used to cache requested secrets.
#'
#'
#' @param name The name of the secret.
#'
#' @param message A character vector with the contents to display in the main
#'   dialog area.
#'
#' @param title The title to display in the dialog box.
#'
#' @note The \code{askForSecret} function was added in version 1.1.419 of
#'   RStudio.
#'
#' @export
askForSecret <- function(
  name,
  message = paste(name, ":", sep = ""),
  title = paste(name, "Secret")) {

  if (hasFun("askForSecret") || isJob()) {
    callFun("askForSecret", name, title, message)
  } else {
    askForPassword(message)
  }

}