rmdformat:更改默认表格滚动框

发布于 2025-01-31 13:39:43 字数 2925 浏览 4 评论 0原文

上下文

rmdformats软件包为RMD提供了一些很酷的默认主题。 我正在使用Downcute主题。

但是,可重现的示例

但是,当我在HTML文档中绘制一个表时,主题会自动设置一些默认值,我无法弄清楚如何编辑。

---
title: "Test"
output: rmdformats::downcute
---  

```{r, echo=FALSE, warning=FALSE, message=FALSE}
library(dplyr)
library(kableExtra)

mtcars %>% 
  bind_cols(mtcars) %>% 
  kbl() %>% 
  scroll_box(width = "100%", height = "400px")
```

这就是看起来像(滚动到底部以查看第二个水平滚动栏):

此输出

有两个问题:

  • scroll_box函数添加了新的滚动条,而不是替换由软件包自动设置的现有的滚动条。因此,您最终得到了两个水平滚动条(第二个滚动条似乎已关闭)。
  • 列的标签未固定在顶部。

预期结果

我想获得以下输出。为了创建预期的结果,我切换到html_document主题,但是我想使用rmdformats主题!

---
title: "Test"
output: html_document
---

```{r, echo=FALSE, warning=FALSE, message=FALSE}
library(dplyr)
library(kableExtra)

mtcars %>% 
  bind_cols(mtcars) %>% 
  kbl() %>% 
  kable_styling() %>% 
  scroll_box(width = "100%", height = "400px")
```

有帮助吗?也许是CSS解决方案?

额外的信息

> sessionInfo()
R version 4.1.3 (2022-03-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=Italian_Italy.1252  LC_CTYPE=Italian_Italy.1252    LC_MONETARY=Italian_Italy.1252 LC_NUMERIC=C                  
[5] LC_TIME=Italian_Italy.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] kableExtra_1.3.4 dplyr_1.0.9     

loaded via a namespace (and not attached):
 [1] bslib_0.3.1       jquerylib_0.1.4   highr_0.9         pillar_1.7.0      compiler_4.1.3    rmdformats_1.0.4  tools_4.1.3      
 [8] digest_0.6.29     jsonlite_1.8.0    viridisLite_0.4.0 evaluate_0.15     lifecycle_1.0.1   tibble_3.1.6      pkgconfig_2.0.3  
[15] rlang_1.0.2       DBI_1.1.2         cli_3.3.0         rstudioapi_0.13   yaml_2.3.5        xfun_0.30         fastmap_1.1.0    
[22] httr_1.4.3        stringr_1.4.0     knitr_1.39        xml2_1.3.3        sass_0.4.1        systemfonts_1.0.4 generics_0.1.2   
[29] vctrs_0.4.1       webshot_0.5.3     tidyselect_1.1.2  svglite_2.1.0     glue_1.6.2        R6_2.5.1          fansi_1.0.3      
[36] rmarkdown_2.14    bookdown_0.26     purrr_0.3.4       magrittr_2.0.3    scales_1.2.0      ellipsis_0.3.2    htmltools_0.5.2  
[43] assertthat_0.2.1  rvest_1.0.2       colorspace_2.0-3  utf8_1.2.2        stringi_1.7.6     munsell_0.5.0     crayon_1.5.1 

Context

The rmdformats package provides a few cool default theme for Rmds.
I'm using the downcute theme.

Reproducible example

However, when I plot a table in my html document, the theme automatically sets a few defaults that I can't figure out how to edit.

---
title: "Test"
output: rmdformats::downcute
---  

```{r, echo=FALSE, warning=FALSE, message=FALSE}
library(dplyr)
library(kableExtra)

mtcars %>% 
  bind_cols(mtcars) %>% 
  kbl() %>% 
  scroll_box(width = "100%", height = "400px")
```

Here is how it looks like (SCROLL TO THE BOTTOM TO SEE THE SECOND HORIZONTAL SCROLLBAR):
enter image description here

Issues

There are two issues with this output:

  • The scroll_box function adds a new layer of scroll bars instead of replacing the existing one which is automatically set up by the package. Thus, you end up with two horizontal scroll bar (the second one seems to be off)
  • The labels of the columns are not fixed on top.

Expected Result

I would like to get the following output. To create the expected result I switched to the html_document theme, but I would like to use the rmdformats theme!

---
title: "Test"
output: html_document
---

