R 将列表的每个 data.frame 打印到单独的表中,标题为 data.frame 名称和描述
跟进我的之前的问题。(感谢@akrun的帮助。
)将列表的每个 data.frame
打印到单独的表格中,标题为 data.frame
名称和来自另一个对象的描述。我尝试的代码如下。无法获取表格标题中的描述。
我在这里缺少什么?
如果这有效:
Names1 %>%
filter(Name == quote(df1)) %>%
pull(Desp)
为什么这在 imap
中不起作用?
Names1 %>%
filter(Name == quote(.y)) %>%
pull(Desp)
完整代码
library(tidyverse)
library(kableExtra)
#>
#> Attaching package: 'kableExtra'
#> The following object is masked from 'package:dplyr':
#>
#> group_rows
df1 <- data.frame(X = 1:3)
df2 <- data.frame(Y = 5:6)
ls1 <-
list(df1, df2) %>%
set_names(c("df1", "df2"))
ls1
#> $df1
#> X
#> 1 1
#> 2 2
#> 3 3
#>
#> $df2
#> Y
#> 1 5
#> 2 6
Names1 <-
tibble(
Name = c("df1", "df2")
, Desp = c("Desp1", "Desp2")
)
Names1
#> # A tibble: 2 x 2
#> Name Desp
#> <chr> <chr>
#> 1 df1 Desp1
#> 2 df2 Desp2
Names1 %>%
filter(Name == quote(df1)) %>%
pull(Desp)
#> [1] "Desp1"
imap(
.x = ls1
, .f = ~ {
kbl(
x = .x
, format = "markdown"
, caption = paste0("Test ", .y, " ( ",
Names1 %>%
filter(Name == quote(.y)) %>%
pull(Desp)
, " )"
)
)
}
)
#> $df1
#>
#>
#> Table: Test df1 ( )
#>
#> | X|
#> |--:|
#> | 1|
#> | 2|
#> | 3|
#>
#> $df2
#>
#>
#> Table: Test df2 ( )
#>
#> | Y|
#> |--:|
#> | 5|
#> | 6|
Follow-up to my earlier question. (Thanks to @akrun for his help.)
Want to print each data.frame
of a list into separate Tables with caption as data.frame
name and description from another object. My attempted code is given below. Not being able to get description in tables caption.
What am I missing here?
If this is working:
Names1 %>%
filter(Name == quote(df1)) %>%
pull(Desp)
Why is this not working inside imap
?
Names1 %>%
filter(Name == quote(.y)) %>%
pull(Desp)
Complete Code
library(tidyverse)
library(kableExtra)
#>
#> Attaching package: 'kableExtra'
#> The following object is masked from 'package:dplyr':
#>
#> group_rows
df1 <- data.frame(X = 1:3)
df2 <- data.frame(Y = 5:6)
ls1 <-
list(df1, df2) %>%
set_names(c("df1", "df2"))
ls1
#> $df1
#> X
#> 1 1
#> 2 2
#> 3 3
#>
#> $df2
#> Y
#> 1 5
#> 2 6
Names1 <-
tibble(
Name = c("df1", "df2")
, Desp = c("Desp1", "Desp2")
)
Names1
#> # A tibble: 2 x 2
#> Name Desp
#> <chr> <chr>
#> 1 df1 Desp1
#> 2 df2 Desp2
Names1 %>%
filter(Name == quote(df1)) %>%
pull(Desp)
#> [1] "Desp1"
imap(
.x = ls1
, .f = ~ {
kbl(
x = .x
, format = "markdown"
, caption = paste0("Test ", .y, " ( ",
Names1 %>%
filter(Name == quote(.y)) %>%
pull(Desp)
, " )"
)
)
}
)
#> $df1
#>
#>
#> Table: Test df1 ( )
#>
#> | X|
#> |--:|
#> | 1|
#> | 2|
#> | 3|
#>
#> $df2
#>
#>
#> Table: Test df2 ( )
#>
#> | Y|
#> |--:|
#> | 5|
#> | 6|
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
.y
已在imap
中被引用,如paste0("Test ", .y, " ( ", 返回
Table: Test df1 (
所以你不需要引用。注意:
所以如果你不包含引用,它会起作用:
你这样做的方式,它正在过滤那些不在 data.frame 中(
.y
)。.y
is already quoted withinimap
as is seen by the output of the first part ofpaste0("Test ", .y, " ( ",
which returnsTable: Test df1 (
so you don't need quote.Note:
so if you don't include quote it works:
The way you were doing it, it was filtering for something that wasn't in the data.frame (
.y
). See here: