|
5 | 5 | # |
6 | 6 | # |
7 | 7 |
|
8 | | -# Avoid overwhelming the console |
9 | | -options(max.print = 1000) |
10 | | - |
11 | | -# Enable HTML help |
12 | | -options(help_type = "html") |
13 | | - |
14 | | -# Use internal editor |
15 | | -options(editor = function(file, title, ..., name = NULL) { |
16 | | - handler_editor(file = file, title = title, ..., name = name) |
17 | | -}) |
18 | | - |
19 | | -# Use custom browser implementation |
20 | | -options(browser = function(url) { |
21 | | - .ps.Call("ps_browse_url", as.character(url)) |
22 | | -}) |
23 | | - |
24 | | -# Register our password handler as the generic `askpass` option. |
25 | | -# Same as RStudio, see `?rstudioapi::askForPassword` for rationale. |
26 | | -options(askpass = function(prompt) { |
27 | | - .ps.ui.askForPassword(prompt) |
28 | | -}) |
29 | | - |
30 | | -# Show Plumber apps in the viewer |
31 | | -options(plumber.docs.callback = function(url) { |
32 | | - .ps.ui.showUrl(url) |
33 | | -}) |
34 | | - |
35 | | -# Show Shiny applications in the viewer |
36 | | -options(shiny.launch.browser = function(url) { |
37 | | - .ps.ui.showUrl(url) |
38 | | -}) |
39 | | - |
40 | | -# Show Profvis output in the viewer |
41 | | -options(profvis.print = function(x) { |
42 | | - # Render the widget to a tag list to create standalone HTML output. |
43 | | - # (htmltools is a Profvis dependency so it's guaranteed to be available) |
44 | | - rendered <- htmltools::as.tags(x, standalone = TRUE) |
45 | | - |
46 | | - # Render the HTML content to a temporary file |
47 | | - tmp_file <- htmltools::html_print(rendered, viewer = NULL) |
48 | | - |
49 | | - # Pass the file to the viewer |
50 | | - .ps.Call("ps_html_viewer", tmp_file, "R Profile", -1L, "editor") |
51 | | -}) |
| 8 | +# Called from Rust after sourcing the user's Rprofile so that user-defined |
| 9 | +# options take precedence over our defaults. |
| 10 | +initialize_options <- function() { |
| 11 | + # These options have non-NULL defaults in R, so we can't detect user |
| 12 | + # overrides by checking for NULL. They are always set unless the user |
| 13 | + # has listed the option name in `positron.protected_options`. |
| 14 | + |
| 15 | + # Use Positron editor |
| 16 | + set_override( |
| 17 | + "editor", |
| 18 | + function(file, title, ..., name = NULL) { |
| 19 | + handler_editor(file = file, title = title, ..., name = name) |
| 20 | + } |
| 21 | + ) |
| 22 | + |
| 23 | + # Use Positron viewer to browse URLs |
| 24 | + set_override( |
| 25 | + "browser", |
| 26 | + function(url) { |
| 27 | + .ps.Call("ps_browse_url", as.character(url)) |
| 28 | + } |
| 29 | + ) |
| 30 | + |
| 31 | + # Register our password handler as the generic `askpass` option. |
| 32 | + # Same as RStudio, see `?rstudioapi::askForPassword` for rationale. |
| 33 | + set_override( |
| 34 | + "askpass", |
| 35 | + function(prompt) { |
| 36 | + .ps.ui.askForPassword(prompt) |
| 37 | + } |
| 38 | + ) |
| 39 | + |
| 40 | + set_override("connectionObserver", .ps.connection_observer()) |
| 41 | + |
| 42 | + # Declare the function name that `dev.new()` and `GECurrentDevice()` |
| 43 | + # go looking for to create a new graphics device when the current one |
| 44 | + # is `"null device"` and a new plot is requested |
| 45 | + set_override("device", ARK_GRAPHICS_DEVICE_NAME) |
| 46 | + |
| 47 | + # Avoid overwhelming the console |
| 48 | + set_override("max.print", 1000) |
| 49 | + |
| 50 | + # These options default to NULL in R, so a non-NULL value means the |
| 51 | + # user has set them. They are only set when the current value is NULL, |
| 52 | + # unless the user has also listed them in `positron.protected_options`, |
| 53 | + # which allows the user to preserve the default `NULL` value. |
| 54 | + |
| 55 | + # Enable HTML help |
| 56 | + set_default("help_type", "html") |
| 57 | + |
| 58 | + set_default("viewer", viewer_option_handler) |
| 59 | + |
| 60 | + # Show Shiny applications in the viewer |
| 61 | + set_default( |
| 62 | + "shiny.launch.browser", |
| 63 | + function(url) { |
| 64 | + .ps.ui.showUrl(url) |
| 65 | + } |
| 66 | + ) |
| 67 | + |
| 68 | + # Show Plumber apps in the viewer |
| 69 | + set_default( |
| 70 | + "plumber.docs.callback", |
| 71 | + function(url) { |
| 72 | + .ps.ui.showUrl(url) |
| 73 | + } |
| 74 | + ) |
| 75 | + |
| 76 | + # Show Profvis output in the viewer |
| 77 | + set_default( |
| 78 | + "profvis.print", |
| 79 | + function(x) { |
| 80 | + # Render the widget to a tag list to create standalone HTML output. |
| 81 | + # (htmltools is a Profvis dependency so it's guaranteed to be available) |
| 82 | + rendered <- htmltools::as.tags(x, standalone = TRUE) |
| 83 | + |
| 84 | + # Render the HTML content to a temporary file |
| 85 | + tmp_file <- htmltools::html_print(rendered, viewer = NULL) |
| 86 | + |
| 87 | + # Pass the file to the viewer |
| 88 | + .ps.Call("ps_html_viewer", tmp_file, "R Profile", -1L, "editor") |
| 89 | + } |
| 90 | + ) |
| 91 | +} |
| 92 | + |
| 93 | +is_protected <- function(name) { |
| 94 | + name %in% getOption("positron.protected_options", default = character()) |
| 95 | +} |
| 96 | + |
| 97 | +# Set an option unconditionally, unless listed in `positron.protected_options` |
| 98 | +set_override <- function(name, value) { |
| 99 | + if (is_protected(name)) { |
| 100 | + return(invisible()) |
| 101 | + } |
| 102 | + do.call(options, set_names(list(value), name)) |
| 103 | +} |
| 104 | + |
| 105 | +# Set an option only when currently `NULL`, unless listed in `positron.protected_options` |
| 106 | +set_default <- function(name, value) { |
| 107 | + if (is_protected(name)) { |
| 108 | + return(invisible()) |
| 109 | + } |
| 110 | + if (!is.null(getOption(name))) { |
| 111 | + return(invisible()) |
| 112 | + } |
| 113 | + do.call(options, set_names(list(value), name)) |
| 114 | +} |
0 commit comments