An open API service indexing awesome lists of open source software.

https://github.com/koderkow/pathr

Work with URL and file paths in R
https://github.com/koderkow/pathr

r string-manipulation url-parser

Last synced: about 1 month ago
JSON representation

Work with URL and file paths in R

Awesome Lists containing this project

README

          

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)

devtools::load_all()
```

# pathr

[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[![CRAN status](https://www.r-pkg.org/badges/version/pathr)](https://CRAN.R-project.org/package=pathr)
[![Codecov test coverage](https://codecov.io/gh/KoderKow/pathr/branch/main/graph/badge.svg)](https://app.codecov.io/gh/KoderKow/pathr?branch=main)
[![R-CMD-check](https://github.com/KoderKow/pathr/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/KoderKow/pathr/actions/workflows/R-CMD-check.yaml)

The goal of {pathr} is to simplify the process of string manipulations for URLs and file paths. This uses modified functions from the [{httr}](https://github.com/r-lib/httr) to create opinionated functions that allow working with URLs and paths easier.

## Installation

You can install the development version of {pathr} like so:

``` r
devtools::install_github("koderkow/pathr")
```

## URLs

### Basic URLs

```r
library(pathr)
```

Sort components of a URL

```{r parse}
url <- "https://github.com/KoderKow/pathr"

x <- parse_url(url)

x

str(x)
```

Put it back to together

```{r}
build_url(x)
```

### URLs with a file path

URLs with item paths will be detected

```{r}
url <- "https://github.com/KoderKow/pathr/example-folder/my-data.csv"

x <- parse_url(url)

x

str(x)
```

Building the URL ignores the file element

```{r}
build_url(x)
```

### Vector of URLS

```{r}
urls <- c(
"https://github.com/KoderKow/pathr1",
"https://github.com/KoderKow/pathr2"
)

x <- parse_url(urls)

x

str(x)
```

#### Working with a *pathr* object

Below are three common methods for turning a lists of lists into a data frame. The base R method is built into the {pathr} package due to it needing no dependencies. The other methods, data.table and dplyr, go over the process for those work flows.

#### Base R

```{r}
pathr_to_df(x)
```

##### data.table

```r
d <- data.table::rbindlist(x)

#> Warning message:
#> In data.table::rbindlist(x) :
#> Column 6 ['port'] of item 1 is length 0. This (and 17 others like it) has been filled with NA (NULL for list columns) to make each item uniform.
```

##### dplyr

The *pathr* class does not play nicely with `dplyr::bind_rows`.

```r
d <-
x |>
dplyr::bind_rows()

#> Error in `dplyr::bind_rows()`:
#> ! Argument 1 must be a data frame or a named atomic vector.
#> Run `rlang::last_error()` to see where the error occurred.
```

The function `remove_pathr()` will unclass any *pathr* object or a list containing nothing but *pathr* objects. This makes it easy to move the object into Rs default class, list.

```r
d <-
x |>
remove_pathr() |>
dplyr::bind_rows()
```

## Thanks to

- [{httr}](https://github.com/r-lib/httr) package for the code for `parse_url()` and `build_url()`