rr-packager-box

R, box: How do I use an external package like stats?


Suppose getwd() yields "C:/Users/Tom/Documents/Tom_Levers_Git_Repository".

In this repository, I have directory TomLeversRBox.

In this box, I have module calculate_probability.R.

In this module, I have the following function.

#' @export
calculate_probability <- function() {
    print(pnorm(1.644854, 0, 1, lower.tail = TRUE))
}

In RStudio's console, I run install.packages("box").

I run box::use(TomLeversRBox/calculate_probability[calculate_probability]) (What does this expression do?).

I run calculate_probability().

I receive the following error.

Error in pnorm(1.644854, 0, 1, lower.tail = TRUE) : 
   could not find function "pnorm"

How do I use package stats within this box, module, or function to allow pnorm to be found?

Tangential question: When I change module calculate_probability.R, I have to restart my R session. How can I get box to recognize changes to the module file?


Solution

  • As per the FAQ, no core R packages (except ‘base’) are attached inside modules. You’ll need to load (and potentially attach) them explicitly by using e.g.

    box::use(stats[pnorm])
    # or:
    # box::use(stats[...])
    

    inside the module source code.