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
- Host: GitHub
- URL: https://github.com/koderkow/pathr
- Owner: KoderKow
- License: other
- Created: 2023-02-28T03:38:04.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-03T07:14:15.000Z (over 3 years ago)
- Last Synced: 2025-01-20T14:28:23.624Z (over 1 year ago)
- Topics: r, string-manipulation, url-parser
- Language: R
- Homepage: https://koderkow.github.io/pathr/
- Size: 76.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Support: .github/SUPPORT.md
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
[](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[](https://CRAN.R-project.org/package=pathr)
[](https://app.codecov.io/gh/KoderKow/pathr?branch=main)
[](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()`