Skip to content

Commit

Permalink
Add support for bundled hexjson files
Browse files Browse the repository at this point in the history
  • Loading branch information
psychemedia committed Jun 30, 2017
1 parent f2e6a2d commit bb1c332
Show file tree
Hide file tree
Showing 9 changed files with 759 additions and 24 deletions.
10 changes: 8 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# Generated by roxygen2: fake comment so roxygen2 overwrites silently.
exportPattern("^[^\\.]")
# Generated by roxygen2: do not edit by hand

export(hexjsonbasefiles)
export(hexjsonwidget)
export(hexjsonwidgetOutput)
export(renderHexjsonwidget)
import(htmlwidgets)
import(jsonlite)
31 changes: 26 additions & 5 deletions R/hexjsonwidget.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,19 @@
#' hexjsonwidget(jj, col_hexfill='#bb3388')
#'
#' @export
hexjsonwidget <- function(jsondata=NA, jsonpath=NA, grid='off',
col_hexfill='', col_gridfill='', col_textfill='',
hexjsonwidget <- function(jsondata=NA, jsonpath=NA, jsonbase=NA,
grid='off', col_gridfill='',
col_hexfill='', col_textfill='',
width = NULL, height = NULL, elementId = NULL) {

if (identical(jsondata, NA)) {
if (identical(jsonpath, NA)) {
stop("Either 'jsondata' or 'jsonpath' must be set")
} else {
if (identical(jsonpath, NA) && identical(jsonbase, NA)) {
stop("Either 'jsondata', 'jsonpath' or 'jsonbase' must be set")
} else if (identical(jsonbase, NA)) {
jsondata=fromJSON(jsonpath)
} else {
#Adding data to packages - http://r-pkgs.had.co.nz/data.html
jsonpath=system.file("extdata", jsonbase, package = "hexjsonwidget")
jsondata=fromJSON(jsonpath)
}
}
Expand All @@ -65,6 +70,22 @@ hexjsonwidget <- function(jsondata=NA, jsonpath=NA, grid='off',
)
}

#' List base hexjson files
#'
#' \code{hexjsonbasefiles} lists the hexjson files available as part of the package.
#'
#' The files are stored in the \code{extdata/} directory in the package (\code{inst/extadata} in the original source code).
#'
#' To use one of the base files, pass the filename as the \code{jsonbase} parameter value when calling \code{hexjsonwidget}.
#'
#' @name hexjsonbasefiles
#'
#' @export
hexjsonbasefiles <- function(){
list.files(system.file("extdata", package = "hexjsonwidget"))
}


#' Shiny bindings for hexjsonwidget
#'
#' Output and render functions for using hexjsonwidget within Shiny
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ You can also add a `col` attribute to hexes in the hexJSON file. If no `col` is

If a hexJSON hex has a `label` attribute, this will be used to label the hex, otherwise the hex ID will be used as the label.

Several hexjson files are bundled as part of the package. List the available hexjson files using the command: `hexjsonbasefiles()`

You can generate a map based on one of the base files by passing the base hexjson filename as the value for the `jsonbase` parameter: `hexjsonwidget(jsonbase="example-grid.hexjson")`
655 changes: 655 additions & 0 deletions inst/extdata/constituencies.hexjson

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions inst/extdata/example-grid.hexjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"layout":"odd-r",
"hexes": {
"Q0R0":{"q":0,"r":0},
"Q1R1":{"q":1,"r":1},
"Q1R2":{"q":1,"r":2},
"Q2R3":{"q":2,"r":3}
}
}
21 changes: 21 additions & 0 deletions inst/extdata/example.hexjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"layout":"odd-r",
"hexes":{
"C0R0":{"q":0,"r":0},
"C1R0":{"q":1,"r":0},
"C2R0":{"q":2,"r":0},
"C3R0":{"q":3,"r":0},
"C0R1":{"q":0,"r":1},
"C1R1":{"q":1,"r":1},
"C2R1":{"q":2,"r":1},
"C3R1":{"q":3,"r":1},
"C0R2":{"q":0,"r":2},
"C1R2":{"q":1,"r":2},
"C2R2":{"q":2,"r":2},
"C3R2":{"q":3,"r":2},
"C0R3":{"q":0,"r":3},
"C1R3":{"q":1,"r":3},
"C2R3":{"q":2,"r":3},
"C3R3":{"q":3,"r":3}
}
}
16 changes: 16 additions & 0 deletions man/hexjsonbasefiles.Rd

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

8 changes: 4 additions & 4 deletions man/hexjsonwidget.Rd

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

30 changes: 17 additions & 13 deletions test/hexjsontest.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,39 @@ hexjsonwidget(jj)
```

```{r}
library(jsonlite)
library(hexjsonwidget)
hexjsonwidget(jsonpath='./example-grid.hexjson')
```

```{r}
library(jsonlite)
library(hexjsonwidget)
jj=fromJSON('./example-grid.hexjson')
hexjsonwidget(jj, grid='on', col_gridfill='#113355', col_hexfill='#FF0000')
hexjsonwidget(jsonpath='./example-grid.hexjson',
grid='on', col_gridfill='#113355', col_hexfill='#FF0000')
```


```{r}
library(jsonlite)
library(hexjsonwidget)
jj=fromJSON('./example-grid.hexjson')
hexjsonwidget(jj, grid='off', col_textfill="orange")
```


```{r}
library(jsonlite)
library(hexjsonwidget)
jj=fromJSON('./example-grid.hexjson')
hexjsonwidget(jj, col_hexfill='#bb3388')
```

```{r}
jj=fromJSON('./example-grid.hexjson')
hexjsonwidget(jj,grid="on", col_hexfill='#bb3388', col_textfill="white")
```


```{r}
#List available data files
hexjsonbasefiles()
```

```{r}
#Use a base datafile
hexjsonwidget(jsonbase="example-grid.hexjson")
```


## Screenshot test
Expand Down

0 comments on commit bb1c332

Please sign in to comment.