```{r, echo=FALSE, warning=FALSE, message=FALSE}
library(dplyr)
library(kableExtra)

mtcars %>% 
  bind_cols(mtcars) %>% 
  kbl() %>% 
  kable_styling() %>% 
  scroll_box(width = "100%", height = "400px")
```

enter image description here

Any help? Maybe a CSS solution?

Extra info

> sessionInfo()
R version 4.1.3 (2022-03-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=Italian_Italy.1252  LC_CTYPE=Italian_Italy.1252    LC_MONETARY=Italian_Italy.1252 LC_NUMERIC=C                  
[5] LC_TIME=Italian_Italy.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] kableExtra_1.3.4 dplyr_1.0.9     

loaded via a namespace (and not attached):
 [1] bslib_0.3.1       jquerylib_0.1.4   highr_0.9         pillar_1.7.0      compiler_4.1.3    rmdformats_1.0.4  tools_4.1.3      
 [8] digest_0.6.29     jsonlite_1.8.0    viridisLite_0.4.0 evaluate_0.15     lifecycle_1.0.1   tibble_3.1.6      pkgconfig_2.0.3  
[15] rlang_1.0.2       DBI_1.1.2         cli_3.3.0         rstudioapi_0.13   yaml_2.3.5        xfun_0.30         fastmap_1.1.0    
[22] httr_1.4.3        stringr_1.4.0     knitr_1.39        xml2_1.3.3        sass_0.4.1        systemfonts_1.0.4 generics_0.1.2   
[29] vctrs_0.4.1       webshot_0.5.3     tidyselect_1.1.2  svglite_2.1.0     glue_1.6.2        R6_2.5.1          fansi_1.0.3      
[36] rmarkdown_2.14    bookdown_0.26     purrr_0.3.4       magrittr_2.0.3    scales_1.2.0      ellipsis_0.3.2    htmltools_0.5.2  
[43] assertthat_0.2.1  rvest_1.0.2       colorspace_2.0-3  utf8_1.2.2        stringi_1.7.6     munsell_0.5.0     crayon_1.5.1 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

Spring初心 2025-02-07 13:39:43

您必须控制表和表包装元素。让我知道这是否不是您想要的。

---
title: "Test"
output: rmdformats::downcute
---  

<style>
table, .table-wrapper {
  height: 400px;
  width: 100%;
  overflow: auto;
}
</style>

```{r, echo=FALSE, warning=FALSE, message=FALSE}
library(dplyr)
library(kableExtra)

mtcars %>% 
  bind_cols(mtcars) %>% 
  kbl() #%>% 
 # scroll_box(width = "100%", height = "400px")
```

我不知道您计划如何使用其他空间,但是此模板具有令人讨厌的边距。如果您想最大化空间的使用,请更新&lt; style&gt;标签之间的内容到以下内容:

<style>
table, .table-wrapper {
  height: 400px;
  width: 100%;
  overflow: auto;
}
.layout-narrow .Wrap {
  margin: unset;
  max-width: unset;
}
</style>

请注意,现在没有水平滚动栏,因为表的整个宽度都在显示。


更新:添加粘性标头


行以制作上排棒,更改&lt; styles&gt;标签中的内容。 (您仍然可以向此添加.layout-narrow .wrap样式。)

请注意,它不是表,.table-wrapper不再在这些标签中。

<style> 
.table-wrapper {
  overflow: auto;
  height: 400px;
  width: 100%;
}
thead, th {
  top: 0;
  position: sticky;
  z-index: 2;
}
</style>

You have to control the table and the table wrapper elements. Let me know if this isn't what you were looking for.

---
title: "Test"
output: rmdformats::downcute
---  

<style>
table, .table-wrapper {
  height: 400px;
  width: 100%;
  overflow: auto;
}
</style>

```{r, echo=FALSE, warning=FALSE, message=FALSE}
library(dplyr)
library(kableExtra)

mtcars %>% 
  bind_cols(mtcars) %>% 
  kbl() #%>% 
 # scroll_box(width = "100%", height = "400px")
```

enter image description here

I don't know how you planned to use the other space, but this template has obnoxious margins. If you wanted to maximize the use of space, then update the content between the <style> tags to the following content:

<style>
table, .table-wrapper {
  height: 400px;
  width: 100%;
  overflow: auto;
}
.layout-narrow .Wrap {
  margin: unset;
  max-width: unset;
}
</style>

