简体   繁体   English

编织为HTML时出现代码R Markdown错误

[英]R Markdown error in code when knit to HTML

I am trying to run code chunks in my markdown document. 我试图在我的降价文档中运行代码块。 I have an R script that runs all the code that I need without any issues. 我有一个R脚本,可以运行我需要的所有代码而没有任何问题。 Then, when I copy and paste the code into the markdown document, the code will run within the chunk, but will fail when trying to knit into an output document (html/pdf). 然后,当我将代码复制并粘贴到markdown文档中时,该代码将在块内运行,但是在尝试编织为输出文档(html / pdf)时将失败。

I had to create a safe.ifelse function to prevent r from converting my dates to a numeric format as discussed here . 我必须创建一个safe.ifelse函数来防止r将日期转换为此处讨论的数字格式。

The error appears to be with the code: 该错误似乎与以下代码有关:

safe.ifelse = function(cond, yes, no){structure(ifelse(cond, yes, no), class = class(yes))
}

The error message I get is: 我收到的错误消息是:

Line 121 Error in structure(ifelse(cond,yes,no), class = class(yes)) : could not find function "days" Calls: ... transform.data.frame ->eval->eval-> safe.ifelse-> structure Execution halted 第121行结构错误(ifelse(cond,yes,no),class = class(yes)):找不到函数“ days”调用:... transform.data.frame-> eval-> eval->安全。 ifelse->结构执行停止

The line of code following my safe.ifelse function is 我的safe.ifelse函数后面的代码行是

seminoma1 = transform(seminoma1, recur.date = safe.ifelse(salvage.tx=="Yes",
date.diagnosis + days(pmax(time.rad, time.chemo, na.rm=TRUE)), NA))

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

I'm still too new to comment, but the only time I get an error like that is when I forget to define a function/variable or forget to source a package. 我仍然太新以至于无法发表评论,但是唯一一次出现这样的错误的时候是我忘记定义函数/变量或忘记提供程序包的源代码。

Since days() isn't part of R's base package, I think you need to add: 由于days()不是R的基本软件包的一部分,因此我认为您需要添加:

```{r echo = FALSE}
library("lubridate")
```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM