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

Find function when only name is provided #416

Merged
merged 6 commits into from May 17, 2019
Merged

Find function when only name is provided #416

merged 6 commits into from May 17, 2019

Conversation

schloerke
Copy link
Collaborator

@schloerke schloerke commented Apr 26, 2019

Use the evaluated function not the raw expression.

Testing Notes

remotes::install_github("trestletech/plumber#416")
p <- callr::r_bg(
  function(){ 
tmp <- tempfile(fileext = ".R")
cat(file = tmp, '
api_msg <- function(msg = "") {
  list(msg = paste0("The message is: \\"", msg, "\\""))
}

# API ----
#* @apiTitle Simple Plumber API

#* Echo back the input
#* @param msg The message to echo
#* @get /echo
api_msg
')

plumber::plumb(tmp)$run(port = 1234)
  }
)

Sys.sleep(1)
x <- httr::GET("127.0.0.1:1234/echo?msg=asdf")
print(httr::content(x))
#> $msg
#> $msg[[1]]
#> [1] "The message is: \"asdf\""
p$kill()

@codecov-io
Copy link

codecov-io commented Apr 26, 2019

Codecov Report

Merging #416 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #416   +/-   ##
=======================================
  Coverage   89.84%   89.84%           
=======================================
  Files          29       29           
  Lines        1527     1527           
=======================================
  Hits         1372     1372           
  Misses        155      155
Impacted Files Coverage Δ
R/plumber-step.R 88.37% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b70993e...0c432b7. Read the comment docs.

@schloerke
Copy link
Collaborator Author

schloerke commented Apr 26, 2019

cc @blairj09

@blairj09
Copy link
Collaborator

blairj09 commented Apr 26, 2019

This works on my end! One thing I noticed given the following example:

# Setup ----
library(plumber)

source("utils.R")
api_msg <- function(msg = "") {
  list(msg = paste0("The message is: '", msg, "'"))
}

# API ----
#* @apiTitle Simple Plumber API

#* Echo back the input
#* @param msg The message to echo
#* @get /utils_echo
utils_msg

#* Echo back the input
#* @param msg The message to echo
#* @get /api_echo
api_msg

Where utils_msg is defined in utils.R. Both endpoints work as expected. However, when I terminate the Plumber API process after running it, utils_msg is persisted in my global environment while api_msg is not. Is this expected?

@schloerke
Copy link
Collaborator Author

schloerke commented Apr 26, 2019

@blairj09 Try adding local = TRUE to the source call.

source("utils.R", local = TRUE)

@schloerke schloerke requested a review from wch Apr 26, 2019
@blairj09
Copy link
Collaborator

blairj09 commented Apr 26, 2019

Ahh that works! This all looks good to me 😄

wch
wch approved these changes May 13, 2019
Copy link
Collaborator

@wch wch left a comment

Add NEWS. Otherwise, looks good.

@schloerke schloerke merged commit 46461b6 into master May 17, 2019
7 checks passed
@schloerke schloerke deleted the plumber_func branch May 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants