SHARE
TWEET

Untitled

a guest Aug 17th, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Create a new RStudio package project in a directory of your choice
  2. # We'll assume it's called, "mypkg"
  3. # Check "Create a git repository" if you use git!
  4.  
  5. # Packages we'll need
  6. install.packages(c(
  7.   "devtools",
  8.   "usethis",
  9.   "roxygen2",
  10.   "testthat"
  11. ))
  12.  
  13.  
  14. # In Tools > Project Options > Build Tools make sure both
  15. # "Use devtools package functions if available", and
  16. # "Generate documentation with Roxygen" are checked (accept defaults)
  17.  
  18. # Take a look at R/hello.R and man/hello.Rmd... then delete
  19. tryCatch(
  20.   file.remove(c("R/hello.R", "man/hello.Rd")),
  21.   error = NULL
  22. )
  23.  
  24. # Oops, you've just realised you do want to use git!
  25. install.packages("git2r")
  26. git2r::config()
  27. usethis::use_git(message = "Initial commit")
  28.  
  29. # usethis is soooo useful
  30. # Note: On my Windows installation this fails if the package is on a network drive
  31. usethis::use_readme_md() # or, usethis::use_readme_rmd() for RMarkdown
  32. usethis::use_mit_license("Your Name, Your Institution")
  33. usethis::use_r("my-function")
  34. usethis::use_testthat()
  35. usethis::use_test("my-function")
  36.  
  37. # Go to my-function.R (or whatever you called it), and type or paste your
  38. # code. Add any dependencies with:
  39. #   devtools::use_package("pkgname")
  40.  
  41. # Then add some roxygen-compatible documentation to my-function.R
  42. # Roxygen documentation lines start with #' and should appear before the code
  43. # Don't forget "#' @import pkgname" for any dependencies and "#' @export"
  44. tryCatch(file.remove("NAMESPACE"), error = NULL) # recreate with roxygen
  45. devtools::document(roclets = c('rd', 'collate', 'namespace'))
  46. # Shortcut for previous line: Ctrl + Shift + D
  47.  
  48. # Got to test-my-function.R (or whatever), and write some tests!
  49. devtools::test()
  50. # Shortcut for previous line: Ctrl + Shift + T
  51.  
  52. # Go to README.md (or README.Rmd if you went that way) and make some changes!
  53.  
  54. # Go to DESCRIPTION and make some changes!
  55. # Enter the package Title: and Version:, and update the Description: field
  56. # Add your name and email to the Author: and Maintainer: fields
  57.  
  58. # Check the package
  59. devtools::check()
  60. # Shortcut for previous line: Ctrl + Shift + E
  61.  
  62. # You're using git, right? Add and commit those changes
  63. git2r::add(path = "*")
  64. git2r::commit(message = "")
  65.  
  66. # Load your package
  67. devtools::load_all(".")
  68. # Shortcut for previous line: Ctrl + Shift + L
  69.  
  70. # Try out your function
  71.  
  72. # Why not install your own package?
  73. devtools::install()
  74. # Or, Build > Install and Restart, or shortcut: Ctrl + Shift + B
  75.  
  76. # Now to use it, just attach the library
  77. library(mypkg)
  78. # or, use the "pkgname::function()" syntax
  79. # Either way, enjoy your new personal R package!
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top