Skip to content

Commit 201611c

Browse files
Merge pull request #12 from timelyportfolio/feature/gadget
Feature - shiny gadget
2 parents 7449b9e + bce1a13 commit 201611c

File tree

7 files changed

+91
-3
lines changed

7 files changed

+91
-3
lines changed

DESCRIPTION

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: listviewer
22
Type: Package
33
Title: 'htmlwidget' for Interactive Views of R Lists
4-
Version: 1.0
5-
Date: 2016-06-14
4+
Version: 1.1
5+
Date: 2016-06-15
66
Authors@R: c(
77
person("Jos", "de Jong", role = c("aut", "cph"), comment = "jsoneditor.js library in htmlwidgets/jsoneditor, http://github.com/josdejong/jsoneditor/" ),
88
person("Kenton","Russell", role = c("aut","cre"), comment = "R interface" , email = "[email protected]" )
@@ -16,5 +16,5 @@ LazyData: TRUE
1616
URL: https://github.com/timelyportfolio/listviewer
1717
BugReports: https://github.com/timelyportfolio/listviewer/issues
1818
Imports: htmlwidgets
19-
Suggests: htmltools, jsonlite, shiny
19+
Suggests: htmltools, jsonlite, miniUI, shiny
2020
RoxygenNote: 5.0.1

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
export(jsonedit)
44
export(jsoneditOutput)
5+
export(jsonedit_gadget)
56
export(renderJsonedit)
67
import(htmlwidgets)

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# listviewer 1.1
2+
3+
* add shiny gadget functionality with `jsonedit_gadget`
4+
15
# listviewer 1.0
26

37
* Submit to CRAN.

R/jsonedit_gadget.R

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}

inst/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gs_wsfeed.rds

inst/examples/examples_gadget.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
library(listviewer)
2+
3+
jsonedit_gadget(
4+
structure(
5+
as.list(1:4),
6+
names=letters[1:4]
7+
)
8+
)

man/jsonedit_gadget.Rd

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)