Skip to content

Commit

Permalink
first version of shiny gadget interface
Browse files Browse the repository at this point in the history
  • Loading branch information
timelyportfolio committed Jun 15, 2016
1 parent 7449b9e commit e909ec2
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ LazyData: TRUE
URL: https://github.com/timelyportfolio/listviewer
BugReports: https://github.com/timelyportfolio/listviewer/issues
Imports: htmlwidgets
Suggests: htmltools, jsonlite, shiny
Suggests: htmltools, jsonlite, miniUI, shiny
RoxygenNote: 5.0.1
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

export(jsonedit)
export(jsoneditOutput)
export(jsonedit_gadget)
export(renderJsonedit)
import(htmlwidgets)
44 changes: 44 additions & 0 deletions R/jsonedit_gadget.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#' Shiny Gadget for 'jsonedit'
#'
#' Provides a \href{http://shiny.rstudio.com/articles/gadgets.html}{Shiny gadget}
#' interface for \code{jsonedit} to interactively edit and return the
#' changes for use in R.
#'
#' @param height,width any valid \code{CSS} size unit for the
#' height and width of the gadget
#' @param ... arguments for \code{\link{jsonedit}}
#'
#' @example ./inst/examples/examples_gadget.R
#' @export
jsonedit_gadget <- function(..., height = NULL, width = NULL) {
# modeled after chemdoodle gadget
# https://github.com/zachcp/chemdoodle/blob/master/R/chemdoodle_sketcher_gadgets.R
stopifnot(requireNamespace("miniUI"), requireNamespace("shiny"))
ui <- miniUI::miniPage(
miniUI::miniContentPanel(jsonedit(...), height=NULL, width=NULL),

miniUI::gadgetTitleBar("Edit Data", right = miniUI::miniTitleBarButton("done", "Done", primary = TRUE)),

htmltools::tags$script('
document.getElementById("done").onclick = function() {
var listdata = JSON.parse(
HTMLWidgets.find(".jsonedit").editor.getText()
);
Shiny.onInputChange("jsoneditordata", listdata);
};
'
)
)

server <- function(input, output, session) {
shiny::observeEvent(input$done, { shiny::stopApp(input$jsoneditordata) })
shiny::observeEvent(input$cancel, { shiny::stopApp (NULL) })
}

shiny::runGadget(
ui,
server,
viewer = shiny::dialogViewer("View and Edit Data"),
stopOnCancel = FALSE
)
}
1 change: 1 addition & 0 deletions inst/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gs_wsfeed.rds
8 changes: 8 additions & 0 deletions inst/examples/examples_gadget.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
library(listviewer)

jsonedit_gadget(
structure(
as.list(1:4),
names=letters[1:4]
)
)
30 changes: 30 additions & 0 deletions man/jsonedit_gadget.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e909ec2

Please sign in to comment.