如何在不使用库(pkg)的情况下修复“搜索列表中没有名为“ package:pkg”的项目”?

mau*_*una 5 debugging r

我正在编写一个名为testpkg的程序包,并将quantmod放在DESCRIPTION文件的Depends部分中。

我编写了以下功能:

#' 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)

}
Run Code Online (Sandbox Code Playgroud)

现在,当我进入一个项目并运行时testpkg::hello1(),我得到了预期的输出。

但是,如果运行testpkg::hello2(),我可以看到x已打印但未生成图。我收到错误:

Error in as.environment("package:quantmod") : 
  no item called "package:quantmod" on the search list
Run Code Online (Sandbox Code Playgroud)

我知道我可以通过在致电library(quantmod)之前致电来解决此问题,testpkg::hello2()但对我来说奇怪的是,testpkg::hello1()无需致电就可以无错误地运行library(quantmod)。这是什么原因,是否有一种testpkg::hello2()无需library(quantmod)先调用就可以运行的替代方法?