我有一个 ggplot2 元素和一个 KableExtra 表。是否可以将它们与 patchwork、cowplot 或任何其他包组合成一个对象,并将其打印在 rmarkdown 文档中?如果没有解决方案,是否有任何其他带有 html 或 Latex 设计的表格包,我可以用它来解决这个问题?
是否可以使用自定义块(https://bookdown.org/yihui/rmarkdown-cookbook/custom-blocks.html)将这些元素连接在一起?我尝试过但没有成功。
将表格作为 custom_annotation 添加到 ggplot 对我来说不起作用,因为我想将许多表格和绘图添加到同一元素。
> dt_example <- data.table(index = c('type', 'model', 'manufacturer', 'time', 'revolutions', 'element', 'fluid'), object = c('B12', 'Schullo', 'GM', '5 h', '54 k', 'XX', 'water'))
> dt_example
index object
1: type B12
2: model Schullo
3: manufacturer GM
4: time 5 h
5: revolutions 54 k
6: element XX
7: fluid water
kable_table <- dt_example %>%
kable(col.names = c('',''), format = …Run Code Online (Sandbox Code Playgroud) 我试图将所有NA传递给函数的内容更改为NULL. 我想要的不是按名称单独处理每个参数,而是捕获所有参数,过滤那些参数并将NA它们更改为 NULL。
foo <- function(a, b = NA, c = NA) {
print(a)
print(b)
print(c)
# do something to capture all args and change NAs to NULL
print(a)
print(b)
print(c)
}
# expected output
> foo(a = 1)
[1] 1
[1] NA
[1] NA
[1] 1
[1] NULL
[1] NULL
Run Code Online (Sandbox Code Playgroud)
编辑:包含一个没有默认值的参数
我有以下 data.table 和 2 个向量:
> dt = data.table(a = c('hi', 'ok', 'no', 'ok', 'no', 'www', 'whatever'))
> dt
a
1: hi
2: ok
3: no
4: ok
5: no
6: www
7: whatever
v1 = c('hi', 'no')
v2 = c('HI', 'NO')
> v1
[1] "hi" "no"
> v2
[1] "HI" "NO"
Run Code Online (Sandbox Code Playgroud)
我想将从 dt 中的 v1 中找到的值替换为 v2 中的值。如果我发现“hi”,我将替换为“HI”,如果我发现“no”,我将替换为“NO”。
我发现的最简单的方法是:
invisible(mapply(function(x, y) `[`(dt, grepl(x, a), a := y), x = v1, y = v2))
> dt
a
1: HI
2: …Run Code Online (Sandbox Code Playgroud) 我有一个这样的函数,其中函数的用户指定输出的类型:
pub fn test(user_input: &str, number: f32) -> ??? {
if user_input == "f32"{
number as f32
} else if user_input == "f64" {
number as f64
} else {
panic!();
}
}
Run Code Online (Sandbox Code Playgroud)
我对 Rust 比较陌生,所以我问你,如何让这个函数返回用户要求的类型?我只对 f32 和 f64 输出感兴趣。