我遇到了一个问题,我有一个类似于下面所示的 R 脚本(有几个循环来绘制一些图形和多个嵌套 if 语句来确定要绘制哪组图形,但这个示例就足够了。)
运行 spin('spin-script.R')
时,if 语句中的所有 #'
注释不会显示在最终文档中。
任何关于为什么会发生这种情况的见解都将不胜感激。
opts_chunk$set(echo=FALSE)
#+ Title
print("This is the title")
#+ Choose, echo = FALSE
choices <-c("0","1", "2", "3", "4", "5")
choice <- select.list(choices, title = "Pick your favourite Number")
numbers <- as.double(choices)
choice <- numbers[choices==choice]
#+ SillyNumber
if(choice != 0){
#+ Loop
x <- 0
for (i in 1:choice){
#' Some text to show user
x <- x + rnorm(1, choice, 1/choice)
#+ IfStatement, echo = TRUE
if( choice == 3 ) {
#' That's my favourite number too
{{cat("\n")}}
#' Have a bonus boost
x <- 2 * x
}
}
}
# Display, echo = TRUE
sprintf("Your favourite number scored a total of %f points", x)
最佳答案
doc
参数控制将哪些行解释为文档,默认值 "^#+'[ ]?"
仅匹配以 开头的行>#
并且前面没有空格。您可能希望将其设置为 spin(... , doc="^\\s*#+'[ ]?")
以允许 #'
之前有空格> 字符串。有关详细信息,请参阅?spin
。
或者,您可以像这样记录代码:
#+ SillyNumber
if(choice != 0){
#+ Loop
x <- 0
for (i in 1:choice){
#' Some text to show user
x <- x + rnorm(1, choice, 1/choice)
#+ IfStatement, echo = TRUE
if( choice == 3 ) {
#' That's my favourite number too
{{cat("\n")}}
#' Have a bonus boost
x <- 2 * x
}
}
}
关于r - 带循环和比较语句的knitr spin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28550747/