setOldClass("xts") 在自定义 R 包中导致问题
setOldClass("xts") causing problems in a custom R package
我写了一个 R 包,其中包含一个使用 xts
对象的 S4 class。据我了解,我需要包含行
setOldClass("xts")
stock.returns <- setClass(
Class = "stock.returns",
slots = c(xts_returns = "xts", timeframe = "timeframe", currency = "character"),
prototype = prototype(xts_returns = xts::xts(, order.by = c(lubridate::today())), timeframe = timeframe(), currency = "Local")
)
在我的 class 定义脚本的顶部。当我使用我的包创建我的对象时出现问题。它有效,但我偶尔 多次收到此警告:
Found more than one class "xts" in cache; using the first, from namespace 'quantmod'
如何强制我的 class 使用正确的 xts
包而不是默认使用 quantmod
包? 如果失败,我至少要如何停止警告?
我注意到 Hadley Wickham 和另一个人之间关于同一主题的对话:http://r.789695.n4.nabble.com/setOldClass-quot-xts-quot-td4714332.html。它哪儿也去不了。
编辑: 我的命名空间文件是
# Generated by roxygen2: do not edit by hand
export("%>%")
export(EOMonth)
export(EOWeek)
export(VaR.cree)
export(date_to_sql_string)
export(df_to_xts)
export(ewma)
export(factor_model_maker)
export(flatten_xts)
export(get_USD_fx)
export(get_benchmark_index)
export(get_benchmark_xts_returns)
export(get_bond_index)
export(get_currency_index)
export(get_end_date)
export(get_financial_history)
export(get_financial_history_and_make_it_monthly)
export(get_frequency)
export(get_fund_performance)
export(get_fx_USD_history)
export(get_fx_cross)
export(get_index_snapshot)
export(get_metrics_history)
export(get_portfolio)
export(get_price_history)
export(get_shock_results)
export(get_shock_results.yield_version)
export(get_spc_xts_raw_total_returns)
export(get_spc_xts_returns)
export(get_start_date)
export(get_table_from_sql_CISMPRDSVR)
export(get_test_date)
export(get_ticker_xts_return_index)
export(get_ticker_xts_t_data_fs_eps)
export(get_watchlist)
export(get_yield_index)
export(market_capture_ratio)
export(previous_business_date_if_weekend)
export(price_plot)
export(rel_plot)
export(return_over_horizon)
export(rolling_correlation)
export(run_sql_in_CISMPRDSVR)
export(scenario_analysis)
export(show_regression)
export(single_experiment_summary)
export(stock.returns)
export(timeframe)
export(update_fund_performance_from_spreadsheet)
export(write.zoo)
export(xts_add_average_series)
export(xts_price_from_returns)
export(xts_returns)
exportClasses(timeframe)
我的 DESCRIPTION 文件是:
Package: cree
Title: CI Risk Engine, Eh?
Version: 0.0.0.9000
Authors@R: person("Mr", "lebelinoz", email = "lebelinoz@mycompany.com", role = c("aut", "cre"))
Description: All the in-house risk tools built in R.
Depends: R (>= 3.3.1)
License: Proprietary. Do not distribute outside My Company Limited.
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.0.1
Imports:
tidyverse,
lubridate,
quantmod,
xts,
zoo,
RODBC,
PerformanceAnalytics
Collate:
'Functions.R'
'Sql_Wrapper.R'
'StockPlot.R'
'VaR.cree.R'
'ewma.R'
'timeframe.R'
'stock.returns.R'
'factor_model_maker.R'
'get_benchmark_index.R'
'get_bond_index.R'
'get_currency_index.R'
'get_financial_history.R'
'get_fx_.R'
'get_metrics_history.R'
'get_portfolio.R'
'get_price_history.R'
'get_shock_results.R'
'get_shock_results.yield_version.R'
'get_yield_index.R'
'market_capture_ratio.R'
'return_over_horizon.R'
'scenario_analysis.R'
'show_regression.R'
'show_regression_between_xts.R'
'single_experiment_summary.R'
'update_fund_performance_from_spreadsheet.R'
问题是您的 NAMESPACE 文件不包含任何导入。在 DESCRIPTION 文件中指定 Imports 仅表示您的包从列出的包中导入内容。您还必须在 NAMESPACE 文件中显式导入特定函数。
使用框架包,我可以使用以下命令复制消息:
library(quantmod)
stock_returns = anRpackage::stock.returns()@xts_returns
economic.factor.model <- setClass("economic.factor.model",
slots = c(stock_returns = "xts", factor_premiums = "xts"))
但是如果我导入 quantmod、xts 和 zoo,我没有收到任何消息。我的包裹只包含以下文件:
> ls -l anRpackage/*
-rw-rw-r-- 1 josh josh 311 Jun 21 05:35 anRpackage/DESCRIPTION
-rw-rw-r-- 1 josh josh 75 Jun 21 05:55 anRpackage/NAMESPACE
anRpackage/man:
total 4
-rw-rw-r-- 1 josh josh 713 Jun 21 05:38 anRpackage-package.Rd
anRpackage/R:
total 4
-rw-rw-r-- 1 josh josh 364 Jun 21 05:40 file.R
DESCRIPTION的内容是:
Package: anRpackage
Type: Package
Title: What the package does (short line)
Version: 1.0
Date: 2017-06-21
Author: Who wrote it
Maintainer: Who to complain to <yourfault@somewhere.net>
Description: More about what it does (maybe more than one line)
License: What license is it under?
Imports: quantmod, xts, zoo
NAMESPACE的内容如下。请注意,出于测试目的,我只是从列出的 3 个包中导入所有内容。更好的做法是有选择地只导入您使用的函数。如果您的 NAMESPACE 文件中没有 import()
命令,您可以使用 R CMD check
来告诉您需要从每个包中导入哪些函数。
经验教训:即使您不打算将包裹提交给 CRAN,R CMD check
也很有用。 ;-)
exportPattern("^[[:alpha:]]+")
import(quantmod)
import(xts)
import(zoo)
R/file.R的内容是:
setOldClass("xts")
stock.returns <- setClass(
Class = "stock.returns",
slots = c(xts_returns = "xts",
timeframe = "character",
currency = "character"),
prototype = prototype(xts_returns = xts::xts(, order.by = Sys.Date()),
timeframe = "timeframe()",
currency = "Local")
)
而man/anRpackage-package.rd的内容是:
\name{anRpackage-package}
\alias{anRpackage-package}
\alias{anRpackage}
\docType{package}
\title{
\packageTitle{anRpackage}
}
\description{
\packageDescription{anRpackage}
}
\details{
The DESCRIPTION file:
\packageDESCRIPTION{anRpackage}
\packageIndices{anRpackage}
~~ An overview of how to use the package, including the most important ~~
~~ functions ~~
}
\author{
\packageAuthor{anRpackage}
Maintainer: \packageMaintainer{anRpackage}
}
\references{
~~ Literature or other references for background information ~~
}
\keyword{ package }
\seealso{
~~ Optional links to other man pages, e.g. ~~
~~ \code{\link[<pkg>:<pkg>-package]{<pkg>}} ~~
}
\examples{
#~~ simple examples of the most important functions ~~
}
我写了一个 R 包,其中包含一个使用 xts
对象的 S4 class。据我了解,我需要包含行
setOldClass("xts")
stock.returns <- setClass(
Class = "stock.returns",
slots = c(xts_returns = "xts", timeframe = "timeframe", currency = "character"),
prototype = prototype(xts_returns = xts::xts(, order.by = c(lubridate::today())), timeframe = timeframe(), currency = "Local")
)
在我的 class 定义脚本的顶部。当我使用我的包创建我的对象时出现问题。它有效,但我偶尔 多次收到此警告:
Found more than one class "xts" in cache; using the first, from namespace 'quantmod'
如何强制我的 class 使用正确的 xts
包而不是默认使用 quantmod
包? 如果失败,我至少要如何停止警告?
我注意到 Hadley Wickham 和另一个人之间关于同一主题的对话:http://r.789695.n4.nabble.com/setOldClass-quot-xts-quot-td4714332.html。它哪儿也去不了。
编辑: 我的命名空间文件是
# Generated by roxygen2: do not edit by hand
export("%>%")
export(EOMonth)
export(EOWeek)
export(VaR.cree)
export(date_to_sql_string)
export(df_to_xts)
export(ewma)
export(factor_model_maker)
export(flatten_xts)
export(get_USD_fx)
export(get_benchmark_index)
export(get_benchmark_xts_returns)
export(get_bond_index)
export(get_currency_index)
export(get_end_date)
export(get_financial_history)
export(get_financial_history_and_make_it_monthly)
export(get_frequency)
export(get_fund_performance)
export(get_fx_USD_history)
export(get_fx_cross)
export(get_index_snapshot)
export(get_metrics_history)
export(get_portfolio)
export(get_price_history)
export(get_shock_results)
export(get_shock_results.yield_version)
export(get_spc_xts_raw_total_returns)
export(get_spc_xts_returns)
export(get_start_date)
export(get_table_from_sql_CISMPRDSVR)
export(get_test_date)
export(get_ticker_xts_return_index)
export(get_ticker_xts_t_data_fs_eps)
export(get_watchlist)
export(get_yield_index)
export(market_capture_ratio)
export(previous_business_date_if_weekend)
export(price_plot)
export(rel_plot)
export(return_over_horizon)
export(rolling_correlation)
export(run_sql_in_CISMPRDSVR)
export(scenario_analysis)
export(show_regression)
export(single_experiment_summary)
export(stock.returns)
export(timeframe)
export(update_fund_performance_from_spreadsheet)
export(write.zoo)
export(xts_add_average_series)
export(xts_price_from_returns)
export(xts_returns)
exportClasses(timeframe)
我的 DESCRIPTION 文件是:
Package: cree
Title: CI Risk Engine, Eh?
Version: 0.0.0.9000
Authors@R: person("Mr", "lebelinoz", email = "lebelinoz@mycompany.com", role = c("aut", "cre"))
Description: All the in-house risk tools built in R.
Depends: R (>= 3.3.1)
License: Proprietary. Do not distribute outside My Company Limited.
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.0.1
Imports:
tidyverse,
lubridate,
quantmod,
xts,
zoo,
RODBC,
PerformanceAnalytics
Collate:
'Functions.R'
'Sql_Wrapper.R'
'StockPlot.R'
'VaR.cree.R'
'ewma.R'
'timeframe.R'
'stock.returns.R'
'factor_model_maker.R'
'get_benchmark_index.R'
'get_bond_index.R'
'get_currency_index.R'
'get_financial_history.R'
'get_fx_.R'
'get_metrics_history.R'
'get_portfolio.R'
'get_price_history.R'
'get_shock_results.R'
'get_shock_results.yield_version.R'
'get_yield_index.R'
'market_capture_ratio.R'
'return_over_horizon.R'
'scenario_analysis.R'
'show_regression.R'
'show_regression_between_xts.R'
'single_experiment_summary.R'
'update_fund_performance_from_spreadsheet.R'
问题是您的 NAMESPACE 文件不包含任何导入。在 DESCRIPTION 文件中指定 Imports 仅表示您的包从列出的包中导入内容。您还必须在 NAMESPACE 文件中显式导入特定函数。
使用框架包,我可以使用以下命令复制消息:
library(quantmod)
stock_returns = anRpackage::stock.returns()@xts_returns
economic.factor.model <- setClass("economic.factor.model",
slots = c(stock_returns = "xts", factor_premiums = "xts"))
但是如果我导入 quantmod、xts 和 zoo,我没有收到任何消息。我的包裹只包含以下文件:
> ls -l anRpackage/*
-rw-rw-r-- 1 josh josh 311 Jun 21 05:35 anRpackage/DESCRIPTION
-rw-rw-r-- 1 josh josh 75 Jun 21 05:55 anRpackage/NAMESPACE
anRpackage/man:
total 4
-rw-rw-r-- 1 josh josh 713 Jun 21 05:38 anRpackage-package.Rd
anRpackage/R:
total 4
-rw-rw-r-- 1 josh josh 364 Jun 21 05:40 file.R
DESCRIPTION的内容是:
Package: anRpackage
Type: Package
Title: What the package does (short line)
Version: 1.0
Date: 2017-06-21
Author: Who wrote it
Maintainer: Who to complain to <yourfault@somewhere.net>
Description: More about what it does (maybe more than one line)
License: What license is it under?
Imports: quantmod, xts, zoo
NAMESPACE的内容如下。请注意,出于测试目的,我只是从列出的 3 个包中导入所有内容。更好的做法是有选择地只导入您使用的函数。如果您的 NAMESPACE 文件中没有 import()
命令,您可以使用 R CMD check
来告诉您需要从每个包中导入哪些函数。
经验教训:即使您不打算将包裹提交给 CRAN,R CMD check
也很有用。 ;-)
exportPattern("^[[:alpha:]]+")
import(quantmod)
import(xts)
import(zoo)
R/file.R的内容是:
setOldClass("xts")
stock.returns <- setClass(
Class = "stock.returns",
slots = c(xts_returns = "xts",
timeframe = "character",
currency = "character"),
prototype = prototype(xts_returns = xts::xts(, order.by = Sys.Date()),
timeframe = "timeframe()",
currency = "Local")
)
而man/anRpackage-package.rd的内容是:
\name{anRpackage-package}
\alias{anRpackage-package}
\alias{anRpackage}
\docType{package}
\title{
\packageTitle{anRpackage}
}
\description{
\packageDescription{anRpackage}
}
\details{
The DESCRIPTION file:
\packageDESCRIPTION{anRpackage}
\packageIndices{anRpackage}
~~ An overview of how to use the package, including the most important ~~
~~ functions ~~
}
\author{
\packageAuthor{anRpackage}
Maintainer: \packageMaintainer{anRpackage}
}
\references{
~~ Literature or other references for background information ~~
}
\keyword{ package }
\seealso{
~~ Optional links to other man pages, e.g. ~~
~~ \code{\link[<pkg>:<pkg>-package]{<pkg>}} ~~
}
\examples{
#~~ simple examples of the most important functions ~~
}