Notice that there is no horizontal scrollbar now because the entire width of the table is showing.

enter image description here


Update: Adding a sticky header row


To make the top row stick, change the content in the <styles> tags to the following. (You can still add the .layout-narrow .Wrap styles to this, as well.)

Note that it's not table, .table-wrapper. table is no longer in these tags.

<style> 
.table-wrapper {
  overflow: auto;
  height: 400px;
  width: 100%;
}
thead, th {
  top: 0;
  position: sticky;
  z-index: 2;
}
</style>

enter image description here

记忆で 2025-02-07 13:39:43

RMDFormats版本1.0.4

当我运行您的代码时:

---
title: "Test"
output: rmdformats::downcute
---  

```{r, echo=FALSE, warning=FALSE, message=FALSE}
library(dplyr)
library(kableExtra)

mtcars %>% 
  bind_cols(mtcars) %>% 
  kbl() %>% 
  scroll_box(width = "100%", height = "400px")
```

这是输出:

“在此处输入图像描述”

只有一个水平和垂直滚动条。

SessionInfo

R version 4.1.0 (2021-05-18)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS 12.4

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] nl_NL.UTF-8/nl_NL.UTF-8/nl_NL.UTF-8/C/nl_NL.UTF-8/nl_NL.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] kableExtra_1.3.4 dplyr_1.0.9      rmdformats_1.0.4

loaded via a namespace (and not attached):
 [1] pillar_1.7.0      compiler_4.1.0    tools_4.1.0       digest_0.6.29     viridisLite_0.4.0
 [6] evaluate_0.15     lifecycle_1.0.1   tibble_3.1.7      pkgconfig_2.0.3   rlang_1.0.2      
[11] cli_3.3.0         DBI_1.1.2         rstudioapi_0.13   yaml_2.3.5        xfun_0.31        
[16] fastmap_1.1.0     httr_1.4.3        stringr_1.4.0     knitr_1.39        xml2_1.3.3       
[21] systemfonts_1.0.4 generics_0.1.2    vctrs_0.4.1       webshot_0.5.2     tidyselect_1.1.2 
[26] svglite_2.1.0     glue_1.6.2        R6_2.5.1          fansi_1.0.3       rmarkdown_2.14   
[31] bookdown_0.25     purrr_0.3.4       magrittr_2.0.3    scales_1.2.0      ellipsis_0.3.2   
[36] htmltools_0.5.2   rsconnect_0.8.25  assertthat_0.2.1  rvest_1.0.2       colorspace_2.0-3 
[41] utf8_1.2.2        stringi_1.7.6     munsell_0.5.0     crayon_1.5.1  

rmdformats版本1.0.3

”在此处输入图像描述”

sessioninfo

R version 4.1.0 (2021-05-18)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS 12.4

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] nl_NL.UTF-8/nl_NL.UTF-8/nl_NL.UTF-8/C/nl_NL.UTF-8/nl_NL.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] kableExtra_1.3.4 dplyr_1.0.9      rmdformats_1.0.3 devtools_2.4.3   usethis_2.1.5   

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.2  xfun_0.31         remotes_2.4.2     purrr_0.3.4       colorspace_2.0-3 
 [6] vctrs_0.4.1       generics_0.1.2    testthat_3.1.2    viridisLite_0.4.0 htmltools_0.5.2  
[11] utf8_1.2.2        rlang_1.0.2       pkgbuild_1.3.1    pillar_1.7.0      glue_1.6.2       
[16] withr_2.5.0       DBI_1.1.2         sessioninfo_1.2.2 lifecycle_1.0.1   stringr_1.4.0    
[21] munsell_0.5.0     rvest_1.0.2       evaluate_0.15     memoise_2.0.1     knitr_1.39       
[26] callr_3.7.0       fastmap_1.1.0     ps_1.7.0          fansi_1.0.3       scales_1.2.0     
[31] cachem_1.0.6      desc_1.4.1        pkgload_1.2.4     webshot_0.5.2     fs_1.5.2         
[36] systemfonts_1.0.4 brio_1.1.3        digest_0.6.29     stringi_1.7.6     bookdown_0.26    
[41] processx_3.5.3    rprojroot_2.0.2   cli_3.3.0         tools_4.1.0       magrittr_2.0.3   
[46] tibble_3.1.7      crayon_1.5.1      pkgconfig_2.0.3   ellipsis_0.3.2    xml2_1.3.3       
[51] prettyunits_1.1.1 assertthat_0.2.1  rmarkdown_2.14    svglite_2.1.0     httr_1.4.3       
[56] rstudioapi_0.13   R6_2.5.1          compiler_4.1.0   

