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

Keeps class when add row to empty tibble #177

Merged
merged 9 commits into from Oct 9, 2016

Conversation

Projects
None yet
3 participants
@anhqle
Contributor

anhqle commented Oct 2, 2016

Fixes #171

When add_row to an empty tibble, all column classes were converted to logical.

We fix this by checking whether we're adding to an empty tibble. If yes, we coerce column classes back to the original.

Thoughts and comments are greatly appreciated as always!

Resolves #171
When add_row to an empty tibble, all column classes were converted to logical.

We fix this by checking whether we're adding to an empty tibble. If yes, we coerce column classes back to the original.
@codecov-io

This comment has been minimized.

codecov-io commented Oct 2, 2016

Current coverage is 99.59% (diff: 100%)

Merging #177 into master will increase coverage by <.01%

@@             master       #177   diff @@
==========================================
  Files            16         16          
  Lines           731        738     +7   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits            728        735     +7   
  Misses            3          3          
  Partials          0          0          

Powered by Codecov. Last update bcfaca0...3f658b1

@anhqle anhqle changed the title from Resolves #171 to Keeps class when add row to empty tibble Oct 2, 2016

krlmlr added some commits Oct 3, 2016

versioned dependency
- Now explicitly stating minimum Rcpp version 0.12.3.
@krlmlr

Needs some refactoring.

R/add.R Outdated
@@ -57,6 +57,17 @@ add_row <- function(.data, ..., .before = NULL, .after = NULL) {
out <- rbind(.data[indexes, ], df, .data[-indexes, ])
}

# If .data is empty, coerce the out column class to .data class
if (nrow(.data) == 0) {
for (i in 1:ncol(out)) {

This comment has been minimized.

@krlmlr

krlmlr Oct 3, 2016

Member

If we go that route, I'd prefer an rbind_xxx() function that takes care of the details. For a 0-row data frame you can still query the first row, this gets you a row of typed NAs:

iris[0,][1,] %>% lapply(class)

Then, in this specific corner case, we might be able to bind a row of NA values with the new data, and remove the first row again.

@@ -1,4 +1,4 @@
# This file was generated by Rcpp::compileAttributes
# Generated by using Rcpp::compileAttributes() -> do not edit by hand

This comment has been minimized.

@krlmlr

krlmlr Oct 3, 2016

Member

I've updated Rcpp in master, please rebase.

@krlmlr

This comment has been minimized.

Member

krlmlr commented Oct 3, 2016

Thanks so far.

R/add.R Outdated
}
}
}
out <- rbind_safe(.data, df, pos)

This comment has been minimized.

@anhqle

anhqle Oct 9, 2016

Contributor

rbind_safe is not the best name -- suggestions most welcome.

This comment has been minimized.

@krlmlr

krlmlr Oct 9, 2016

Member

Thanks. Looks like you could inline rbind_position() so that all cases are handled in one function, perhaps rbind_at() instead of rbind_safe()?

anhqle added some commits Oct 2, 2016

Resolves #171
When add_row to an empty tibble, all column classes were converted to logical.

We fix this by checking whether we're adding to an empty tibble. If yes, we coerce column classes back to the original.

@krlmlr krlmlr merged commit 7c4a712 into tidyverse:master Oct 9, 2016

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
@krlmlr

This comment has been minimized.

Member

krlmlr commented Oct 9, 2016

Awesome, thanks!

krlmlr added a commit that referenced this pull request Nov 30, 2016

Merge tag 'v1.2-13'
- New `frame_matrix()` (#140, #168, @LaDilettante).
- The `max.print` option is ignored when printing a tibble (#194, #195, @t-kalinowski).
- Fix typo in `obj_sum` documentation (#193, @etiennebr).
- Keep column classes when adding row to empty tibble (#171, #177, @LaDilettante).
- Now explicitly stating minimum Rcpp version 0.12.3.

krlmlr added a commit that referenced this pull request Apr 1, 2017

Merge tag 'v1.3.0'
Bug fixes
=========

- Time series matrices (objects of class `mts` and `ts`) are now supported in `as_tibble()` (#184).
- The `all_equal()` function (called by `all.equal.tbl_df()`) now forwards to `dplyr` and fails with a helpful message if not installed. Data frames with list columns cannot be compared anymore, and differences in the declared class (`data.frame` vs. `tbl_df`) are ignored. The `all.equal.tbl_df()` method gives a warning and forwards to `NextMethod()` if `dplyr` is not installed; call `all.equal(as.data.frame(...), ...)` to avoid the warning. This ensures consistent behavior of this function, regardless if `dplyr` is loaded or not (#198).

Interface changes
=================

- Now requiring R 3.1.0 instead of R 3.1.3 (#189).
- Add `as.tibble()` as an alias to `as_tibble()` (#160, @LaDilettante).
- New `frame_matrix()`, similar to `frame_data()` but for matrices (#140, #168, @LaDilettante).
- New `deframe()` as reverse operation to `enframe()` (#146, #214).
- Removed unused dependency on `assertthat`.

Features
========

General
-------

- Keep column classes when adding row to empty tibble (#171, #177, @LaDilettante).
- Singular and plural variants for error messages that mention a list of objects (#116, #138, @LaDilettante).
- `add_column()` can add columns of length 1 (#162, #164, @LaDilettante).

Input validation
----------------

- An attempt to read or update a missing column now throws a clearer warning (#199).
- An attempt to call `add_row()` for a grouped data frame results in a helpful error message (#179).

Printing
--------

- Render Unicode multiplication sign as `x` if it cannot be represented in the current locale (#192, @ncarchedi).
- Backtick `NA` names in printing (#206, #207, @jennybc).
- `glimpse()` now uses `type_sum()` also for S3 objects (#185, #186, @holstius).
- The `max.print` option is ignored when printing a tibble (#194, #195, @t-kalinowski).

Documentation
=============

- Fix typo in `obj_sum` documentation (#193, @etiennebr).
- Reword documentation for `tribble()` (#191, @kwstat).
- Now explicitly stating minimum Rcpp version 0.12.3.

Internal
========

- Using registration of native routines.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment