Debian Codegraph @

R/addins.R

raw
1
2 renv_addins_embed_ui <- function() {
3
4 miniUI::miniPage(
5 miniUI::gadgetTitleBar("Embed a Lockfile"),
6 miniUI::miniContentPanel(
7 shiny::verticalLayout(
8 shiny::fileInput(
9 inputId = "lockfile",
10 label = "Lockfile path:",
11 placeholder = "(Use default)"
12 )
13 )
14 )
15 )
16
17 }
18
19 renv_addins_embed_server <- function(input, output, session) {
20
21 shiny::observeEvent(input$done, {
22
23 # notify the user that we're working now
24 progress <- shiny::Progress$new(
25 session = shiny::getDefaultReactiveDomain(),
26 style = "notification"
27 )
28
29 progress$set(message = "Embedding lockfile...")
30
31 # get editor context
32 context <- rstudioapi::getSourceEditorContext()
33
34 # validate we have a path
35 path <- context$path
36 if (!nzchar(path))
37 stop("cannot embed lockfile into an unsaved file", call. = FALSE)
38
39 # get project path
40 project <- rstudioapi::getActiveProject()
41
42 # read lockfile
43 lockfile <- input$lockfile
44 if (!is.null(lockfile))
45 lockfile <- renv_lockfile_read(file = lockfile$datapath)
46
47 # save document and run embed
48 rstudioapi::documentSave(id = context$id)
49 embed(path = path, lockfile = lockfile, project = project)
50
51 # stop app
52 invisible(shiny::stopApp())
53
54 })
55
56 }
57
58 renv_addins_embed <- function() {
59
60 # first, check that shiny and miniUI are available
61 for (package in c("miniUI", "rstudioapi", "shiny")) {
62 if (!requireNamespace(package, quietly = TRUE)) {
63 fmt <- "required package '%s' is not available"
64 stopf(fmt, package)
65 }
66 }
67
68 # ask the user to save the document first if necessary
69 context <- rstudioapi::getSourceEditorContext()
70 if (!nzchar(context$path))
71 stop("this addin cannot be run with an unsaved document")
72
73 # okay, we can run the addin
74 shiny::runGadget(
75 app = renv_addins_embed_ui(),
76 server = renv_addins_embed_server,
77 viewer = shiny::dialogViewer(
78 dialogName = "Embed Lockfile",
79 width = 400,
80 height = 200
81 )
82 )
83 }
84