Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cat2write lines #87

Merged
merged 5 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Suggests:
knitr,
pkgbuild,
pkgload,
roxygen2 (>= 7.0.1),
roxygen2 (>= 6.0.1),
rmarkdown,
rstudioapi
RoxygenNote: 7.1.1
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ instead.

(Github issue/PR numbers in parentheses)

* New [vignette](http://mc-stan.org/rstantools/articles/) walking through the package creation process. (#9) (thanks to Stefan Siegert)
* New [vignette](https://mc-stan.org/rstantools/articles/) walking through the package creation process. (#9) (thanks to Stefan Siegert)

* `rstan_package_skeleton()` now calls `usethis::create_package()` instead of `utils::package.skeleton()`. (#28)

Expand Down
31 changes: 11 additions & 20 deletions R/init_cpp.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,26 @@ init_cpp <- function(name, path) {
if (!missing(path))
file <- file.path(path, file)

cat(
"// Generated by the rstantools package\n\n",
writeLines(c(
"// Generated by the rstantools package",
"", "",
"#include <R.h>",
"#include <Rinternals.h>",
"#include <R_ext/Rdynload.h>",
"#include <R_ext/Visibility.h>",
"#include <Rversion.h>",
sep = "\n",
file = file,
append = FALSE
)

cat(
"\n\nstatic const R_CallMethodDef CallEntries[] = {",
"", "",
"static const R_CallMethodDef CallEntries[] = {",
" {NULL, NULL, 0}",
"};",
sep = "\n",
file = file,
append = TRUE
)

cat(
paste0("\n\nvoid attribute_visible R_init_", name, "(DllInfo *dll) {"),
"", "",
paste0("void attribute_visible R_init_", name, "(DllInfo *dll) {"),
" // next line is necessary to avoid a NOTE from R CMD check",
" R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);",
" R_useDynamicSymbols(dll, TRUE); // necessary for .onLoad() to work",
"}",
sep = "\n",
file = file,
append = TRUE
"}"
),
con = file
)
return(invisible(NULL))
}
11 changes: 5 additions & 6 deletions R/rstan_create_package.R
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,11 @@ rstan_create_package <- function(path,
message("Configuring Stan compile and module export instructions ...")
rstan_config(pkgdir)

cat(
readLines(.system_file("Read_and_delete_me")), "\n",
file = file.path(pkgdir, "Read-and-delete-me"),
sep = "\n",
append = TRUE
)
# open = "at" implies text is appended
con <- file(file.path(pkgdir, "Read-and-delete-me"), open = "at")
writeLines(readLines(.system_file("Read_and_delete_me")), con)
close(con)

message(
domain = NA,
sprintf(
Expand Down
18 changes: 1 addition & 17 deletions R/rstan_package_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,8 @@
if (acc) {
# create/overwrite file
if (msg) message("Adding ", basename(dest_file), " file ...")
cat(file_lines, sep = "\n", file = dest_file) # can't this fail?
writeLines(file_lines, con = dest_file)
}
}
## acc <- has_file && !is_stanfile # create stan file if acc = TRUE
## if (acc) {
## # non-stan file found: don't overwrite
## if (warn) .warning_nowrite(file.path(basename(pkgdir), ...))
## } else {
## # stan file found: only overwrite if changed
## if (noedit && (file_lines[1] != noedit_msg)) {
## # add "noedit" to top of file
## file_lines <- c(noedit_msg, "", file_lines)
## }
## old_lines <- readLines(dest_file)
## acc <- !identical(old_lines, file_lines)
## if (acc) {
## cat(file_lines, sep = "\n", file = dest_file) # can't this fail?
## }
## }
invisible(acc)
}
5 changes: 0 additions & 5 deletions R/update_description.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@
if(acc) {
if(msg) message("Updating DESCRIPTION ...")
desc_pkg$write()
## cat(desc_pkg$str(normalize = FALSE), "\n",
## file = file.path(pkgdir, "DESCRIPTION"))
## dep_fields <- dep_fields[dep_fields %in% colnames(desc_pkg)]
## write.dcf(desc_pkg, file = file.path(pkgdir, "DESCRIPTION"),
## keep.white = dep_fields)
}
if(!auto_config && msg) {
if(has_rstantools) {
Expand Down
9 changes: 4 additions & 5 deletions R/use_rstan.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,10 @@ use_rstan <- function(pkgdir = ".", license = TRUE, auto_config = TRUE) {
if (.is_generic_namespace(namespc)) {
# automatically update default NAMESPACE
if (msg) message("Updating NAMESPACE ...")
cat("# Generated by roxygen2: fake comment so roxygen2 overwrites silently.",
namespc[!grepl("^#", namespc)],
req_lines,
file = file.path(pkgdir, "NAMESPACE"),
sep = "\n")
writeLines(c("# Generated by roxygen2: fake comment so roxygen2 overwrites silently.",
namespc[!grepl("^#", namespc)],
req_lines),
con = file.path(pkgdir, "NAMESPACE"))
acc <- TRUE # namespace was modified
} else {
# print message stating required dependencies
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/RStanTest/test-rstan_package_skeleton_plus.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ file.copy(from = "test-postsamp.R",
# roxygen2 can't overwrite NAMESPACE unless it created it,
# so trick it into thinking it did
# (and add the rstan stuff or else you get errors...)
cat("# Generated by roxygen2: do not edit by hand",
"",
"import(Rcpp,methods)",
"useDynLib(RStanTest, .registration = TRUE)", sep = "\n",
file = file.path(pkg_path, pkg_name, "NAMESPACE"))
writeLines(c("# Generated by roxygen2: do not edit by hand",
"",
"import(Rcpp,methods)",
"useDynLib(RStanTest, .registration = TRUE)"),
con = file.path(pkg_path, pkg_name, "NAMESPACE"))

roxygen2::roxygenize(pkg_name) # run roxygen2
utils::install.packages(pkg_name, repos = NULL, type = "source")
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-rstan_config.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,16 @@ test_that("Unmodified Stan src files are not overwritten", {

# fake .o files
invisible(sapply(gsub("\\.stan$", "", basename(stan_files)), function(sf) {
cat("fake .o file\n",
file = file.path(pkg_dest_path, "src",
rstantools:::.stan_prefix(sf, ".o")))
writeLines("fake .o file",
con = file.path(pkg_dest_path, "src",
rstantools:::.stan_prefix(sf, ".o")))
}))

# dummy src files
src_files <- c("foo.cpp", "stanExports_foo.txt", "abcstanExports_foo.cc")
invisible(sapply(src_files, function(sf) {
cat("// fake source file\n",
file = file.path(pkg_dest_path, "src", sf))
writeLines("// fake source file\n",
con = file.path(pkg_dest_path, "src", sf))
}))


Expand Down
14 changes: 7 additions & 7 deletions vignettes/developer-guidelines.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ output:
rmarkdown::html_vignette:
toc: true
vignette: >
%\VignetteIndexEntry{Developer Guidelines}
%\VignetteIndexEntry{Guidelines for Developers of R Packages Interfacing with Stan}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
Expand All @@ -17,7 +17,7 @@ One of the coolest things about working on a project like Stan has been seeing
some of our users begin to develop tools for making Stan more accessible to
audiences that may otherwise not benefit from what Stan offers. In particular,
recently we have started seeing a growing number of R packages that provide
high-level interfaces to Stan, using the [__rstan__](https://mc-stan.org/rstan)
high-level interfaces to Stan, using the [__rstan__](https://mc-stan.org/rstan/)
package for estimating models without requiring that the user be familiar with
the Stan modeling language itself.

Expand Down Expand Up @@ -56,7 +56,7 @@ package should be an exception definitely let us know.
which you should use to create the basic structure of your package. (As of
`v2.0.0` this replaces the `rstan_package_skeleton` function.) This will set up
a package with functionality for pre-compiled Stan programs, in the style of the
[__rstanarm__](https://mc-stan.org/rstanarm) package (source code:
[__rstanarm__](https://mc-stan.org/rstanarm/) package (source code:
https://github.com/stan-dev/rstanarm).

* Use version control (e.g., git).
Expand Down Expand Up @@ -171,10 +171,10 @@ decent grammar and actual sentences wherever possible.

### Recommended resources

* Hadley Wickham's [book on R packages](http://r-pkgs.had.co.nz/). If you are
interested in developing an R package that interfaces with Stan but are not an
experienced package developer, we recommend Hadley's book, which is free to read
online. Even if you are an experienced developer of R packages, Hadley's book is
* Hadley Wickham and Jenny Bryan's [book on R packages](https://r-pkgs.org/). If you
are interested in developing an R package that interfaces with Stan but are not an
experienced package developer, we recommend this book, which is free to read
online. Even if you are an experienced developer of R packages, the book is
still a great resource.

* If you need help setting up your package or have questions about these
Expand Down
21 changes: 11 additions & 10 deletions vignettes/minimal-rstan-package.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if(dir.exists(PATH)) {

In this vignette we will walk through the steps necessary for creating an
R package that depends on Stan by creating a package with one function
that fits a simple linear regression. Before continuing, we recommend that you first read the other vignette [_Guidelines for Developers of R Packages Interfacing with Stan_](http://mc-stan.org/rstantools/articles/developer-guidelines.html).
that fits a simple linear regression. Before continuing, we recommend that you first read the other vignette [_Guidelines for Developers of R Packages Interfacing with Stan_](https://mc-stan.org/rstantools/articles/developer-guidelines.html).

## Creating the package skeleton

Expand Down Expand Up @@ -156,7 +156,7 @@ model {
y ~ normal(intercept + beta * x, sigma);
}
"
cat(stan_prog, file = file.path(PATH, "inst", "stan", "lm.stan"))
writeLines(stan_prog, con = file.path(PATH, "inst", "stan", "lm.stan"))
rstan_config(PATH)
```

Expand Down Expand Up @@ -213,7 +213,7 @@ lm_stan <- function(x, y, ...) {
return(out)
}
"
cat(Rcode, file = file.path(PATH, "R", "lm_stan.R"))
writeLines(Rcode, con = file.path(PATH, "R", "lm_stan.R"))
```

When __roxygen2__ documentation is enabled, a top-level package file
Expand All @@ -236,17 +236,18 @@ information about the package.
With __roxygen__ documentation enabled, we need to generate the documentation
for `lm_stan` and update the `NAMESPACE` so the function is exported, i.e.,
available to users when the package is installed. This can be done with the
function `roxygen2::roxygenize()`.
function `roxygen2::roxygenize()`, which needs to be called twice initially.

```{r, results = "hide"}
example(source) # defines the sourceDir() function
```
```{r, eval = FALSE}
try(roxygen2::roxygenize(load_code = roxygen2::load_source), silent = TRUE)
pkgbuild::compile_dll()
try(roxygen2::roxygenize(load_code = sourceDir), silent = TRUE)
roxygen2::roxygenize()
```
```{r, echo=FALSE}
try(roxygen2::roxygenize(PATH, load_code = roxygen2::load_source), silent = TRUE)
try(roxygen2::roxygenize(PATH, load_code = sourceDir), silent = TRUE)
rm(lm_stan)
pkgbuild::compile_dll(PATH)
roxygen2::roxygenize(PATH)
```

Expand Down Expand Up @@ -320,6 +321,6 @@ The following steps are required if one is using `devtools`:

* Ask a question at the [Stan Forums](https://discourse.mc-stan.org/)

* [R packages](http://r-pkgs.had.co.nz/) by Hadley Wickham provides a solid
foundation in R package development as well as the release process.
* [R packages](https://r-pkgs.org/) by Hadley Wickham and Jenny Bryan provides
a solid foundation in R package development as well as the release process.