rmdformats version 1.0.4

When I run your code:

---
title: "Test"
output: rmdformats::downcute
---  

```{r, echo=FALSE, warning=FALSE, message=FALSE}
library(dplyr)
library(kableExtra)

mtcars %>% 
  bind_cols(mtcars) %>% 
  kbl() %>% 
  scroll_box(width = "100%", height = "400px")
```

This is the output:

enter image description here

There is only one horizontal and vertical scrollbar.

Sessioninfo

R version 4.1.0 (2021-05-18)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS 12.4

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] nl_NL.UTF-8/nl_NL.UTF-8/nl_NL.UTF-8/C/nl_NL.UTF-8/nl_NL.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] kableExtra_1.3.4 dplyr_1.0.9      rmdformats_1.0.4

loaded via a namespace (and not attached):
 [1] pillar_1.7.0      compiler_4.1.0    tools_4.1.0       digest_0.6.29     viridisLite_0.4.0
 [6] evaluate_0.15     lifecycle_1.0.1   tibble_3.1.7      pkgconfig_2.0.3   rlang_1.0.2      
[11] cli_3.3.0         DBI_1.1.2         rstudioapi_0.13   yaml_2.3.5        xfun_0.31        
[16] fastmap_1.1.0     httr_1.4.3        stringr_1.4.0     knitr_1.39        xml2_1.3.3       
[21] systemfonts_1.0.4 generics_0.1.2    vctrs_0.4.1       webshot_0.5.2     tidyselect_1.1.2 
[26] svglite_2.1.0     glue_1.6.2        R6_2.5.1          fansi_1.0.3       rmarkdown_2.14   
[31] bookdown_0.25     purrr_0.3.4       magrittr_2.0.3    scales_1.2.0      ellipsis_0.3.2   
[36] htmltools_0.5.2   rsconnect_0.8.25  assertthat_0.2.1  rvest_1.0.2       colorspace_2.0-3 
[41] utf8_1.2.2        stringi_1.7.6     munsell_0.5.0     crayon_1.5.1  

rmdformats version 1.0.3

enter image description here

Sessioninfo

R version 4.1.0 (2021-05-18)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS 12.4

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] nl_NL.UTF-8/nl_NL.UTF-8/nl_NL.UTF-8/C/nl_NL.UTF-8/nl_NL.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] kableExtra_1.3.4 dplyr_1.0.9      rmdformats_1.0.3 devtools_2.4.3   usethis_2.1.5   

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.2  xfun_0.31         remotes_2.4.2     purrr_0.3.4       colorspace_2.0-3 
 [6] vctrs_0.4.1       generics_0.1.2    testthat_3.1.2    viridisLite_0.4.0 htmltools_0.5.2  
[11] utf8_1.2.2        rlang_1.0.2       pkgbuild_1.3.1    pillar_1.7.0      glue_1.6.2       
[16] withr_2.5.0       DBI_1.1.2         sessioninfo_1.2.2 lifecycle_1.0.1   stringr_1.4.0    
[21] munsell_0.5.0     rvest_1.0.2       evaluate_0.15     memoise_2.0.1     knitr_1.39       
[26] callr_3.7.0       fastmap_1.1.0     ps_1.7.0          fansi_1.0.3       scales_1.2.0     
[31] cachem_1.0.6      desc_1.4.1        pkgload_1.2.4     webshot_0.5.2     fs_1.5.2         
[36] systemfonts_1.0.4 brio_1.1.3        digest_0.6.29     stringi_1.7.6     bookdown_0.26    
[41] processx_3.5.3    rprojroot_2.0.2   cli_3.3.0         tools_4.1.0       magrittr_2.0.3   
[46] tibble_3.1.7      crayon_1.5.1      pkgconfig_2.0.3   ellipsis_0.3.2    xml2_1.3.3       
[51] prettyunits_1.1.1 assertthat_0.2.1  rmarkdown_2.14    svglite_2.1.0     httr_1.4.3       
[56] rstudioapi_0.13   R6_2.5.1          compiler_4.1.0   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文