我目前正在努力让 knitr 渲染我的传单地图,从集合中获取以正确显示在渲染的 RMD html 输出中。我已经意识到循环集合并使用 RMD/knitr 生成图形输出时的一些潜在问题,但我仍然无法弄清楚如何使我的示例适用于传单地图。
可重现的工作示例(Test_1.Rmd):
---
title: "test1"
author: "phabee"
date: "22 Mai 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Title 1
```{r, fig.show='asis', echo=FALSE, results='asis'}
for (i in 1:4) {
cat("### Plot Number ", i, "\n")
plot(1,1)
# use plot.new() here to force rendering of potential plot-duplicates
plot.new()
cat("\n\n")
}
```
Run Code Online (Sandbox Code Playgroud)
上面的例子按预期渲染(至少在添加plot.new()之后,这是我从Freedomtowin学到的。但是当我尝试对传单地图做同样的事情时,它根本不起作用。没有渲染任何单个地图:
可重现的失败示例 (Test_2.Rmd)
---
title: "test2"
author: "phabee"
date: "22 Mai 2018"
output: html_document
--- …Run Code Online (Sandbox Code Playgroud) 在一个函数中,我正在调用另一个计算密集型外部函数,该函数在某些情况下会触发警告,但也会返回一个值,我想评估该值,无论是否发生警告。
此外,如果发生警告或错误,我想捕获警告/错误消息以进行进一步处理。
下面的 R 代码展示了我的意图:
hurz <- function(x) {
# HINT: max(x) triggers a warning when x = NULL
max(x)
return(12345)
}
laus <- function(x) {
r <- tryCatch({
list(value = hurz(x), error_text = "No error.")
}, warning = function(e) {
error_text <- paste0("WARNING: ", e)
# ugly hack to get the result while still catching the warning
return(list(value = (suppressWarnings(hurz(5))), error_text = error_text))
}, error = function(e) {
error_text <- paste0("ERROR: ", e)
return(list(value = NA, error_text = error_text)) …Run Code Online (Sandbox Code Playgroud) 我怎样才能让 data.table 理解,这些值是上面定义的变量而不是列名?
带有第一个注释的行应该返回 0L,但返回 dt 中包含的所有数据。
可重现的例子:
library(data.table)
dt <- structure(list(zip_from = c("1000", "1000", "1000", "1000", "1000",
"1000", "1000", "1000", "1000", "1000"), zip_to = c("1000", "1001",
"1002", "1003", "1004", "1005", "1006", "1007", "1008", "1009"
), time_1 = c(0, 332.8, 332.8, 362.5, 504.9, 256.6, 446.4, 694.4,
723.4, 462.3), dist_1 = c(0, 3208, 3208, 3465.3, 4275.5, 2267.6,
4158.1, 5811.4, 8842.6, 4624.7), dist_2 = c(0, 3208, 3208, 3465.3,
4275.5, 2267.6, 4158.1, 5811.4, 8842.6, 4624.7), time_2 = c(0,
332.8, 332.8, 362.5, 504.9, 256.6, …Run Code Online (Sandbox Code Playgroud)