Skip to content

Commit 2de9cdb

Browse files
committed
Update style_pkg() to handle R and Rmd, update and add tests
1 parent 6d620fb commit 2de9cdb

File tree

15 files changed

+267
-16
lines changed

15 files changed

+267
-16
lines changed

R/ui.R

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,47 @@ style_pkg <- function(pkg = ".",
4040
...,
4141
style = tidyverse_style,
4242
transformers = style(...),
43-
filetype = c("r"),
43+
filetype = "R",
4444
exclude_files = "R/RcppExports.R") {
4545
pkg_root <- rprojroot::find_package_root_file(path = pkg)
46-
changed <- withr::with_dir(pkg_root, prettify_local(transformers, exclude_files))
46+
changed <- withr::with_dir(pkg_root,
47+
prettify_local(transformers, filetype, exclude_files)
48+
)
4749
invisible(changed)
4850
}
4951

50-
prettify_local <- function(transformers, exclude_files) {
51-
r_files <- dir(
52-
path = "R", pattern = "[.][rR]$", recursive = TRUE, full.names = TRUE
53-
)
52+
prettify_local <- function(transformers, filetype, exclude_files) {
5453

55-
r_files <- grep("/RcppExports[.]R$", r_files, invert = TRUE, value = TRUE)
56-
test_files <- dir(
57-
path = "tests/testthat", pattern = "[.][rR]$",
58-
recursive = TRUE, full.names = TRUE
59-
)
54+
filetype <- set_and_assert_arg_filetype(filetype)
55+
r_files <- test_files <- data_raw_files <- vignette_files <- readme <- NULL
6056

61-
files <- setdiff(c(r_files, test_files), exclude_files)
57+
if ("\\.r" %in% filetype) {
58+
r_files <- dir(
59+
path = "R", pattern = "\\.r$", ignore.case = TRUE,
60+
recursive = TRUE, full.names = TRUE
61+
)
62+
test_files <- dir(
63+
path = "tests", pattern = "\\.r$", ignore.case = TRUE,
64+
recursive = TRUE, full.names = TRUE
65+
)
66+
data_raw_files <- dir(
67+
path = "data-raw", pattern = "\\.r$", ignore.case = TRUE,
68+
recursive = TRUE, full.names = TRUE
69+
)
70+
}
71+
72+
if ("\\.rmd" %in% filetype) {
73+
vignette_files <- dir(
74+
path = "vignettes", pattern = "\\.rmd$", ignore.case = TRUE,
75+
recursive = TRUE, full.names = TRUE
76+
)
77+
readme <- dir(pattern = "readme\\.rmd", ignore.case = TRUE)
78+
}
79+
80+
files <- setdiff(
81+
c(r_files, test_files, data_raw_files, vignette_files, readme),
82+
exclude_files
83+
)
6284

6385
transform_files(files, transformers)
6486
}

man/style_pkg.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Package: xyzpackage
2+
Title: What the Package Does (one line, title case)
3+
Version: 0.0.0.9000
4+
Authors@R: person("First", "Last", email = "[email protected]", role = c("aut", "cre"))
5+
Description: What the package does (one paragraph).
6+
Depends: R (>= 3.3.2)
7+
License: What license is it under?
8+
Encoding: UTF-8
9+
LazyData: true
10+
Suggests: testthat
11+
LinkingTo:
12+
Rcpp
13+
Imports:
14+
Rcpp
15+
RoxygenNote: 6.0.1.9000
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Generated by roxygen2: do not edit by hand
2+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
2+
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
3+
4+
timesTwo <- function(x) {
5+
.Call("_xyzpackage_timesTwo", PACKAGE = "xyzpackage", x)
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hello_world <- function() {
2+
print("hello, world")
3+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
output: github_document
3+
---
4+
5+
<!-- README.md is generated from README.Rmd. Please edit that file -->
6+
7+
```{r setup, include = FALSE}
8+
knitr::opts_chunk$set(
9+
collapse = TRUE,
10+
comment = "#>",
11+
fig.path = "man/figures/README-"
12+
)
13+
```
14+
# styler
15+
16+
The goal of styler is to ...
17+
18+
## Installation
19+
20+
You can install styler from github with:
21+
22+
```{r gh-installation, eval = FALSE}
23+
# install.packages("devtools")
24+
devtools::install_github("jonmcalder/styler")
25+
```
26+
27+
## Example
28+
29+
This is a basic example which shows you how to solve a common problem:
30+
31+
```{r example}
32+
## basic example code
33+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.o
2+
*.so
3+
*.dll
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Generated by using Rcpp::compileAttributes() -> do not edit by hand
2+
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
3+
4+
#include <Rcpp.h>
5+
6+
using namespace Rcpp;
7+
8+
// timesTwo
9+
NumericVector timesTwo(NumericVector x);
10+
RcppExport SEXP _xyzpackage_timesTwo(SEXP xSEXP) {
11+
BEGIN_RCPP
12+
Rcpp::RObject rcpp_result_gen;
13+
Rcpp::RNGScope rcpp_rngScope_gen;
14+
Rcpp::traits::input_parameter< NumericVector >::type x(xSEXP);
15+
rcpp_result_gen = Rcpp::wrap(timesTwo(x));
16+
return rcpp_result_gen;
17+
END_RCPP
18+
}
19+
20+
static const R_CallMethodDef CallEntries[] = {
21+
{"_xyzpackage_timesTwo", (DL_FUNC) &_xyzpackage_timesTwo, 1},
22+
{NULL, NULL, 0}
23+
};
24+
25+
RcppExport void R_init_xyzpackage(DllInfo *dll) {
26+
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
27+
R_useDynamicSymbols(dll, FALSE);
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <Rcpp.h>
2+
using namespace Rcpp;
3+
4+
// This is a simple example of exporting a C++ function to R. You can
5+
// source this function into an R session using the Rcpp::sourceCpp
6+
// function (or via the Source button on the editor toolbar). Learn
7+
// more about Rcpp at:
8+
//
9+
// http://www.rcpp.org/
10+
// http://adv-r.had.co.nz/Rcpp.html
11+
// http://gallery.rcpp.org/
12+
//
13+
14+
// [[Rcpp::export]]
15+
NumericVector timesTwo(NumericVector x) {
16+
return x * 2;
17+
}
18+
19+
20+
// You can include R code blocks in C++ files processed with sourceCpp
21+
// (useful for testing and development). The R code will be automatically
22+
// run after the compilation.
23+
//
24+
25+
/*** R
26+
timesTwo(42)
27+
*/

0 commit comments

Comments
 (0)