如何解决在R中,不能在程序包Vignette文件中编织相同的代码无法将“列表”对象强制为整数
此问题与GRATIS软件包中的generate_msts()函数有关。
我添加了一些新内容(使该函数具有将其输出转换为可爱的tsibble格式或保留原始“ list”格式的选项),并准备对CRAN的更新。
我想知道我是否应该整理索引?但是生成的数据似乎没有索引?
output <- if (output_format == "list") {
res #this is output name defined before
} else if (output_format == "tsibble") {
as_tsibble(res)
}
return(output)
}
作为指导,我在 Vignette 中更新了此功能的相应示例。然后事情就变成有线了。
如果我没有保存生成的时间序列输出(例如x 渐晕 无法编织。 (但是,我可以直接在独立的常规RMD文件中使用此功能)
直接使用此代码可以在RStudio内显示输出,但不能编织出来。
my_function(seasonal.periods = c(7,365),n = 800,nComp = 2,output_format="tsibble")
Error in Fun(X[[i]],...): 'list' object cannot be coerced to type 'integer' Calls: <Anonymous>...
as.data.frame -> head -> head.data.frame -> lappy -> FUN Execution halted.
但是,这很好。它可以编织出装饰图案并显示出头部轮廓。
x <- my_function(seasonal.periods = c(7,output_format="tsibble")
head(x)
但是,每次使用前保存起来都是很不方便的。我想知道这是否是因为我在程序包或小插图中使用的任何默认设置都不会更改吗?还是在更改R包中的功能后需要做一些额外的步骤?甚至我需要添加的其他内容是否需要改进?
我尝试devtools::document("C:/Users/mreal/Documents/GitHub/package_name");devtools::install("C:/Users/mreal/Documents/GitHub/package_name")
更新重建功能。但这仍然无助于装饰图案。
我还在rm(list=ls())
中尝试过console
。效果不佳
我在小插图中使用的代码如下
Github链接:
https://github.com/BocongZhao823/gratis/blob/master/vignettes/QuickStart.Rmd
---
title: "Introduction to gratis"
author: "Bocong Zhao"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Introduction to gratis}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
{r initial,echo = FALSE,cache = FALSE,results = 'hide'}
library(knitr)
opts_chunk$set(
warning = FALSE,message = FALSE,echo = TRUE,fig.width = 7,fig.height = 6,fig.align = 'centre',comment = "#>"
)
original <- options("tibble.print_min")
options(tibble.print_min = 5)
# <---- Do stuff with changed option,e.g. print some tibbles ---->
options(tibble.print_min = original)
{r,message=FALSE,include = FALSE}
library(forecast)
library(tsibble)
{r setup}
# load package
library(gratis)
## Generate mutiple seasonal time series
Time series can exhibit multiple seasonal pattern of different length,especially when series observed at a high frequency such as daily or hourly data.
We use function **generate_msts()** to generate mutiple seasonal time series.
**DeFinitions**
Here are the deFinitions of parameter settings in function generate_msts():
|parameter settings | DeFinition|
|:----|:-----|
|seasonal.periods | a vector of seasonal periods of the time series to be generated|
|nComp|number of mixing components when simulating time series using MAR models|
|n |length of the generated time series|
**Example**
Suppose we want to use MAR model to generate a time series with **2** mixing components and the length **800** from random parameter spaces. Particularly,this time series has two seasonal periods **7** and **365**.
{r fig.height = 6,fig.width = 7}
# Generate mutiple seasonal time series with 'tsibble' output format
x <- generate_msts(seasonal.periods = c(7,output_format="tsibble")
head(x)
**Plot time series**
{r fig.height = 6,fig.width = 7}
# Generate mutiple seasonal time series with 'list' output format
x <- generate_msts(seasonal.periods = c(7,output_format="list")
autoplot(x)
** Github链接**
https://github.com/BocongZhao823/gratis/blob/master/R/generate_ts.R
#' Generate mutiple seasonal time series from random parameter spaces of the mixture autoregressive (MAR) models.
#'
#' Generate mutiple seasonal time series from random parameter spaces of the mixture autoregressive (MAR) models.
#' @param seasonal.periods a vector of seasonal periods of the time series to be generated.
#' @param n length of the generated time series.
#' @param nComp number of mixing components when simulating time series using MAR models.
#' @param output_format An optional argument which allows to choose output format between "list" and "tsibble"
#' @return a time series with multiple seasonal periods.
#' @export
#' @examples
#' x <- generate_msts(seasonal.periods = c(7,output_format= "list")
#' forecast::autoplot(x)
generate_msts <- function(seasonal.periods = c(7,nComp = NULL,output_format="list") {
x.list <- map(seasonal.periods,function(p) {
generate_ts(n.ts = 1,freq = p,n = n,nComp = nComp)$N1$x
})
names(x.list) <- paste0("Season",seasonal.periods)
x.list[1:(length(x.list) - 1)] <- lapply(x.list[1:(length(x.list) - 1)],function(x) {
x - trendcycle(stl(x,"per"))
})
weights <- msts_weights(length(seasonal.periods))
res <- as_tibble(scale(x.list %>% bind_cols())[,]) %>%
mapply("*",.,weights) %>%
as_tibble() %>%
mutate(x = rowSums(.)) %>%
select(x) %>%
msts(seasonal.periods = seasonal.periods)
# New content
output <- if (output_format == "list") {
res
} else if (output_format == "tsibble") {
as_tsibble(res)
}
return(output)
}
# ===========================================================================
# Simulated weights for the simulation of msts
# ===========================================================================
msts_weights <- function(n.periods) {
gamma <- runif(n.periods,0)
weights <- gamma / sum(gamma)
return(weights)
}
解决方法
我尝试为您运行此操作-我的第一个猜测是NAMESPACE问题。但这似乎也与generate_msts()
函数有关。
我真的认为这与先将其保存到变量x
无关。
这是我的发现:
不起作用:
x <- generate_msts(seasonal.periods = c(7,365),n = 800,nComp = 2,output_format="tsibble")
x
不起作用:
print(generate_msts(seasonal.periods = c(7,output_format="tsibble"))
不起作用:
x <- generate_msts(seasonal.periods = c(7,output_format="tsibble")
print(x)
作品:
head(generate_msts(seasonal.periods = c(7,output_format="tsibble"))
在失败的情况下,它始终是与您相同的错误消息:
错误:处理小插图'QuickStart.Rmd'失败,并显示以下诊断信息: 'list'对象不能强制输入'integer'
因此,由于head()
,str()
,class()
一直为我工作,并且只有print()
无效,我假设这是一个打印功能出现问题。因此,将其保存到变量x
中的解决方法只能正常工作,因为您没有调用print函数。
同样重要的是仅在Rmarkdown
内使用generate_msts()时问题才发生。正如我稍后解释的那样,这似乎是合理的,因为在knitr中进行打印不同于在控制台上进行打印。
当我更改您的generate_msts()
并重建软件包时:
output <- if (output_format == "list") {
res
} else if (output_format == "tsibble") {
tsibble(date = as.Date("2017-01-01") + 0:9,value = rnorm(10))
}
Rmarkdown突然运行而没有错误。
我猜想 print()可能是您与knitr交互时使用的特定数据的问题。
在knitr中进行打印似乎与在控制台上进行打印有所不同(这可能是为什么不使用rmarkdown就能进行打印的原因)
以下是有关自定义打印方法和编织的一个不错的链接: https://cran.r-project.org/web/packages/knitr/vignettes/knit_print.html
在knitr v1.6之前,以R代码块打印对象基本上是模仿R控制台。
我可以想象到tsibble包中用于knit_print的S3方法(它仅使用了tibble的所有打印方法?)可能不适用于您的特定数据集(我的意思是它适用于用{{1 }})。但是,只是(一个疯狂的?)猜测...总体上的错误和行为真的很奇怪...
编辑: 这也是该错误的R Markdown调用堆栈:
tsibble()
应该与您相似,但是如果您想独立获取,则必须在rmarkdown文档中使用以下命令
1. ├─base::print(x)
2. └─tibble:::print.tbl(x)
3. ├─cli::cat_line(format(x,...,n = n,width = width,n_extra = n_extra))
4. │ └─base::paste0(...,collapse = "\n")
5. ├─base::format(x,n_extra = n_extra)
6. └─tsibble:::format.tbl_ts(x,n_extra = n_extra)
7. ├─base::format(trunc_mat(x,n_extra = n_extra))
8. └─tibble::trunc_mat(x,n_extra = n_extra)
9. ├─base::as.data.frame(head(x,n))
10. ├─utils::head(x,n)
11. └─utils:::head.data.frame(x,n)
12. └─base::lapply(...)
13. └─utils:::FUN(X[[i]],...)
但是正如您在调用堆栈中看到的那样,该错误是由base :: print(x)引起的,它调用S3方法tibble ::: print.tbl(x),然后此方法内部调用tsibble ::: format.tbl_ts,它调用tibble :: trunc_mat,...并导致错误。
好吧。。。接下来我会继续做这些事情……最终这些函数调用中的混乱是您在开始时设置的knitr选项。
您在rmarkdown的开头写道:
options(rlang_trace_top_env = rlang::current_env())
options(error = function() {
sink()
print(rlang::trace_back(bottom = sys.frame(-1)),simplify = "none")
})
将其更改为:
original <- options("tibble.print_min")
options(tibble.print_min = 5)
# <---- Do stuff with changed option,e.g. print some tibbles ---->
options(tibble.print_min = original)
那应该工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。