Skip to content

Commit

Permalink
added post about personal packages
Browse files Browse the repository at this point in the history
  • Loading branch information
TinasheMTapera committed Aug 18, 2024
1 parent eeda3e2 commit f716ddd
Show file tree
Hide file tree
Showing 6 changed files with 1,205 additions and 387 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"hash": "63d87fb05f078e9cfea3e1f3bc1853c4",
"result": {
"markdown": "---\ntitle: \"Building a Personal Package\"\nauthor: \"Tinashe M. Tapera\"\ndate: \"2024-08-16\"\nimage: https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExcG5rOWxoeDBxOG10dTh2OTZtamVsMmJzbmk3M3VkbGM4YmM1NTBscCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/XbJLL1uIH3PwEB2hui/giphy.gif\n---\n\n\nDo you ever find yourself revisiting short code snippets you’ve written over and over\nagain? It’s probably a sign that you should be maintaining a personal R package.\n\n![You should have a package](https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExcG5rOWxoeDBxOG10dTh2OTZtamVsMmJzbmk3M3VkbGM4YmM1NTBscCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/XbJLL1uIH3PwEB2hui/giphy.gif)\n\nThere are multiple benefits to writing your own personal package. Some early career\nR programmers believe that package development is a task that you should only \nundertake if you’re creating something particularly novel, elaborate, or serious, \nwhen in truth an R package is just a collection of functions that help you get your\njob done. It can be as complex, or as simple, as you need it to be. Some advantages \ninclude improved code organization, easier code reuse, and better collaboration \nwith others. Additionally, creating your own package allows you to document your\nfunctions and ensure they are well-tested, which can save you time in the long run. \nWhat’s more, you get to practice your package development workflow in a low-stakes \nenvironment, giving you the option to be as formal or informal as you want with \nregards to standardized workflows, tools, and frameworks. For example, I used my \npersonal package as a way to test out a bunch of features from the `fusen` package, \nwhich is a package designed for “notebook-driven-development” that I’ve been \ninvestigating. Importantly, you can use the package to document any particularly \nelegant or novel solutions you might have come up with for niche situations you’ve \nbeen faced with in your work (without a place to put them existing beforehand, \nyou’re less likely to document it when the moment arrives), and by using GitHub, \nhave access to these solutions in a matter of seconds at any machine.\n\nSo, with all of that being said, here are 3 functions I put in my personal package \nto get the ball rolling. Over time, my list of functions will grow and the \npackage’s utility will increase on its own.\n\n## `len_uniq`\n\nIf you ever find yourself doing this pattern often:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nsome_vector %>%\n unique() %>%\n length()\n\n# OR\nlength(unique(some_vector))\n```\n:::\n\n\nIt's time to write a function for it:\n\n\n::: {.cell}\n\n```{.r .cell-code}\n#' len_uniq\n#' \n#' A short hand for unique() %>% length()\n#' \n#' @param vec A vector with a various objects\n#' @return The number of unique objects in the vector\n#' @export\n#'\n#' @examples\nlen_uniq <- function(vec) {\n length(unique(vec))\n}\n```\n:::\n\n\n`fusen` allows me to write all of the function's examples and tests in one single RMarkdown\nor Quarto document. Check out the [repo](https://github.com/TinasheMTapera/tinasheR/blob/main/dev/flat_core.Rmd) to see how it works.\n\n## `sort_uniq`\n\nLikewise, I'm also prone doing this a lot:\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nsome_vec %>%\n unique() %>%\n sort()\n\n# OR \nsort(unique(some_vec))\n```\n:::\n\n\nSo, putting that in a function, I defined:\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n#' sort_uniq\n#' \n#' A Shorthand for unique() %>% sort()\n#'\n#' @param vec A vector that can be reasonably sorted\n#' @return A sorted vector of unique values in the vector\n#' @export\n#'\n#' @examples\nsort_uniq <- function(vec) {\n sort(unique(vec))\n}\n```\n:::\n\n\n## Not %in% operator\n\nThis is a personal pet peeve of mine, so I had to write a function\nfor it. Python's `not in` is just too fun not to emulate in R:\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n#' %!in%\n#' \n#' An operator that negates the %in% operator\n#' \n#' @return boolean\n#' \n#' @export\n`%!in%` <- Negate(`%in%`)\n```\n:::\n\n\nAnd using these 3 functions, I already had enough material to develop\na simple package that includes documentation with Roxygen,\ntesting with `testthat`, and publishing with `pkgdown` — all useful\nskills to know before the day comes that you're asked to build\nsomething \"important\".\n\n## Conclusion\n\nEven with that little effort, we've managed to develop a fully\ndocumented and operational personal R package available on\nGithub for me to use whenever I need my shorthand snippets: [tinasheR](https://tinashemtapera.github.io/tinasheR/). \n\nI hope this might encourage you to develop your own!",
"supporting": [],
"filters": [
"rmarkdown/pagebreak.lua"
],
"includes": {},
"engineDependencies": {},
"preserve": {},
"postProcess": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
---
title: "Building a Personal Package"
author: "Tinashe M. Tapera"
date: "2024-08-16"
image: https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExcG5rOWxoeDBxOG10dTh2OTZtamVsMmJzbmk3M3VkbGM4YmM1NTBscCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/XbJLL1uIH3PwEB2hui/giphy.gif
---

Do you ever find yourself revisiting short code snippets you’ve written over and over
again? It’s probably a sign that you should be maintaining a personal R package.

![You should have a package](https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExcG5rOWxoeDBxOG10dTh2OTZtamVsMmJzbmk3M3VkbGM4YmM1NTBscCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/XbJLL1uIH3PwEB2hui/giphy.gif)

There are multiple benefits to writing your own personal package. Some early career
R programmers believe that package development is a task that you should only
undertake if you’re creating something particularly novel, elaborate, or serious,
when in truth an R package is just a collection of functions that help you get your
job done. It can be as complex, or as simple, as you need it to be. Some advantages
include improved code organization, easier code reuse, and better collaboration
with others. Additionally, creating your own package allows you to document your
functions and ensure they are well-tested, which can save you time in the long run.
What’s more, you get to practice your package development workflow in a low-stakes
environment, giving you the option to be as formal or informal as you want with
regards to standardized workflows, tools, and frameworks. For example, I used my
personal package as a way to test out a bunch of features from the `fusen` package,
which is a package designed for “notebook-driven-development” that I’ve been
investigating. Importantly, you can use the package to document any particularly
elegant or novel solutions you might have come up with for niche situations you’ve
been faced with in your work (without a place to put them existing beforehand,
you’re less likely to document it when the moment arrives), and by using GitHub,
have access to these solutions in a matter of seconds at any machine.

So, with all of that being said, here are 3 functions I put in my personal package
to get the ball rolling. Over time, my list of functions will grow and the
package’s utility will increase on its own.

## `len_uniq`

If you ever find yourself doing this pattern often:

```{r, eval=FALSE}
some_vector %>%
unique() %>%
length()
# OR
length(unique(some_vector))
```

It's time to write a function for it:

```{r function-len_uniq}
#' len_uniq
#'
#' A short hand for unique() %>% length()
#'
#' @param vec A vector with a various objects
#' @return The number of unique objects in the vector
#' @export
#'
#' @examples
len_uniq <- function(vec) {
length(unique(vec))
}
```

`fusen` allows me to write all of the function's examples and tests in one single RMarkdown
or Quarto document. Check out the [repo](https://github.com/TinasheMTapera/tinasheR/blob/main/dev/flat_core.Rmd) to see how it works.

## `sort_uniq`

Likewise, I'm also prone doing this a lot:


```{r, eval=FALSE}
some_vec %>%
unique() %>%
sort()
# OR
sort(unique(some_vec))
```

So, putting that in a function, I defined:


```{r}
#' sort_uniq
#'
#' A Shorthand for unique() %>% sort()
#'
#' @param vec A vector that can be reasonably sorted
#' @return A sorted vector of unique values in the vector
#' @export
#'
#' @examples
sort_uniq <- function(vec) {
sort(unique(vec))
}
```

## Not %in% operator

This is a personal pet peeve of mine, so I had to write a function
for it. Python's `not in` is just too fun not to emulate in R:


```{r}
#' %!in%
#'
#' An operator that negates the %in% operator
#'
#' @return boolean
#'
#' @export
`%!in%` <- Negate(`%in%`)
```

And using these 3 functions, I already had enough material to develop
a simple package that includes documentation with Roxygen,
testing with `testthat`, and publishing with `pkgdown` — all useful
skills to know before the day comes that you're asked to build
something "important".

## Conclusion

Even with that little effort, we've managed to develop a fully
documented and operational personal R package available on
Github for me to use whenever I need my shorthand snippets: [tinasheR](https://tinashemtapera.github.io/tinasheR/).

I hope this might encourage you to develop your own!
Loading

0 comments on commit f716ddd

Please sign in to comment.