|
| 1 | +#' Shiny Gadget for 'jsonedit' |
| 2 | +#' |
| 3 | +#' Provides a \href{http://shiny.rstudio.com/articles/gadgets.html}{Shiny gadget} |
| 4 | +#' interface for \code{jsonedit} to interactively edit and return the |
| 5 | +#' changes for use in R. |
| 6 | +#' |
| 7 | +#' @param height,width any valid \code{CSS} size unit for the |
| 8 | +#' height and width of the gadget |
| 9 | +#' @param ... arguments for \code{\link{jsonedit}} |
| 10 | +#' |
| 11 | +#' @example ./inst/examples/examples_gadget.R |
| 12 | +#' @export |
| 13 | +jsonedit_gadget <- function(..., height = NULL, width = NULL) { |
| 14 | + # modeled after chemdoodle gadget |
| 15 | + # https://github.com/zachcp/chemdoodle/blob/master/R/chemdoodle_sketcher_gadgets.R |
| 16 | + stopifnot(requireNamespace("miniUI"), requireNamespace("shiny")) |
| 17 | + ui <- miniUI::miniPage( |
| 18 | + miniUI::miniContentPanel(jsonedit(...), height=NULL, width=NULL), |
| 19 | + |
| 20 | + miniUI::gadgetTitleBar("Edit Data", right = miniUI::miniTitleBarButton("done", "Done", primary = TRUE)), |
| 21 | + |
| 22 | + htmltools::tags$script(' |
| 23 | +document.getElementById("done").onclick = function() { |
| 24 | + var listdata = JSON.parse( |
| 25 | + HTMLWidgets.find(".jsonedit").editor.getText() |
| 26 | + ); |
| 27 | + Shiny.onInputChange("jsoneditordata", listdata); |
| 28 | +}; |
| 29 | +' |
| 30 | + ) |
| 31 | + ) |
| 32 | + |
| 33 | + server <- function(input, output, session) { |
| 34 | + shiny::observeEvent(input$done, { shiny::stopApp(input$jsoneditordata) }) |
| 35 | + shiny::observeEvent(input$cancel, { shiny::stopApp (NULL) }) |
| 36 | + } |
| 37 | + |
| 38 | + shiny::runGadget( |
| 39 | + ui, |
| 40 | + server, |
| 41 | + viewer = shiny::dialogViewer("View and Edit Data"), |
| 42 | + stopOnCancel = FALSE |
| 43 | + ) |
| 44 | +} |
0 commit comments