@@ -3,26 +3,36 @@ MAKE <- Sys.getenv("MAKE", "make")
33
44
55verify_tinystan_path <- function (path ) {
6- suppressWarnings({
7- folder <- normalizePath(path )
8- })
9- if (! dir.exists(folder )) {
10- stop(paste0(" TinyStan folder '" , folder , " ' does not exist!\n " , " If you need to set a different location, call 'set_tinystan_path()'" ))
11- }
12- makefile <- file.path(folder , " Makefile" )
13- if (! file.exists(makefile )) {
14- stop(paste0(" TinyStan folder '" , folder , " ' does not contain file 'Makefile'," ,
15- " please ensure it is built properly!\n " , " If you need to set a different location, call 'set_tinystan_path()'" ))
16- }
6+ suppressWarnings({
7+ folder <- normalizePath(path )
8+ })
9+ if (! dir.exists(folder )) {
10+ stop(paste0(
11+ " TinyStan folder '" ,
12+ folder ,
13+ " ' does not exist!\n " ,
14+ " If you need to set a different location, call 'set_tinystan_path()'"
15+ ))
16+ }
17+ makefile <- file.path(folder , " Makefile" )
18+ if (! file.exists(makefile )) {
19+ stop(paste0(
20+ " TinyStan folder '" ,
21+ folder ,
22+ " ' does not contain file 'Makefile'," ,
23+ " please ensure it is built properly!\n " ,
24+ " If you need to set a different location, call 'set_tinystan_path()'"
25+ ))
26+ }
1727}
1828
1929# ' @title Function `set_tinystan_path()`
2030# ' @description Set the path to TinyStan.
2131# ' @details This should point to the top-level folder of the repository.
2232# ' @export
2333set_tinystan_path <- function (path ) {
24- verify_tinystan_path(path )
25- Sys.setenv(TINYSTAN = normalizePath(path ))
34+ verify_tinystan_path(path )
35+ Sys.setenv(TINYSTAN = normalizePath(path ))
2636}
2737
2838# ' Get the path to TinyStan.
@@ -36,21 +46,28 @@ set_tinystan_path <- function(path) {
3646# '
3747# ' @seealso [set_tinystan_path]
3848get_tinystan_path <- function () {
39- # try to get from environment
40- path <- Sys.getenv(" TINYSTAN" , unset = " " )
41- if (path == " " ) {
42- path <- CURRENT_TINYSTAN
43- tryCatch({
44- verify_tinystan_path(path )
45- }, error = function (e ) {
46- print(paste0(" TinyStan not found at location specified by $TINYSTAN " ,
47- " environment variable, downloading version " , packageVersion(" tinystan" ),
48- " to " , path ))
49- get_tinystan_src()
50- })
51- }
49+ # try to get from environment
50+ path <- Sys.getenv(" TINYSTAN" , unset = " " )
51+ if (path == " " ) {
52+ path <- CURRENT_TINYSTAN
53+ tryCatch(
54+ {
55+ verify_tinystan_path(path )
56+ },
57+ error = function (e ) {
58+ print(paste0(
59+ " TinyStan not found at location specified by $TINYSTAN " ,
60+ " environment variable, downloading version " ,
61+ packageVersion(" tinystan" ),
62+ " to " ,
63+ path
64+ ))
65+ get_tinystan_src()
66+ }
67+ )
68+ }
5269
53- return (path )
70+ return (path )
5471}
5572
5673
@@ -73,52 +90,67 @@ get_tinystan_path <- function() {
7390# ' @seealso [tinystan::set_tinystan_path()]
7491# ' @export
7592compile_model <- function (stan_file , stanc_args = NULL , make_args = NULL ) {
76- verify_tinystan_path(get_tinystan_path())
77- suppressWarnings({
78- file_path <- normalizePath(stan_file )
79- })
80- if (tools :: file_ext(file_path ) != " stan" ) {
81- stop(paste0(" File '" , file_path , " ' does not end with '.stan'" ))
82- }
83- if (! file.exists(file_path )) {
84- stop(paste0(" File '" , file_path , " ' does not exist!" ))
85- }
93+ verify_tinystan_path(get_tinystan_path())
94+ suppressWarnings({
95+ file_path <- normalizePath(stan_file )
96+ })
97+ if (tools :: file_ext(file_path ) != " stan" ) {
98+ stop(paste0(" File '" , file_path , " ' does not end with '.stan'" ))
99+ }
100+ if (! file.exists(file_path )) {
101+ stop(paste0(" File '" , file_path , " ' does not exist!" ))
102+ }
86103
87- output <- paste0(tools :: file_path_sans_ext(file_path ), " _model.so" )
88- stancflags <- paste(" --include-paths=." , paste(stanc_args , collapse = " " ))
104+ output <- paste0(tools :: file_path_sans_ext(file_path ), " _model.so" )
105+ stancflags <- paste(" --include-paths=." , paste(stanc_args , collapse = " " ))
89106
90- flags <- c(paste(" -C" , get_tinystan_path()), make_args , paste0(" STANCFLAGS=\" " ,
91- stancflags , " \" " ), output )
107+ flags <- c(
108+ paste(" -C" , get_tinystan_path()),
109+ make_args ,
110+ paste0(" STANCFLAGS=\" " , stancflags , " \" " ),
111+ output
112+ )
92113
93- suppressWarnings({
94- res <- system2(MAKE , args = flags , stdout = TRUE , stderr = TRUE )
95- })
96- res_attrs <- attributes(res )
97- if (" status" %in% names(res_attrs ) && res_attrs $ status != 0 ) {
98- stop(paste0(" Compilation failed with error code " , res_attrs $ status , " \n output:\n " ,
99- paste(res , collapse = " \n " )))
100- }
114+ suppressWarnings({
115+ res <- system2(MAKE , args = flags , stdout = TRUE , stderr = TRUE )
116+ })
117+ res_attrs <- attributes(res )
118+ if (" status" %in% names(res_attrs ) && res_attrs $ status != 0 ) {
119+ stop(paste0(
120+ " Compilation failed with error code " ,
121+ res_attrs $ status ,
122+ " \n output:\n " ,
123+ paste(res , collapse = " \n " )
124+ ))
125+ }
101126
102- return (output )
127+ return (output )
103128}
104129
105130tbb_found <- function () {
106- suppressWarnings(out <- system2(" where.exe" , " tbb.dll" , stdout = NULL , stderr = NULL ))
107- return (out == 0 )
131+ suppressWarnings(
132+ out <- system2(" where.exe" , " tbb.dll" , stdout = NULL , stderr = NULL )
133+ )
134+ return (out == 0 )
108135}
109136
110137WINDOWS_PATH_SET <- FALSE
111138
112139windows_dll_path_setup <- function () {
113- if (.Platform $ OS.type == " windows" && ! WINDOWS_PATH_SET ) {
114-
115- if (tbb_found()) {
116- assign(" WINDOWS_PATH_SET" , TRUE , envir = .GlobalEnv )
117- } else {
118- tbb_path <- file.path(get_tinystan_path(), " stan" , " lib" , " stan_math" ,
119- " lib" , " tbb" )
120- Sys.setenv(PATH = paste(tbb_path , Sys.getenv(" PATH" ), sep = " ;" ))
121- assign(" WINDOWS_PATH_SET" , tbb_found(), envir = .GlobalEnv )
122- }
140+ if (.Platform $ OS.type == " windows" && ! WINDOWS_PATH_SET ) {
141+ if (tbb_found()) {
142+ assign(" WINDOWS_PATH_SET" , TRUE , envir = .GlobalEnv )
143+ } else {
144+ tbb_path <- file.path(
145+ get_tinystan_path(),
146+ " stan" ,
147+ " lib" ,
148+ " stan_math" ,
149+ " lib" ,
150+ " tbb"
151+ )
152+ Sys.setenv(PATH = paste(tbb_path , Sys.getenv(" PATH" ), sep = " ;" ))
153+ assign(" WINDOWS_PATH_SET" , tbb_found(), envir = .GlobalEnv )
123154 }
155+ }
124156}
0 commit comments