library(rlang)
library(purrr)
## 
## Attaching package: 'purrr'
## The following objects are masked from 'package:rlang':
## 
##     %@%, %||%, as_function, flatten, flatten_chr, flatten_dbl,
##     flatten_int, flatten_lgl, invoke, list_along, modify, prepend,
##     rep_along, splice
library(ggplot2)

plot_priors <- function(x, n, asymptote = rbeta(n, 10, 20),
                        midpoint = rnorm(n, 0, 1),
                        scale = abs(rnorm(n, .1, .025))) {

  # stash sampling statements
  asymptote_expr <- enexpr(asymptote)
  midpoint_expr <- enexpr(midpoint)
  scale_expr <- enexpr(scale)

  # evaluate sampling statements with n supplied
  samples <- list(
    sample = seq_len(n),
    asymptote = rlang::eval_tidy(asymptote_expr, list(n = n)),
    midpoint = rlang::eval_tidy(midpoint_expr, list(n = n)),
    scale = rlang::eval_tidy(scale_expr, list(n = n)))

  # compute the nonlinear curve for each sample
  xs <- seq(min(x), max(x), length.out = 80)

  data <- samples %>%
    pmap_dfr(
      function(sample, asymptote, midpoint, scale) {
        tibble::tibble(
          xs = xs,
          ys = asymptote / (1 + exp((midpoint - xs) / scale)),
          sample = sample)
      })

  p <- ggplot(data) +
    aes(xs, ys) +
    geom_line(aes(group = sample))

  print(p)
  invisible(data)
}



# too early
plot_priors(
  x = 0:54,
  n = 40,
  midpoint = rnorm(n, 2, 2),
  scale = rnorm(n, 10, 5))
## Warning: `list_len()` is soft-deprecated as of rlang 0.2.0.
## Please use `new_list()` instead
## This warning is displayed once per session.

# contain negative slopes
plot_priors(
  x = 0:54,
  n = 40,
  midpoint = rnorm(n, 20, 10),
  scale = rnorm(n, 10, 5))

# too steep
plot_priors(
  x = 0:54,
  n = 40,
  midpoint = rnorm(n, 20, 10),
  scale = abs(rnorm(n, 10, 5)))

# y value too low
plot_priors(
  x = 0:54,
  n = 40,
  midpoint = rnorm(n, 20, 10),
  scale = abs(rnorm(n, 18, 5)))

plot_priors(
  x = 0:54,
  n = 40,
  asymptote = rbeta(n, 7, 5),
  midpoint = rnorm(n, 20, 10),
  scale = abs(rnorm(n, 18, 5)))