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

Motivate #12

Merged
merged 1 commit into from
Jan 9, 2019
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
34 changes: 5 additions & 29 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,9 @@ Your nickname is R-guru. Your colleague enjoys analyzing data and she head that

> I have a couple of minutes before the next meeting. Can you please show me how to start playing with some data in R?

Which of the following two options do you choose?
TWO MINUTES?!

### Option 1 (with seriously great packages)

```{r}
# Here I have a bunch of files, including some .csv files with data.
dir()

# I "open" a bunch of programs I need to read the .csv files.
# This program is to find files in my computer.
library(fs)
# This program is to read each individual .csv file.
library(readr)
# And this program is to iterate over each file so I can read all of them.
library(purrr)

# First I need to find the address in my computer to each file I want to read.
# `"."` means "here" -- where I have the files I showed you before
# `regexp = "[.]csv$"` means "care only about .csv files -- ignore the rest".
csv_files <- dir_ls(".", regexp = "[.]csv$")

# So I take the addresses to the .csv files, and then ...
csv_files %>%
# ... and I read each file into a list
map(read_csv)

# Done.
```

### Option 2 (with the modest tor)
Don't panic. __tor__ is here to help you.

```{r}
# Here I have a bunch of files, including some .csv files with data.
Expand Down Expand Up @@ -220,6 +193,9 @@ format_path(names(dfms), "csv", base = "home", prefix = "this-")
Combine it with [__purrr__](https://purrr.tidyverse.org/).

```{r}
library(purrr)
library(fs)

imap_chr(dfms, ~ format_path(.y, "csv"))

# Same
Expand Down
168 changes: 96 additions & 72 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ head that R is great.
> I have a couple of minutes before the next meeting. Can you please
> show me how to start playing with some data in R?

Which of the following two options do you choose?
TWO MINUTES?\!

### Option 1 (with seriously great packages)
Don’t panic. **tor** is here to help you.

``` r
# Here I have a bunch of files, including some .csv files with data.
Expand All @@ -49,26 +49,14 @@ dir()
#> [7] "DESCRIPTION" "docs" "inst"
#> [10] "LICENSE.md" "man" "NAMESPACE"
#> [13] "NEWS.md" "R" "README.md"
#> [16] "README.Rmd" "tests" "tmp.R"
#> [19] "tor.Rproj"
#> [16] "README.Rmd" "tests" "this-csv1.csv"
#> [19] "this-csv2.csv" "tmp.R" "tor.Rproj"

# I "open" a bunch of programs I need to read the .csv files.
# This program is to find files in my computer.
library(fs)
# This program is to read each individual .csv file.
library(readr)
# And this program is to iterate over each file so I can read all of them.
library(purrr)

# First I need to find the address in my computer to each file I want to read.
# `"."` means "here" -- where I have the files I showed you before
# `regexp = "[.]csv$"` means "care only about .csv files -- ignore the rest".
csv_files <- dir_ls(".", regexp = "[.]csv$")
# I "open" the programs I need to read the .csv files.
library(tor)

# So I take the addresses to the .csv files, and then ...
csv_files %>%
# ... and I read each file into a list
map(read_csv)
# And I list the data in all those .csv files
list_csv()
#> Parsed with column specification:
#> cols(
#> x = col_double()
Expand All @@ -77,41 +65,6 @@ csv_files %>%
#> cols(
#> y = col_character()
#> )
#> $csv1.csv
#> # A tibble: 2 x 1
#> x
#> <dbl>
#> 1 1
#> 2 2
#>
#> $csv2.csv
#> # A tibble: 2 x 1
#> y
#> <chr>
#> 1 a
#> 2 b

# Done.
```

### Option 2 (with the modest tor)

``` r
# Here I have a bunch of files, including some .csv files with data.
dir()
#> [1] "_pkgdown.yml" "cran-comments.md" "CRAN-RELEASE"
#> [4] "csv1.csv" "csv2.csv" "datasets"
#> [7] "DESCRIPTION" "docs" "inst"
#> [10] "LICENSE.md" "man" "NAMESPACE"
#> [13] "NEWS.md" "R" "README.md"
#> [16] "README.Rmd" "tests" "tmp.R"
#> [19] "tor.Rproj"

# I "open" the programs I need to read the .csv files.
library(tor)

# And I list the data in all those .csv files
list_csv()
#> Parsed with column specification:
#> cols(
#> x = col_double()
Expand All @@ -132,6 +85,20 @@ list_csv()
#> y
#> <chr>
#> 1 a
#> 2 b
#>
#> $`this-csv1`
#> # A tibble: 2 x 1
#> x
#> <dbl>
#> 1 1
#> 2 2
#>
#> $`this-csv2`
#> # A tibble: 2 x 1
#> y
#> <chr>
#> 1 a
#> 2 b

# Done.
Expand All @@ -155,8 +122,8 @@ dir()
#> [7] "DESCRIPTION" "docs" "inst"
#> [10] "LICENSE.md" "man" "NAMESPACE"
#> [13] "NEWS.md" "R" "README.md"
#> [16] "README.Rmd" "tests" "tmp.R"
#> [19] "tor.Rproj"
#> [16] "README.Rmd" "tests" "this-csv1.csv"
#> [19] "this-csv2.csv" "tmp.R" "tor.Rproj"

list_csv()
#> Parsed with column specification:
Expand All @@ -167,6 +134,14 @@ list_csv()
#> cols(
#> y = col_character()
#> )
#> Parsed with column specification:
#> cols(
#> x = col_double()
#> )
#> Parsed with column specification:
#> cols(
#> y = col_character()
#> )
#> $csv1
#> # A tibble: 2 x 1
#> x
Expand All @@ -179,6 +154,20 @@ list_csv()
#> y
#> <chr>
#> 1 a
#> 2 b
#>
#> $`this-csv1`
#> # A tibble: 2 x 1
#> x
#> <dbl>
#> 1 1
#> 2 2
#>
#> $`this-csv2`
#> # A tibble: 2 x 1
#> y
#> <chr>
#> 1 a
#> 2 b
```

Expand Down Expand Up @@ -284,11 +273,6 @@ It understands lambda functions and formulas (powered by
``` r
# Use the pipe (%>%)
library(magrittr)
#>
#> Attaching package: 'magrittr'
#> The following object is masked from 'package:purrr':
#>
#> set_names

(path_rdata <- tor_example("rdata"))
#> [1] "C:/Users/LeporeM/Documents/R/R-3.5.2/library/tor/extdata/rdata"
Expand Down Expand Up @@ -433,8 +417,8 @@ dir()
#> [7] "DESCRIPTION" "docs" "inst"
#> [10] "LICENSE.md" "man" "NAMESPACE"
#> [13] "NEWS.md" "R" "README.md"
#> [16] "README.Rmd" "tests" "tmp.R"
#> [19] "tor.Rproj"
#> [16] "README.Rmd" "tests" "this-csv1.csv"
#> [19] "this-csv2.csv" "tmp.R" "tor.Rproj"

load_csv()

Expand Down Expand Up @@ -485,7 +469,7 @@ output files.

``` r
dir(pattern = "[.]csv$")
#> [1] "csv1.csv" "csv2.csv"
#> [1] "csv1.csv" "csv2.csv" "this-csv1.csv" "this-csv2.csv"

dfms <- list_csv()
#> Parsed with column specification:
Expand All @@ -496,25 +480,42 @@ dfms <- list_csv()
#> cols(
#> y = col_character()
#> )
#> Parsed with column specification:
#> cols(
#> x = col_double()
#> )
#> Parsed with column specification:
#> cols(
#> y = col_character()
#> )

format_path(names(dfms), "csv")
#> [1] "./csv1.csv" "./csv2.csv"
#> [1] "./csv1.csv" "./csv2.csv" "./this-csv1.csv" "./this-csv2.csv"

format_path(names(dfms), "csv", base = "home", prefix = "this-")
#> [1] "home/this-csv1.csv" "home/this-csv2.csv"
#> [1] "home/this-csv1.csv" "home/this-csv2.csv"
#> [3] "home/this-this-csv1.csv" "home/this-this-csv2.csv"
```

Combine it with [**purrr**](https://purrr.tidyverse.org/).

``` r
library(purrr)
#>
#> Attaching package: 'purrr'
#> The following object is masked from 'package:magrittr':
#>
#> set_names
library(fs)

imap_chr(dfms, ~ format_path(.y, "csv"))
#> csv1 csv2
#> "./csv1.csv" "./csv2.csv"
#> csv1 csv2 this-csv1 this-csv2
#> "./csv1.csv" "./csv2.csv" "./this-csv1.csv" "./this-csv2.csv"

# Same
map_chr(dfms, ~ format_path(names(.), "csv", ".", "this-"))
#> csv1 csv2
#> "./this-x.csv" "./this-y.csv"
#> csv1 csv2 this-csv1 this-csv2
#> "./this-x.csv" "./this-y.csv" "./this-x.csv" "./this-y.csv"

(dfs <- list_csv())
#> Parsed with column specification:
Expand All @@ -525,6 +526,14 @@ map_chr(dfms, ~ format_path(names(.), "csv", ".", "this-"))
#> cols(
#> y = col_character()
#> )
#> Parsed with column specification:
#> cols(
#> x = col_double()
#> )
#> Parsed with column specification:
#> cols(
#> y = col_character()
#> )
#> $csv1
#> # A tibble: 2 x 1
#> x
Expand All @@ -537,6 +546,20 @@ map_chr(dfms, ~ format_path(names(.), "csv", ".", "this-"))
#> y
#> <chr>
#> 1 a
#> 2 b
#>
#> $`this-csv1`
#> # A tibble: 2 x 1
#> x
#> <dbl>
#> 1 1
#> 2 2
#>
#> $`this-csv2`
#> # A tibble: 2 x 1
#> y
#> <chr>
#> 1 a
#> 2 b

paths <- dfs %>%
Expand All @@ -545,7 +568,8 @@ paths <- dfs %>%
walk2(dfs, paths, readr::write_csv)

dir_ls(".", regexp = "this-")
#> this-csv1.csv this-csv2.csv
#> this-csv1.csv this-csv2.csv this-this-csv1.csv
#> this-this-csv2.csv
```

# Related projects
Expand Down
Loading