Skip to content

Commit

Permalink
add: custom but really basic yaml writer with UTF-8 support for #56
Browse files Browse the repository at this point in the history
  • Loading branch information
daroczig committed Jan 16, 2013
1 parent ce96e9e commit 112f48b
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 10 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ Collate:
'template.R'
'utils.R'
'tpl-check.R'
'yaml.R'
27 changes: 17 additions & 10 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
S3method(as.character,rp.inputs)
S3method(as.character,rp.meta)
S3method(print,rapport)
S3method(print,rp.info)
S3method(print,rp.inputs)
S3method(print,rp.meta)
export("rp.label<-")
export(adj.rle)
export(alike.integer)
export(capitalise)
export(catn)
export(fml)
export(get.tags)
export(htest)
export(htest.short)
export(htest)
export(is.boolean)
export(is.empty)
export(is.heading)
Expand All @@ -25,11 +19,11 @@ export(kurtosis)
export(lambda.test)
export(messagef)
export(pct)
export(rapport)
export(rapport.docx)
export(rapport.html)
export(rapport.odt)
export(rapport.pdf)
export(rapport)
export(rp.barplot)
export(rp.boxplot)
export(rp.densityplot)
Expand Down Expand Up @@ -63,6 +57,13 @@ export(rp.var)
export(skewness)
export(stopf)
export(tocamel)
export(toYAML.character)
export(toYAML.default)
export(toYAML.list)
export(toYAML.logical)
export(toYAML.NULL)
export(toYAML.numeric)
export(toYAML)
export(tpl.body)
export(tpl.check)
export(tpl.example)
Expand All @@ -71,15 +72,21 @@ export(tpl.info)
export(tpl.inputs)
export(tpl.list)
export(tpl.meta)
export(tpl.paths)
export(tpl.paths.add)
export(tpl.paths.remove)
export(tpl.paths.reset)
export(tpl.paths)
export(tpl.rerun)
export(trim.space)
export(vgsub)
export(warningf)
importFrom(reshape,cast)
importFrom(reshape,melt)
importFrom(reshape,melt.data.frame)
importFrom(reshape,melt.table)
importFrom(reshape,melt)
S3method(as.character,rp.inputs)
S3method(as.character,rp.meta)
S3method(print,rapport)
S3method(print,rp.info)
S3method(print,rp.inputs)
S3method(print,rp.meta)
73 changes: 73 additions & 0 deletions R/yaml.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#' Convert to YAML
#'
#' A simple method to convert various (err: a few) R objects to YAML syntax.
#' @param o R object
#' @param indent.level indentation level
#' @return string
#' @export
toYAML <- function(o, indent.level = 0)
UseMethod('toYAML', o)


#' @export
toYAML.list <- function(o, indent.level = 0) {

ol <- length(o)
res <- paste(sapply(1:ol, function(i) {
res <- ''
if (!is.null(names(o[i])) && (names(o[i]) != '')) {
res <- paste0(res, paste(rep(' ', indent.level * 2), collapse = ''), names(o[i]), ':')
if (length(o[[i]]) <= 1)
res <- paste(res, toYAML(o[[i]], indent.level + 1), '\n')
else
res <- paste0(res, '\n', toYAML(o[[i]], indent.level + 1))
} else {
res <- paste0(res, paste(rep(' ', max(indent.level - 1, 0) * 2), collapse = ''), '- ')
if (length(o[[i]]) <= 1)
res <- paste0(res, paste(rep(' ', indent.level * 2), collapse = ''), o[i], '\n')
else
res <- paste0(res, toYAML(o[[i]][1], (indent.level - 1)), toYAML(o[[i]][2:length(o[[i]])], indent.level))
}
res
}), collapse = '')

res

}


#' @export
toYAML.numeric <- function(o, indent.level = 0) {

if (length(o) == 0)
return('[]')

if (length(o) == 1)
return(o)

paste0(paste(sapply(o, function(x) paste0(paste(rep(' ', (indent.level - 1) * 2), collapse = ''), '- ', x)), collapse = '\n'), '\n')

}


#' @export
toYAML.character <- toYAML.numeric


#' @export
toYAML.NULL <- function(o, indent.level = 0)
return('~')


#' @export
toYAML.logical <- function(o, indent.level = 0)
toYAML(as.character(o, indent.level))


#' @export
toYAML.default <- function(o, indent.level = 0) {

class(o) <- 'list'
toYAML(o, indent.level)

}
19 changes: 19 additions & 0 deletions man/toYAML.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
\name{toYAML}
\alias{toYAML}
\title{Convert to YAML}
\usage{
toYAML(o, indent.level = 0)
}
\arguments{
\item{o}{R object}

\item{indent.level}{indentation level}
}
\value{
string
}
\description{
A simple method to convert various (err: a few) R objects
to YAML syntax.
}

0 comments on commit 112f48b

Please sign in to comment.