rstudio / plumber

Turn your R code into a web API.
https://www.rplumber.io
Other
1.39k stars 256 forks source link

`source()` to include extra files doesn't work #847

Open meowcat opened 2 years ago

meowcat commented 2 years ago

System details

Output of sessioninfo::session_info()():

- Session info  ------------------------------------------------------------------------------------------------------
 hash: prince: medium-light skin tone, man teacher: medium-light skin tone, woman and man holding hands: medium-dark skin tone, light skin tone

 setting  value
 version  R version 4.1.2 (2021-11-01)
 os       Windows 10 x64 (build 19042)
 system   x86_64, mingw32
 ui       RStudio
 language (EN)
 collate  English_United States.1252
 ctype    English_United States.1252
 tz       Europe/Berlin
 date     2021-12-17
 rstudio  2021.09.0+351 Ghost Orchid (desktop)
 pandoc   NA

- Packages -----------------------------------------------------------------------------------------------------------
 package     * version date (UTC) lib source
 cli           3.1.0   2021-10-27 [1] CRAN (R 4.1.1)
 ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.1.1)
 httpuv        1.6.3   2021-09-09 [1] CRAN (R 4.1.1)
 jsonlite      1.7.2   2020-12-09 [1] CRAN (R 4.1.1)
 later         1.3.0   2021-08-18 [1] CRAN (R 4.1.1)
 lifecycle     1.0.1   2021-09-24 [1] CRAN (R 4.1.1)
 magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.1.1)
 plumber       1.1.0   2021-03-24 [1] CRAN (R 4.1.2)
 promises      1.2.0.1 2021-02-11 [1] CRAN (R 4.1.1)
 R6            2.5.1   2021-08-19 [1] CRAN (R 4.1.1)
 Rcpp          1.0.7   2021-07-07 [1] CRAN (R 4.1.1)
 rlang         0.4.12  2021-10-18 [1] CRAN (R 4.1.1)
 sessioninfo   1.2.1   2021-11-02 [1] CRAN (R 4.1.2)
 stringi       1.7.5   2021-10-04 [1] CRAN (R 4.1.1)
 swagger       3.33.1  2020-10-02 [1] CRAN (R 4.1.2)
 webutils      1.1     2020-04-28 [1] CRAN (R 4.1.2)

 [1] C:/Archives_Libraries/R/R-4.1.2
 [2] C:/Program Files/R/R-4.1.2/library

----------------------------------------------------------------------------------------------------------------------

Example application or steps to reproduce the problem

plumber.R

source("api2.R")

#* @get /test
function() {
  return("1")
}

api2.r

#* @get /test2
function() {
  return("2")
}

Run

plumber::plumb(file='R/plumber.R')$run()

Swagger only shows /test but not /test2; also the query to /test2 fails.

Describe the problem in detail

Plumber doesn't follow into api2.R to find annotation entries there, so I would instead have to have everything in the same file. I would like to split up my api into multiple files. This is really for legibility and not for some stricter isolation purposes; in particular, there is some global state involved in practice.

schloerke commented 2 years ago

This should work. source()ing only does a standard R source on the files. We need to plumb the files to get its contents ready for plumber.

plumber.R

api2_pr <- plumber::plumb("api2.R")

#* @get /test
function() {
  return("1")
}

#* @plumber
function(pr) {
  plumber::pr_mount(pr, "/", api2_pr)
}