2017-05-31 61 views
0

我编写了一个R包,其中包含使用xts对象的S4类。这是我的理解,我需要包括行setOldClass(“xts”)导致自定义R包中的问题

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") 
) 

在我的类定义脚本的顶部。当我使用我的包创建我的对象时,会发生问题。它的工作原理,但我偶尔得到这样的警告反复几次:

Found more than one class "xts" in cache; using the first, from namespace 'quantmod' 

我怎么能强迫我的班,而不是使用默认为quantmod包正确xts包?如果不这样做,我该如何至少停止警告?

我注意到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) 

而我的描述文件:

Package: cree 
Title: CI Risk Engine, Eh? 
Version: 0.0.0.9000 
[email protected]: person("Mr", "lebelinoz", email = "[email protected]", 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' 
+0

你有没有尝试使用'::'运算符?例如:'xts ::'以避免冲突? – ar7

+0

@ ar7是的我有,但它会导致编译错误。我已经添加了示例类,以帮助澄清我的问题。 – lebelinoz

+0

您的NAMESPACE和DESCRIPTION文件也是相关的。您的软件包源代码是否公开? –

回答

2

的问题是,你的空间文件不包含任何进口。在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和动物园我没有得到任何消息。我的包只包含下列文件:

> 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 

描述的内容是:

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 <[email protected]> 
Description: More about what it does (maybe more than one line) 
License: What license is it under? 
Imports: quantmod, xts, zoo 

命名空间的内容如下。请注意,我只是从3个列出的软件包中导入所有内容以进行测试。有选择地只导入您使用的功能是更好的做法。如果您的NAMESPACE文件中没有import()命令,则可以使用R CMD check来告诉您需要从每个包中导入哪些功能。

经验教训:R CMD check即使您不打算将您的软件包提交给CRAN也很有用。 ;-)

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") 
) 

而且人/ 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 ~~ 
} 
+0

谢谢。有没有修改NAMESPACE的'roxygen2'方法?我的NAMESPACE文件的第一行是'#由roxygen2生成:不用手编辑' – lebelinoz

+1

我想我找到了答案。我需要在我的'roxygen2'注释中使用'@ import'和'@ importFrom'标记,如https://stackoverflow.com/questions/10325231/when-writing-my-own-r-package-i-似乎可以得到其他软件包到进口corr/10325867#10325867 – lebelinoz

+1

@lebelinoz:是的,我鼓励你使用'@ importFrom'而不是拉入整个软件包。 –