我有一个构建脚本,它将多个.Rmd文件编织成.md文件,我从以下调用方法得到不同的结果:
R -e source('bin/build_script.R')
按预期工作,但
Rscript bin/build_script.R
无法按预期工作。生成的.md文件之间的区别与具有行as(x, "Spatial")
的代码块有关。在第一种方法中,x
被转换,每个人都很高兴。使用Rscript调用会导致代码块返回错误
Error in as(x, "Spatial"): no method or default for coercing "sfc_POINT" to "Spatial"
Rscript和源处理导入库的方式有所不同吗?
这是我的构建脚本:
require(knitr)
require(yaml)
require(stringr)
config = yaml.load_file('docs/_config.yml')
render_markdown(fence_char = '~')
opts_knit$set(
root.dir = '.',
base.dir = 'docs/',
base.url = '{{ site.baseurl }}/')
opts_chunk$set(
comment = NA,
fig.path = 'images/',
block_ial = c('{:.input}', '{:.output}'),
cache = FALSE,
cache.path = 'docs/_slides_Rmd/cache/')
current_chunk = knit_hooks$get('chunk')
chunk = function(x, options) {
x <- current_chunk(x, options)
if (!is.null(options$title)) {
# add title to kramdown block IAL
x <- gsub('~~~(\n*(!\\[.+)?$)',
paste0('~~~\n{:.text-document title="', options$title, '"}\\1'),
x)
# move figures to <div> on next slide
x <- gsub('(!\\[.+$)', '===\n\n\\1\n{:.captioned}', x)
} else {
# add default kramdown block IAL to kramdown block IAL to input
x <- gsub('~~~\n(\n+~~~)',
paste0('~~~\n', options$block_ial[1], '\\1'),
x)
if (str_count(x, '~~~') > 2) {
idx <- 2
} else {
idx <- 1
}
x <- gsub('~~~(\n*$)',
paste0('~~~\n', options$block_ial[idx], '\\1'),
x)
}
return(x)
}
knit_hooks$set(chunk = chunk)
files <- list.files('docs/_slides_Rmd')
for (f in config$slide_sorter) {
f.Rmd <- paste0(f, '.Rmd')
if (f.Rmd %in% files) {
f.md <- paste0(f, '.md')
knit(input = file.path('docs/_slides_Rmd', f.Rmd),
output = file.path('docs/_slides', f.md))
}
}
答案 0 :(得分:7)
这是一个古老的功能&#34;并且值得一个FAQ条目:Rscript
,它的永恒智慧不加载Base R附带的methods
包,需要正确处理S4类。< / p>
只需在您的脚本中添加一行library(methods)
即可。
或者使用littler代替加载它的Rscript
:)