Skip to content

Commit

Permalink
make jsonedit and reactjson consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
timelyportfolio committed Oct 31, 2016
1 parent d2c06a2 commit fa1efab
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 27 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ Suggests:
jsonlite,
miniUI,
rstudioapi
Enhances:
reactR
RoxygenNote: 5.0.1
19 changes: 1 addition & 18 deletions R/jsonedit.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,7 @@ jsonedit <- function(
, elementId = NULL
) {

# to avoid toJSON keep_vec_names warnings
# with named vectors
# convert named vectors to list
# see https://github.com/timelyportfolio/listviewer/issues/10
named_vec2list <- function(listx){
if(
!inherits(listx,"list") &&
is.null(dim(listx)) &&
!is.null(names(listx))
){
listx <- as.list(listx)
}
return(listx)
}

if(inherits(listdata,"list")){
listdata <- rapply(listdata,named_vec2list,how="list")
}
listdata <- list_proper_form(listdata)

# forward options using x
x = list(
Expand Down
16 changes: 13 additions & 3 deletions R/reactjson.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
#' Edit R Data with 'react-json'
#'
#' @param listdata \code{list} or \code{String} data to view. Although designed for \code{lists}, \code{listdata} can
#' be any data source that can be rendered into \code{JSON} with \code{jsonlite}. Alternately,
#' \code{listdata} could be a \code{String} of valid \code{JSON}. This might be helpful
#' when dealing with an API response.
#' @param width integer in pixels defining the width of the \code{div} container.
#' @param height integer in pixels defining the height of the \code{div} container.
#' @param elementId character to specify valid \code{CSS} id of the
#' htmlwidget for special situations in which you want a non-random
#' identifier.
#'
#' @import htmlwidgets
#'
#' @export
#' @example inst/examples/examples_reactjson.R
reactjson <- function(
json = list(),
listdata = list(),
width = NULL, height = NULL, elementId = NULL
) {

if(!require(reactR)){
if(!requireNamespace("reactR")){
stop("please devtools::install_github('timelyportfolio/reactR')", call.=FALSE)
}

# forward options using x
x = list(
json = json
data = listdata
)

# create widget
Expand Down
24 changes: 24 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#' @keywords internal

named_vec2list <- function(listx){
# to avoid toJSON keep_vec_names warnings
# with named vectors
# convert named vectors to list
# see https://github.com/timelyportfolio/listviewer/issues/10
if(
!inherits(listx,"list") &&
is.null(dim(listx)) &&
!is.null(names(listx))
){
listx <- as.list(listx)
}
return(listx)
}

#' @keywords internal
list_proper_form <- function(listdata){
if(inherits(listdata,"list")){
listdata <- rapply(listdata,named_vec2list,how="list")
}
}

6 changes: 4 additions & 2 deletions inst/examples/examples_reactjson.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# requires dev version of listviewer
# devtools::install_github("timelyportfolio/listviewer@feature/react-json")
\dontrun{

library(listviewer)

# use reactR for React dependencies
Expand All @@ -25,3 +25,5 @@ shinyApp(
)
}
)

}
5 changes: 4 additions & 1 deletion inst/htmlwidgets/reactjson.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ HTMLWidgets.widget({
ReactDOM.render(
React.createElement(
Json,
{ value: x.json, onChange: logChange }
{
value: (typeof(x.data)==="string") ? JSON.parse(x.data) : x.data,
onChange: logChange
}
),
document.body
);
Expand Down
23 changes: 20 additions & 3 deletions man/reactjson.Rd

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

0 comments on commit fa1efab

Please sign in to comment.