How to fix package "no item called": pkg "in search list" without using library (pkg)?

I am writing a package called testpkg and have placed quantmod in the Depends section of the DESCRIPTION file.

I wrote the following functions:

#' hello1
#'
#' @return NA
#' @export
hello1 <- function() {
  print("hello1!")
  quantmod::is.HLC("Hello, world!")
}

#' hello2
#'
#' @return NA
#' @export
hello2 <- function () {

    x <- structure(c(25.85, 25.639999, 26.700001, 26.26, 26.92, 27.870001,
                   25.26, 25.52, 26.66, 25.610001, 26.85, 27.74, 26352700, 32512200,
                   64264600, 25.610001, 26.85, 27.74),
                 .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC",
                 src = "yahoo", updated = structure(1437653990.9303, class = c("POSIXct",
                                                                               "POSIXt")),
                 class = c("xts", "zoo"), index = structure(c(1167782400,
                                                              1167868800, 1167955200),
                                                            tzone = "UTC",
                                                            tclass = "Date"),
                 .Dim = c(3L, 6L), .Dimnames = list(NULL, c("YHOO.Open", "YHOO.High", "YHOO.Low",
                                                            "YHOO.Close", "YHOO.Volume", "YHOO.Adjusted")))
    print(x)
    quantmod::chartSeries(x)

}

      

Now when I go to the project and run testpkg::hello1()

I get the expected output.

However, if I run testpkg::hello2()

, I can see what x

is printed, but no graph is generated. I am getting the error:

Error in as.environment("package:quantmod") : 
  no item called "package:quantmod" on the search list

      

I know I can fix this by calling library(quantmod)

before making the call testpkg::hello2()

, but it seems strange to me that it testpkg::hello1()

can work without errors without calling library(quantmod)

. What is the reason for this and is there an alternative way to start testpkg::hello2()

without calling first library(quantmod)

?

+3


source to share





All Articles