Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metadata #421

Merged
merged 5 commits into from
Jan 3, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export(text)
export(title)
export(view)
export(view_info)
export(whatDate)
export(whatVersion)
export(xlab)
export(xlim)
export(ylab)
Expand Down
26 changes: 25 additions & 1 deletion R/access-gsplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,28 @@ view_info <- function(object){
viewInfo$y.side.defined.by.user <- viewInfo$y %in% defined.sides

return(viewInfo)
}
}

#' Get gsplot object creation date.
#'
#' Get the date that this gsplot object was originally created.
#'
#' @param object a gsplot object
#' @return The date the gsplot object was created.
#' @export
whatDate <- function(object){
date_created <- object$metadata$created
return(date_created)
}

#' Get gsplot version used.
#'
#' Get the version number that was used to create this gsplot object.
#'
#' @param object a gsplot object
#' @return The gsplot version number.
#' @export
whatVersion <- function(object){
version_created <- object$metadata$gsplot.version
return(version_created)
}
12 changes: 10 additions & 2 deletions R/gsplot-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#' Used to change the class of inputs to "gsplot".
#'
#' @param x list
#' @param created vector of length one giving the date the gsplot object was created. Defaults to
#' using \code{Sys.Date()}. Output class matches that of the input.
#' @param gsplot.version vector of length one giving the version of the gsplot package used to create the
#' object. Defaults to calling \code{packageDescription()}. Output class matches that of the input.
#' @param config.file path to the file that will only be used for setting
#' par in this one gsplot object. If \code{NA} (default), par is set by the global options set by
#' loadConfig().
Expand All @@ -19,8 +23,12 @@ gsplot <- function(x = NULL, ...) UseMethod("gsplot")

#' @rdname gsplot
#' @export
gsplot.default <- function(...,config.file=NA, theme=NA, frame.plot=TRUE) {
object <- gsplot(list(global=list('config'=list(frame.plot=frame.plot,
gsplot.default <- function(..., created=Sys.Date(),
gsplot.version=packageDescription("gsplot", fields = "Version"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might use packageVersion("gsplot") here instead

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better yet is

packageVersion(getPackageName())

so we don't need to modify it if we change the name of the package.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried packageVersion("gsplot") at first, but it returns a list instead of a character string. packageDescription("gsplot", fields = "Version") returns a single string. To get the output of packageVersion into a single string seemed more complex than using packageDescription.

I will make the package reference generic per @jread-usgs's suggestion.

config.file=NA, theme=NA, frame.plot=TRUE) {
object <- gsplot(list(metadata=list(created=created,
gsplot.version=gsplot.version),
global=list('config'=list(frame.plot=frame.plot,
config.file=!is.na(config.file),
theme=!is.na(theme)))))

Expand Down
11 changes: 9 additions & 2 deletions man/gsplot.Rd

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

18 changes: 18 additions & 0 deletions man/whatDate.Rd

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

18 changes: 18 additions & 0 deletions man/whatVersion.Rd

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

13 changes: 13 additions & 0 deletions tests/testthat/test-access_gsplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,16 @@ test_that("summary ",{
points(x=3:10,y=4:11, side=c(1,2), log='xy')
expect_output(summary(usrDef),regexp = "2 views:")
})

test_that("users can access metadata", {
gs <- gsplot()
expect_equal(whatDate(gs), Sys.Date())

old.version <- "0.5.2"
gs2 <- gsplot(gsplot.version=old.version)
expect_equal(whatVersion(gs2), old.version)

old.date <- as.Date("2010-10-21")
gs3 <- gsplot(created=old.date)
expect_equal(whatDate(gs3), old.date)
})
3 changes: 2 additions & 1 deletion tests/testthat/tests-axis.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ test_that("axis",{
test_that("axis gsplot",{
gs = points(gsplot(mar=c(1,1,1,1)), c(-2,3), c(-1,5)) %>%
axis(3)
expect_true(all(names(gs) %in% c("side.1", "side.2", "side.3", "view.1.2", "global")))
expect_true(all(names(gs) %in% c("side.1", "side.2", "side.3",
"view.1.2", "global", "metadata")))

gs <- gsplot() %>%
lines(1:5, c(1,10,100,1000,10000), log="y", axes=FALSE) %>%
Expand Down