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

Fix issue with read_dta crashing R #619

Merged
merged 4 commits into from
Jul 26, 2021
Merged

Conversation

gorcha
Copy link
Member

@gorcha gorcha commented Jul 25, 2021

The updates for cpp11 reintroduced the bug in #79 when read_dta is used on a file that has empty values in an StrL variable (#600, #608). This PR restores the old behaviour and returns an NA for these observations.

@hadley, the StrL section of the dta spec says that a NULL (i.e. no StrL address) should be interpreted as the empty string "" rather than a missing value, so NA_STRING might not actually be the most appropriate value here (although it would be consistent with previous versions of haven).
What do you think?

@gorcha
Copy link
Member Author

gorcha commented Jul 25, 2021

Woops, missed one, also fixes #594

@hadley
Copy link
Member

hadley commented Jul 25, 2021

Yeah, lets make it "". Thanks for looking into this!

@gorcha
Copy link
Member Author

gorcha commented Jul 26, 2021

No worries, done! I'm not much of a C person so it feels super hacky, but it works 😛

src/DfReader.cpp Outdated

if (readstat_value_is_tagged_missing(value)) {
col[obs_index] = NA_STRING;
} else if (!user_na_ && readstat_value_is_defined_missing(value, variable)) {
col[obs_index] = NA_STRING;
} else if (readstat_value_is_system_missing(value)) {
col[obs_index] = NA_STRING;
} else if (str_value == NULL) {
const char empty_string[1] = { '\0' };
col[obs_index] = cpp11::r_string(empty_string);
Copy link
Member

Choose a reason for hiding this comment

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

Can't you just do cpp11::r_string("") ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Haha yep! Too much time in R treating "" and '' as equivalent 🤦

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks!

@hadley hadley merged commit 078dd6b into tidyverse:master Jul 26, 2021
@hadley
Copy link
Member

hadley commented Jul 26, 2021

Thanks for working on this! Will prepare a release next week some time.

@gorcha gorcha deleted the fix-stata-crash branch July 26, 2021 04:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants