Home > Enterprise >  How do I use an external package like stats?
How do I use an external package like stats?

Time:09-17

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?

CodePudding user response:

I think what you are missing is to add #' @export before the calculate_probability function in your module.
In the next step you can call the function as name_of_module$name_of_function you can read more on how the package works here

CodePudding user response:

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.

  • Related