{"id":16576916,"url":"https://github.com/lightbridge-ks/rd","last_synced_at":"2026-06-08T05:31:14.350Z","repository":{"id":104274073,"uuid":"333770908","full_name":"Lightbridge-KS/rd","owner":"Lightbridge-KS","description":"Read multiple files from a directory","archived":false,"fork":false,"pushed_at":"2021-01-31T17:06:51.000Z","size":25,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-05T19:55:43.480Z","etag":null,"topics":["r","r-package","read-directory","read-file"],"latest_commit_sha":null,"homepage":"","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Lightbridge-KS.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-01-28T13:42:29.000Z","updated_at":"2021-11-28T07:04:31.000Z","dependencies_parsed_at":null,"dependency_job_id":"2559aa79-4afb-44d2-b176-be5b757a8f97","html_url":"https://github.com/Lightbridge-KS/rd","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Lightbridge-KS/rd","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lightbridge-KS%2Frd","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lightbridge-KS%2Frd/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lightbridge-KS%2Frd/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lightbridge-KS%2Frd/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Lightbridge-KS","download_url":"https://codeload.github.com/Lightbridge-KS/rd/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lightbridge-KS%2Frd/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34050225,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-08T02:00:07.615Z","response_time":111,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["r","r-package","read-directory","read-file"],"created_at":"2024-10-11T22:09:19.598Z","updated_at":"2026-06-08T05:31:14.345Z","avatar_url":"https://github.com/Lightbridge-KS.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n```\n\n# rd\n\n\u003c!-- badges: start --\u003e\n\n\u003c!-- badges: end --\u003e\n\n**Quick and Easy way to read multiple files from a directory.**\n\n## Installation\n\nYou can install rd from [GitHub](https://github.com/Lightbridge-AI/rd) with:\n\n``` r\n# install.packages(\"devtools\")\ndevtools::install_github(\"Lightbridge-AI/rd\")\n```\n\n## How to read multiple files from a directory\n\nUsually, if you want to read any multiple files form a directory (folder) into a list you can do this ...\n\n**Step 1 : list all files path with `list.files()`**\n\n```{r eval=FALSE}\nabs.path \u003c- list.files(\"path/to/dir\", full.names = T)\n```\n\n**Step 2 : Filter files you want**\n\n```{r eval=FALSE}\ncsv.abs.path \u003c- grep(\"\\\\.csv$\", abs.path, value = T) # select only .csv files\n```\n\n**Step 3 : Loop each file path to read function** (use `utils::read.csv` in this case )\n\n```{r eval=FALSE}\nlist_of_df \u003c- lapply(csv.abs.path, utils::read.csv) # or you can use `purrr:map()`\n```\n\n**Step 4 : Set files name to each component (data frame) of the list**\n\n```{r eval=FALSE}\nnames(list_of_df) \u003c- sub(\"\\\\.[^\\\\.]+$\",\"\", basename(csv.abs.path))\n\nlist_of_df\n```\n\n`basename()` : gives file names\n\n`sub(\"\\\\.[^\\\\.]+$\",\"\", basename(csv.abs.path))` : removes everything after the last dot\n\n## Quick \u0026 Easy Way !\n\n`rd` package wrap all of the above code in to a single function: `rd()`\n\n`rd()` has 3 main arguments\n\n1.  `.f` : Function to read files from a directory (any function that has file path as first argument will work)\n\n2.  `path` : Path to desired directory\n\n3.  `pattern` : Regular expression to match file names and/or file extensions\n\n### Examples\n\n-   **Read all .csv files form working directory** (default) using `utils::read.csv` .\n-   File names are automatically set to names of each data frame.\n\n```{r eval=FALSE}\nlibrary(rd)\n\nrd(utils::read.csv) # default `pattern` is \"\\\\.csv$\" (csv files)\n```\n\n-   **Read .xlsx file from specified directory** using `readxl::read_excel` .\n-   Must specify regular expression to match file extension.\n\n```{r eval=FALSE}\nrd(readxl::read_excel, path = \"path/to/dir\" ,pattern = \"\\\\.xlsx$\") \n```\n\n-   **You can pass extra arguments to `.f` by 2 ways**\n\n    1.   Pass inside `.f` : using formula style similar to [`purrr`](https://purrr.tidyverse.org/reference/map.html) package (recommend)\n\n    2.   Pass outside `.f` : an argument `...` of `rd()` will passed to `.f`\n\n```{r eval=FALSE}\n# Pass `col_names = FALSE` to `readr::read_csv`\nrd(~readr::read_csv(.x, col_names = FALSE), recursive = TRUE, snake_case = TRUE) # inside `.f`\n\nrd(readr::read_csv, recursive = TRUE, snake_case = TRUE, col_names = FALSE) # outside `.f`\n```\n\nPassed an argument `col_names = FALSE` into `readr::read_csv`\n\n`recursive = TRUE` to also read files from sub-directory\n\n`snake_case = TRUE` to format names to snake_case (require `snakecase` package installed)\n\n-   **Read files using multiple engine from multiple path and multiple file extension.**\n\nUsing [`purrr::pmap`](https://purrr.tidyverse.org/reference/map2.html) in combination with `rd()`\n\nNow, you can customize to read any files from any directory with any file reading function you want !!\n\n```{r eval=FALSE}\nparams \u003c- list(\n  .f = c(~read_csv(.x, col_names = F), readxl::read_excel),\n  path = c(\"path/to/dir1\", \"path/to/dir2\"), # `path` can be individual file as well \n  pattern = c(\"\\\\.csv$\", \"\\\\.xlsx\")\n  )\n\npurrr::pmap(params, rd)\n```\n\n## Note\n\nThe `rd()` function was created as a wrapper for possibly any reading functions using functional programming to read multiple files.\n\n**The main goal is to provide a simple and easy way read multiple files from a directory.**\n\nHowever, if you want to further customize file reading beyond this function can do, have a look at\n\n[`fs`](https://fs.r-lib.org) package for more advance files system operation.\n\n\u003cbr\u003e\n\nArgument `.f` of `rd()` actually can be **any function** that take file path as input (similar to [`fs::dir_map`](https://fs.r-lib.org/reference/dir_ls.html) and [`fs::dir_walk`](https://fs.r-lib.org/reference/dir_ls.html) )\n\nHowever, you should use `rd()` to read files because it was optimized in this way.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flightbridge-ks%2Frd","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flightbridge-ks%2Frd","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flightbridge-ks%2Frd/lists"}