Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Create_R_Packages_-_Gerry_Alfa_Dito.pdf

 Create_R_Packages_-_Gerry_Alfa_Dito.pdf

Gerry Alfa Dito

March 10, 2022
Tweet

More Decks by Gerry Alfa Dito

Other Decks in Programming

Transcript

  1. /TAHAPAN UMUM PEMBUATAN R PACKAGE /1. Pembuatan R Code /2.

    Package Metadata /3. Lisensi /4. Dokumentasi Objek /5. Testing /6. Namespace /7. Pemeriksaan Automatis /8. Publikasi Package INDEX.HTML
  2. INDEX.HTML /Menata fungsi-fungsi R ke dalam file .r /PRINSIP PENATAAN

    /SOURCE FILE /KETERANGAN SATU FUNGSI glmnetExt/R/plot.boot_glmnet .R Defines exactly one function, plot.boot_glmnet() FUNGSI UTAMA + HELPERS glmnetExt/R/boot_glmnet. R Defines the user- facing boot_glmnet() And Private helpers KELUARGA FUNGSI tidyr/R/rectangle.R Defines a family of functions for “rectangling” nested lists (hoist() and the unnest() functions
  3. INDEX.HTML Fast feedback via load_all() As you add or modify

    functions defined in files below R/, you will naturally want to try them out. We want to reiterate our strong recommendation to use devtools::load_all() to make them available for interactive exploration instead of, for example, source()ing files below R/. The main coverage of load_all() is in the workflows chapter and load_all() also shows up as one of the natural development tasks in the whole game. Compared to the alternatives, load_all() helps you to iterate more quickly and provides an excellent approximation to the namespace regime of an installed package.
  4. INDEX.HTML File Deskripsi Package: mypackage Title: What the Package Does

    (One Line, Title Case) Version: 0.0.0.9000 Authors@R: person(given=“Name”, email="[email protected]", role = c("aut", "cre"), comment = c(ORCID = "YOUR- ORCID-ID")) Description: What the package does (one paragraph). License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a license Encoding: UTF-8 Roxygen: list(markdown = TRUE) RoxygenNote: 7.1.2
  5. INDEX.HTML Dependencies: What does your package need? Suggests: your package

    can use these packages, but doesn’t require them. You might use suggested packages for example datasets, to run tests, build vignettes, or maybe there’s only one function that needs the package. Imports: packages listed here must be present for your package to work. Any time your package is installed, those packages will also be installed, if not already present. devtools::load_all() also checks that all packages in Imports are installed. Depends: Prior to the roll-out of namespaces in R 2.14.0 in 2011, Depends was the only way to “depend” on another package. The easiest way to add a package to Imports, Suggests or Depends is with usethis::use_package() and usethis::use_dev_package()
  6. INDEX.HTML Package Version Recommended framework for managing the package version

    number: ✓ Always use . as the separator, never -. ✓ A released version number consists of three numbers, <major>.<minor>.<patch>. For version number 1.9.2, 1 is the major number, 9 is the minor number, and 2 is the patch number. Never use versions like 1.0, instead always spell out the three components, 1.0.0. ✓ An in-development package has a fourth component: the development version. This should start at 9000. For example, the first version of the package should be 0.0.0.9000. There are two reasons for this recommendation: First, it makes it easy to see if a package is released or in-development. Also, the use of the fourth place means that you’re not limited to what the next version will be. 0.0.1, 0.1.0, and 1.0.0 are all greater than 0.0.0.9000.
  7. INDEX.HTML Lisensi ➢ If you want a permissive license so

    people can use your code with minimal restrictions, choose the MIT license with use_mit_license(). ➢ If you want a copyleft license so that all derivatives and bundles of your code are also open source, choose the GPLv3 license with use_gpl_license(). ➢ If your package primarily contains data, not code, and you want minimal restrictions, choose the CC0 license with use_cc0_license(). Or if you want to require attribution when your data is used, choose the CC BY license by calling use_ccby_license().
  8. INDEX.HTML Dokumentasi Object #' Add together two numbers #' #'

    @param x A number. #' @param y A number. #' @return The sum of \code{x} and \code{y}. #' @examples #' add(1, 1) #' add(10, 1) add <- function(x, y) { x + y } The process starts when you add roxygen comments to your source file: roxygen comments start with #' to distinguish them from regular comments. Here’s documentation for a simple function:
  9. INDEX.HTML Dokumentasi Object The process starts when you add roxygen

    comments to your source file: roxygen comments start with #' to distinguish them from regular comments. Here’s documentation for a simple function:
  10. INDEX.HTML Documentation Workflow 1. Add roxygen comments to your .R

    files. 2. Click in the clean & build pane or press Ctrl/Cmd + Shift + B. This completely rebuilds the package, including updating all the documentation, installs it in your regular library, then restarts R and reloads your package. This is slow but thorough. 3. Preview documentation with ?. 4. Rinse and repeat until the documentation looks the way you want.
  11. INDEX.HTML Namespace S3method(as.character,expectation) S3method(compare,character) export(auto_test) export(auto_test_package) export(colourise) export(context) @export exportClasses(ListReporter)

    exportClasses(MinimalReporter) importFrom(methods,setRefClass) useDynLib(testthat,duplicate_) useDynLib(testthat,reassign_function)} Generating the namespace with roxygen2 is just like generating function documentation with roxygen2. You use roxygen2 blocks (starting with #') and tags (starting with @). The workflow is the same:
  12. INDEX.HTML Dokumentasi Object In total, there are eight namespace directives.

    Four describe exports: export(): export functions (including S3 and S4 generics). exportPattern(): export all functions that match a pattern. exportClasses(), exportMethods(): export S4 classes and methods. S3method(): export S3 methods. And four describe imports: import(): import all functions from a package. importFrom(): import selected functions (including S4 generics). importClassesFrom(), importMethodsFrom(): import S4 classes and methods. useDynLib(): import a function from C. This is described in more detail in compiled code.
  13. INDEX.HTML Publikasi CRAN Jalankan sintaks devtools::release(). Ikuti Langkah-Langkah yang ditampilkan

    dari output sintaks tersebut devtools::release(). Uji coba terlebih dahulu di beberapa platfrom (Windows, Linux, Mac) dengan menjalankan devtools::check_rhub()