R 将列表的每个 data.frame 打印到单独的表中,标题为 data.frame 名称和描述

发布于 2025-01-10 11:02:14 字数 1821 浏览 3 评论 0原文

跟进我的之前的问题(感谢@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光是把杀猪刀 2025-01-17 11:02:14

.y 已在 imap 中被引用,如 paste0("Test ", .y, " ( ", 返回 Table: Test df1 ( 所以你不需要引用。

注意:

Names1 %>% 
  filter(Name == "df1") %>% 
  pull(Desp)
# [1] "Desp1"

所以如果你不包含引用,它会起作用:

imap(
  .x = ls1
  , .f = ~ {
    kbl(
      x       = .x
      , format  = "markdown"
      , caption = paste0("Test ", .y, " ( ", 
                         Names1 %>% 
                           filter(Name == .y) %>% 
                           pull(Desp)
                         , " )"
      )
    )
  }
)
# $df1


# Table: Test df1 ( Desp1 )

# |  X|
# |--:|
# |  1|
# |  2|
# |  3|

# $df2


# Table: Test df2 ( Desp2 )

# |  Y|
# |--:|
# |  5|
# |  6|

你这样做的方式,它正在过滤那些不在 data.frame 中(.y)。

imap(
  .x = ls1
  , .f = ~ {
    .y
  }
)
# $df1
# [1] "df1"

# $df2
# [1] "df2"


imap(
  .x = ls1
  , .f = ~ {
    quote(.y)
  }
)
# $df1
# .y

# $df2
# .y

.y is already quoted within imap as is seen by the output of the first part of paste0("Test ", .y, " ( ", which returns Table: Test df1 ( so you don't need quote.

Note:

Names1 %>% 
  filter(Name == "df1") %>% 
  pull(Desp)
# [1] "Desp1"

so if you don't include quote it works:

imap(
  .x = ls1
  , .f = ~ {
    kbl(
      x       = .x
      , format  = "markdown"
      , caption = paste0("Test ", .y, " ( ", 
                         Names1 %>% 
                           filter(Name == .y) %>% 
                           pull(Desp)
                         , " )"
      )
    )
  }
)
# $df1


# Table: Test df1 ( Desp1 )

# |  X|
# |--:|
# |  1|
# |  2|
# |  3|

# $df2


# Table: Test df2 ( Desp2 )

# |  Y|
# |--:|
# |  5|
# |  6|

The way you were doing it, it was filtering for something that wasn't in the data.frame (.y). See here:

imap(
  .x = ls1
  , .f = ~ {
    .y
  }
)
# $df1
# [1] "df1"

# $df2
# [1] "df2"


imap(
  .x = ls1
  , .f = ~ {
    quote(.y)
  }
)
# $df1
# .y

# $df2
# .y
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文