Skip to content

Commit

Permalink
feat(R): Add JSON and data.frame conversion functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Jul 27, 2019
1 parent bcf98cb commit 8d1176b
Show file tree
Hide file tree
Showing 20 changed files with 2,732 additions and 75 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ __pycache__
.tox
*.out.*
/types.ts
.Rproj.user
5 changes: 3 additions & 2 deletions r/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.Rproj.user
/man
/stencilaschema_*.tar.gz
/stencilaschema.Rcheck
/stencila_*
/*.Rcheck
/.Rhistory
1 change: 1 addition & 0 deletions r/.lintr
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
linters: with_defaults(line_length_linter(120), object_usage_linter=NULL, object_name_linter(c("snake_case", "UpperCamelCase")))
exclusions: list("R/types.R")
15 changes: 10 additions & 5 deletions r/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
Package: stencilaschema
Package: stencila
Type: Package
Title: Stencila schema
Description: Stencila schema.
Title: Stencila
Description: Stencila.
Authors@R: c(person("Nokome", "Bentley", email = "nokome@stenci.la", role = c("aut", "cre")))
URL: https://github.com/stencila/schema#readme
BugReports: https://github.com/stencila/schema/issues
License: Apache-2.0
License: MIT + file LICENSE
Version: 0.0.1
Encoding: UTF-8
Imports:
jsonlite
Suggests:
Expand All @@ -17,4 +18,8 @@ Suggests:
testthat
RoxygenNote: 6.1.1.9000
Collate:
'util.R'
'stencila.R'
'typing.R'
'types.R'
'datatable.R'
'json.R'
19 changes: 19 additions & 0 deletions r/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2019 Stencila and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
13 changes: 11 additions & 2 deletions r/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@ setup:
Rscript -e "install.packages('devtools')"
Rscript -e "devtools::install_github(c('jimhester/lintr', 'klutometis/roxygen', 'r-lib/covr', 'r-lib/testthat'))"

regen:
npm run build:r

lint:
Rscript -e 'lintr::lint_package()'

test:
Rscript -e 'devtools::test()'

autotest:
Rscript -e 'testthat::auto_test_package()'

cover:
Rscript -e 'devtools::document()'
Rscript -e 'covr::package_coverage()'

check:
Rscript -e 'devtools::check(document = FALSE)'

build:
R CMD build . && R CMD check *.tar.gz
Rscript -e 'devtools::document(); warnings()'
Rscript -e 'devtools::build()'

docs:
Rscript -e 'devtools::document()'
Expand Down
52 changes: 52 additions & 0 deletions r/NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,2 +1,54 @@
# Generated by roxygen2: do not edit by hand

export(Article)
export(AudioObject)
export(BlockContent)
export(Brand)
export(Code)
export(CodeBlock)
export(CodeChunk)
export(CodeExpr)
export(Collection)
export(ContactPoint)
export(CreativeWork)
export(Datatable)
export(DatatableColumn)
export(DatatableColumnSchema)
export(Delete)
export(Emphasis)
export(Entity)
export(Environment)
export(Heading)
export(ImageObject)
export(Include)
export(InlineContent)
export(Link)
export(List)
export(ListItem)
export(Mark)
export(MediaObject)
export(Mount)
export(Node)
export(Organization)
export(Paragraph)
export(Person)
export(Product)
export(Quote)
export(QuoteBlock)
export(ResourceParameters)
export(SoftwareApplication)
export(SoftwareSession)
export(SoftwareSourceCode)
export(Strong)
export(Subscript)
export(Superscript)
export(Table)
export(TableCell)
export(TableRow)
export(ThematicBreak)
export(Thing)
export(VideoObject)
export(from_dataframe)
export(from_json)
export(to_dataframe)
export(to_json)
35 changes: 35 additions & 0 deletions r/R/datatable.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

#' Create a \code{\link{Datatable}} from a \code{data.frame}
#'
#' @name from_dataframe
#' @aliases as.Datatable.data.frame
#' @export
from_dataframe <- function(df){
Datatable(
columns = lapply(colnames(df), function(colname) {
values <- df[[colname]]
DatatableColumn(
name = colname,
schema = DatatableColumnSchema(
items = list(
type = node_type(values)
)
),
values = values
)
})
)
}

as.Datatable.data.frame <- from_dataframe

#' Create a \code{data.frame} from a \code{\link{Datatable}}
#'
#' @name to_dataframe
#' @aliases as.data.frame.Datatable
#' @export
to_dataframe <- function(df){
# TODO: Implement it!
}

as.data.frame.Datatable <- to_dataframe
31 changes: 31 additions & 0 deletions r/R/json.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#' Convert a node to JSON
#'
#' @param node The schema node to convert
#' @param pretty Should the JSON be pretyy printed (indented)
#' @export
to_json <- function(node, pretty = FALSE) {
if (inherits(node, "Entity")) {
node <- c(
list(type = jsonlite::unbox(last_class(node))),
node
)
}
as.character(
jsonlite::toJSON(node, null = "null", na = "null", pretty = pretty)
)
}

#' Create a node from JSON
#'
#' @param json The JSON to parse
#' @export
from_json <- function(json) {
obj <- jsonlite::fromJSON(json, simplifyVector = FALSE)
if (is.list(obj) && !is.null(obj$type)) {
type <- obj$type
obj$type <- NULL
do.call(type, obj)
} else {
obj
}
}
11 changes: 11 additions & 0 deletions r/R/stencila.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#' A R package for Stencila executable documents.
#'
#' @section Types
#'
#' @section Datatable
#'
#' @section JSON
#'
#' @docType package
#' @name stencila
NULL

0 comments on commit 8d1176b

Please sign in to comment.