Quantcast
Channel: R-bloggers
Viewing all articles
Browse latest Browse all 12300

R package development workflow (assuming you’re using macOS or Linux)

$
0
0
[This article was first published on T. Moudiki's Webpage - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I’m using VS Code on macOS for Python, R, Javascript (etc.) development. I needed a quick/automated workflow for my R package development at the command line, so I created this Makefile (if necessary install Python):

https://gist.github.com/thierrymoudiki/3bd7cfa099aef0c64eb5f91138d8cedb

All you need to do is: store the Makefile at the root of the package folder. Type make or make help at the command line to see all the commands available. You can start with make initialize, that will install devtools and rmarkdown, if they’re not available yet. Here’s what you can do so far:

  • make clean: remove all R artifacts
  • make start: start or restart R session
  • make setwd: set working directory to current directory
  • make docs: generate package documentation
  • make check: check package for potential errors
  • make install: install package
  • make initialize: initialize environment (install packages)
  • make load: load all (when developing the package)
  • make render: run R markdown file in /vignettes (you’ll be prompted to give the file name, without extension)

Of course, work in progress (no package creation, or running tests, etc.). And also, nothing malicious about the script (ask a LLM to break it down for you if necessary 🙂 ). Feel free to comment the GitHub Gist.

.PHONY: clean docs start setwd check install load render initialize.DEFAULT_GOAL := helpdefine BROWSER_PYSCRIPTimport os, webbrowser, sysfrom urllib.request import pathname2url# The input is expected to be the full HTML filenamefilename = sys.argv[1]filepath = os.path.abspath(os.path.join("./vignettes/", filename))webbrowser.open("file://" + pathname2url(filepath))endefexport BROWSER_PYSCRIPTdefine PRINT_HELP_PYSCRIPTimport re, sysfor line in sys.stdin:match = re.match(r'^([a-zA-Z_-]+):.*?## (.*)$$', line)if match:target, help = match.groups()print("%-20s %s" % (target, help))endefexport PRINT_HELP_PYSCRIPTBROWSER := python3 -c "$$BROWSER_PYSCRIPT"help:@python3 -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST)clean: ## remove all build, test, coverage and Python artifactsrm -f .Rbuildignorerm -f .Rhistoryrm -f *.RDatarm -f *.Rprojrm -rf .Rproj.userstart: ## start or restart R sessionRscript -e "system('R')"setwd: ## set working directoryRscript -e "setwd(getwd())"docs: clean setwd ## generate docsRscript -e "devtools::document('.')"check: clean setwd ## check packageRscript -e "devtools::check('.')"install: clean setwd ## install packageRscript -e "devtools::install('.')"initialize: setwd ## initialize environment (install packages)Rscript -e "utils::install.packages(c('devtools', 'rmarkdown'), repos='https://cloud.r-project.org')"load: clean setwd ## load all (when developing the package)Rscript -e "devtools::load_all('.')"render: ## run markdown file in /vignettes@read -p "Enter the name of the Rmd file (without extension): " filename; \Rscript -e "rmarkdown::render(paste0('./vignettes/', '$$filename', '.Rmd'))"; \python3 -c "$$BROWSER_PYSCRIPT" "$$filename.html"
To leave a comment for the author, please follow the link and comment on their blog: T. Moudiki's Webpage - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Continue reading: R package development workflow (assuming you’re using macOS or Linux)

Viewing all articles
Browse latest Browse all 12300

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>