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 new whitespace-delimited example file and update examples for read_table() and melt_*() #1354

Merged
merged 11 commits into from
Jan 25, 2022

Conversation

sbearrows
Copy link
Contributor

@sbearrows sbearrows commented Jan 22, 2022

Closes #1333

Copy link
Member

@jennybc jennybc left a comment

Choose a reason for hiding this comment

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

I made a few inline comments.

I recommend updating any melt_*() function to wherever this ends up. Yeah those functions are slated to live elsewhere, but it's also not too hard to make sure the example code is sensible in the meantime.

I prefer writeLines(read_lines(...)) over cat(read_lines(...)). This is already used in some places.

inst/extdata/massey-rating.txt Outdated Show resolved Hide resolved
R/read_table.R Outdated Show resolved Hide resolved
inst/extdata/whitespace-sample.txt Outdated Show resolved Hide resolved
@jennybc
Copy link
Member

jennybc commented Jan 22, 2022

Nudge me to help make sense of the snapshot test failure. We'll probably want to resolve that first, in main, then rebase this branch, to keep separate matters separate.

inst/extdata/massey-rating.txt Outdated Show resolved Hide resolved
R/melt_table.R Outdated
Comment on lines 28 to 30
#' ws <- readr_example("whitespace-sample.txt")
#' writeLines(read_file(ws))
#' melt_table(ws)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#' ws <- readr_example("whitespace-sample.txt")
#' writeLines(read_file(ws))
#' melt_table(ws)
#' fwf <- readr_example("fwf-sample.txt")
#' writeLines(read_lines(fwf))
#' melt_table(fwf)
#'
#' ws <- readr_example("whitespace-sample.txt")
#' writeLines(read_lines(ws))
#' melt_table2(ws)

Copy link
Member

@jennybc jennybc Jan 24, 2022

Choose a reason for hiding this comment

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

Remember the overall goal is to demonstrate readr functions with an example dataset that shows the most suitable input for the function. The discovery of #1333 is that these examples were originally written when readr behaved differently. Then the readr functions changed, but the examples didn't, which left behind some rather nonsensical example code.

The melt*() functions are designed to return output where "each row represents a single token".

I think melt_table() is best paired with fwf-sample.txt. melt_table2() makes much more sense for whitespace-sample.txt.

Also, from playing around with this, I think read_lines() is generally what we want (not read_file()).

library(readr)

fwf <- readr_example("fwf-sample.txt")
writeLines(read_lines(fwf))
#> John Smith          WA        418-Y11-4111
#> Mary Hartford       CA        319-Z19-4341
#> Evan Nolan          IL        219-532-c301
melt_table(fwf)
#> Warning: `melt_table()` was deprecated in readr 2.0.0.
#> Please use `meltr::melt_table()` instead
#> # A tibble: 12 × 4
#>      row   col data_type value       
#>    <dbl> <dbl> <chr>     <chr>       
#>  1     1     1 character John        
#>  2     1     2 character Smith       
#>  3     1     3 character WA          
#>  4     1     4 character 418-Y11-4111
#>  5     2     1 character Mary        
#>  6     2     2 character Hartford    
#>  7     2     3 character CA          
#>  8     2     4 character 319-Z19-4341
#>  9     3     1 character Evan        
#> 10     3     2 character Nolan       
#> 11     3     3 character IL          
#> 12     3     4 character 219-532-c301

ws <- readr_example("whitespace-sample.txt")
writeLines(read_lines(ws))
#> first last state phone
#> John Smith WA 418-Y11-4111
#> Mary Hartford CA 319-Z19-4341
#> Evan Nolan IL 219-532-c301

# this doesn't make much sense as an example
# it does not produce one token per row
melt_table(ws)
#> Warning: `melt_table()` was deprecated in readr 2.0.0.
#> Please use `meltr::melt_table()` instead
#> # A tibble: 4 × 4
#>     row   col data_type value                        
#>   <dbl> <dbl> <chr>     <chr>                        
#> 1     1     1 character first last state phone       
#> 2     2     1 character John Smith WA 418-Y11-4111   
#> 3     3     1 character Mary Hartford CA 319-Z19-4341
#> 4     4     1 character Evan Nolan IL 219-532-c301

# this does
melt_table2(ws)
#> Warning: `melt_table2()` was deprecated in readr 2.0.0.
#> Please use `meltr::melt_table2()` instead
#> # A tibble: 16 × 4
#>      row   col data_type value       
#>    <dbl> <dbl> <chr>     <chr>       
#>  1     1     1 character first       
#>  2     1     2 character last        
#>  3     1     3 character state       
#>  4     1     4 character phone       
#>  5     2     1 character John        
#>  6     2     2 character Smith       
#>  7     2     3 character WA          
#>  8     2     4 character 418-Y11-4111
#>  9     3     1 character Mary        
#> 10     3     2 character Hartford    
#> 11     3     3 character CA          
#> 12     3     4 character 319-Z19-4341
#> 13     4     1 character Evan        
#> 14     4     2 character Nolan       
#> 15     4     3 character IL          
#> 16     4     4 character 219-532-c301

Created on 2022-01-24 by the reprex package (v2.0.1.9000)

R/read_table.R Outdated Show resolved Hide resolved
@sbearrows sbearrows force-pushed the update-read_table-example branch from 1ad3c59 to 79e1cd4 Compare January 25, 2022 21:39
@jennybc jennybc marked this pull request as ready for review January 25, 2022 21:54
@jennybc jennybc changed the title Update read table example Add new whitespace-delimited example file and update examples for read_table() and melt_*() Jan 25, 2022
@jennybc jennybc merged commit 90736aa into tidyverse:main Jan 25, 2022
@sbearrows sbearrows deleted the update-read_table-example branch January 25, 2022 22:19
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.

Example dataset for read_table()
2 participants