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

StringsAsFactors #58

Merged
merged 6 commits into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from all 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: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: git2rdata
Title: Store and Retrieve Data.frames in a Git Repository
Version: 0.2.0
Version: 0.2.1
Authors@R: c(
person(
"Thierry", "Onkelinx", role = c("aut", "cre"),
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
git2rdata 0.2.1 (2020-02-03)
=================================

### Bugfixes

* Explicitly use the `stringsAsFactors` of `data.frame()` in the examples and
unit tests if the dataframe contains characters. The upcoming change in
default value of `stringsAsFactors` requires this change. See
https://developer.r-project.org/Blog/public/2020/02/16/stringsasfactors/index.html

git2rdata 0.2.0 (2019-11-08)
=================================

Expand Down
9 changes: 7 additions & 2 deletions R/relabel.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
#' # Create a dataframe and store it as an optimized git2rdata object.
#' # Note that write_vc() uses optimization by default.
#' # Stage and commit the git2rdata object.
#' ds <- ds <- data.frame(a = c("a1", "a2"), b = c("b2", "b1"))
#' ds <- data.frame(
#' a = c("a1", "a2"),
#' b = c("b2", "b1"),
#' stringsAsFactors = TRUE
#' )
#' junk <- write_vc(ds, "relabel", repo, sorting = "b", stage = TRUE)
#' cm <- commit(repo, "initial commit")
#' # check that the workspace is clean
Expand All @@ -49,7 +53,8 @@
#' change <- data.frame(
#' factor = c("a", "a", "b"),
#' old = c("a3", "a1", "b2"),
#' new = c("c2", "c1", "b3")
#' new = c("c2", "c1", "b3"),
#' stringsAsFactors = TRUE
#' )
#' relabel("relabel", repo, change)
#' # check the changes
Expand Down
8 changes: 4 additions & 4 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
],
"issueTracker": "https://github.com/ropensci/git2rdata/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.2.0",
"version": "0.2.1",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
"version": "3.6.1",
"version": "3.6.2",
"url": "https://r-project.org"
},
"runtimePlatform": "R version 3.6.1 (2019-07-05)",
"runtimePlatform": "R version 3.6.2 (2019-12-12)",
"author": [
{
"@type": "Person",
Expand Down Expand Up @@ -203,7 +203,7 @@
],
"releaseNotes": "https://github.com/ropensci/git2rdata/blob/master/NEWS.md",
"readme": "https://github.com/ropensci/git2rdata/blob/master/README.md",
"fileSize": "341.663KB",
"fileSize": "341.883KB",
"contIntegration": [
"https://travis-ci.org/inbo/git2rdata",
"https://ci.appveyor.com/project/ThierryO/git2rdata/branch/master",
Expand Down
9 changes: 7 additions & 2 deletions man/relabel.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test_b_update.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test_that("updates to numeric", {
test_that("updates to character", {
write_vc(original, "character", root, sorting = "test_logical")
updated <- matrix("xyz", ncol = ncol(original), dimnames = dimnames(original))
updated <- as.data.frame(updated)
updated <- as.data.frame(updated, stringsAsFactor = FALSE)
expect_is(
suppressWarnings(
fn <- write_vc(updated, "character", root, strict = FALSE)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_d_relabel.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
context("relabel")
root <- tempfile("git2rdata-relabel")
dir.create(root)
ds <- data.frame(a = c("a1", "a2"), b = c("b2", "b1"))
ds <- data.frame(a = c("a1", "a2"), b = c("b2", "b1"), stringsAsFactors = TRUE)
write_vc(ds, "relabel", root, sorting = "b")
new_labels <- list(a = list(a2 = "a3"))
test_that("relabel handles a list of changes", {
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/test_e_empty_label.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ test_that("write_vc handles empty labels", {
})

test_that("relabel handles empty labels", {
change <- data.frame(factor = "var", old = "", new = "something")
change <- data.frame(
factor = "var", old = "", new = "something", stringsAsFactors = TRUE
)

# "" is first level
mydf <- data.frame(
Expand Down
23 changes: 16 additions & 7 deletions vignettes/version_control.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,18 @@ print_file("column_order.tsv", root, n = 5)
We take this even a bit further: what happens if new data arrives and we need an extra factor level?

```{r factor}
old <- data.frame(color = c("red", "blue"))
old <- data.frame(color = c("red", "blue"), stringsAsFactors = TRUE)
write_vc(old, "factor", root, sorting = "color")
print_file("factor.yml", root)
```

Let's add an observation with a new factor level. If we store the updated dataframe in a new file, we see that the indices are different. The factor level `"blue"` remains unchanged, but `"red"` becomes the third level and get index `3` instead of index `2`. This could lead to a large diff whereas the potential semantics (and thus the information content) are not changed.

```{r factor2}
updated <- data.frame(color = c("red", "green", "blue"))
updated <- data.frame(
color = c("red", "green", "blue"),
stringsAsFactors = TRUE
)
write_vc(updated, "factor2", root, sorting = "color")
print_file("factor2.yml", root)
```
Expand All @@ -239,12 +242,13 @@ print_file("factor.yml", root)
```

The next example removes the `"blue"` level and switches the order of the remaining levels.
Notice that the medatadata retains the existing indices.
Notice that the meta data retains the existing indices.
The order of the labels and indices reflects their new ordering.

```{r factor_deleted}
deleted <- data.frame(
color = factor(c("red", "green"), levels = c("red", "green")))
color = factor(c("red", "green"), levels = c("red", "green"))
)
write_vc(deleted, "factor", root, sorting = "color", strict = FALSE)
print_file("factor.yml", root)
```
Expand All @@ -253,7 +257,8 @@ Changing a factor to an ordered factor or _vice versa_ will also keep existing l

```{r factor_ordered}
ordered <- data.frame(
color = factor(c("red", "green"), levels = c("red", "green"), ordered = TRUE))
color = factor(c("red", "green"), levels = c("red", "green"), ordered = TRUE)
)
write_vc(ordered, "factor", root, sorting = "color", strict = FALSE)
print_file("factor.yml", root)
```
Expand Down Expand Up @@ -285,8 +290,12 @@ The dataframe format requires a `factor`, `old` and `new` variable with one row
write_vc(old, "relabel", root, sorting = "color")
relabel("relabel", root, change = list(color = c(red = "rood", blue = "blauw")))
print_file("relabel.yml", root)
relabel("relabel", root,
change = data.frame(factor = "color", old = "blauw", new = "blue"))
relabel(
"relabel", root,
change = data.frame(
factor = "color", old = "blauw", new = "blue", stringsAsFactors = TRUE
)
)
print_file("relabel.yml", root)
```

Expand Down