繁体   English   中英

gt表中的源代码后是否可以有脚注?

[英]Is it possible to have footnotes after source in gt table?

我有下表:

datasets::mtcars %>%
  head(5) %>% 
  gt()  %>%
  tab_footnote("Footnote: Go Below",
               locations =  cells_body(columns = mpg,
                                       rows = c(2))) %>%
  tab_source_note("Source:PUT THIS ON TOP")

这给了我这个输出:

在此处输入图像描述

我遇到的问题是我需要源代码下方的脚注:更改代码的位置也不起作用。

datasets::mtcars %>%
  head(5) %>% 
  gt()  %>% tab_source_note("Source: PUT THIS ON TOP") %>% 

  tab_footnote("Footnote: Go Below",
               locations =  cells_body(columns = mpg,
                                       rows = c(2))) 

期望的输出:

在此处输入图像描述

更新:此代码有效但未达到预期

在此处输入图像描述

custom_css1 <- paste0("
    #two .gt_sourcenote {
      color: red;
      position: absolute;
      top: ",240,"px;
                     }")
      

custom_css2 <- paste0("
    #two .gt_footnote {
      color: blue;
      position: absolute;
      top: ",270,"px;
                     }")                  
                    

datasets::mtcars %>%
  head(5) %>%
  gt(id="two")  %>%
  tab_footnote("Footnote: Go Below",
               locations =  cells_body(columns = mpg,
                                       rows = c(2))) %>%
  tab_footnote("Footnote2: Go Below2",
               locations =  cells_body(columns = mpg,
                                       rows = c(5))) %>%
  tab_source_note("Source:PUT THIS ON TOP") %>%
  opt_css(
    css = custom_css1 
  ) %>% 
  opt_css(
    css = custom_css2
    
  )

我的解决方案使用 RMarkdown 创建 HTML 输出,并使用 CSS 设置样式。 我使用gt(id="two")gt::opt_css()添加 CSS。 我在浏览器中使用了检查工具,看到 sourcenote 被标记为 .gt_sourcenote,而脚注被标记为 .gt_footnote。 根据自己的喜好调整 css 位置。

通常,您会使用 3 个反引号来开始和结束 Rmd 中的块,但 stackoverflow 使用 3 个反引号来表示代码块。 我将我的块的 3 个反引号(例如 ```)表示为 3 个星号(例如 ***)。 根据您的喜好编辑此内容。

输出截图: https ://prnt.sc/7Xuf2Q0XIyuA

---
title: "Untitled"
output: html_document
---

***{r lib, warning=F, echo=F, message=F}
library(tidyverse)
library(gt)
knitr::opts_chunk$set(echo = TRUE)
***

***{r demo, echo=F}
datasets::mtcars %>%
  head(5) %>%
  gt(id="two")  %>%
  tab_footnote("Footnote: Go Below",
               locations =  cells_body(columns = mpg,
                                       rows = c(2))) %>%
  tab_source_note("Source:PUT THIS ON TOP") %>%
  opt_css(
  css = "
    #two .gt_sourcenote {
      color: red;
      position: absolute;
      top: 310px;
    }

    #two .gt_footnote {
      color: blue;
      position: absolute;
      top: 340px;
    }
    "
)
***

有用的链接: 如何使用 R 包 gt 旋转列标题? https://www.rdocumentation.org/packages/gt/versions/0.5.0/topics/opt_css https://www.w3schools.com/cssref/pr_class_position.asp


编辑重叠脚注:

这在我的浏览器中看起来不错。

opt_css(
  css = "
    #two .gt_sourcenote {
      color: red;
      position: relative;
      top: -40px;
    }

    #two .gt_footnote {
      color: blue;
      position: relative;
      top: 28px;
    }
    "
)

我问了这个问题几天后 - gt更新到 0.6.0 版,现在tab_footnote()函数不需要位置。 https://gt.rstudio.com/news/index.html#gt-development-version

#install.packages("gt", repos='http://cran.us.r-project.org')
library(gt)
library(dplyr)

datasets::mtcars %>%
  head(5) %>%
  gt(id="two")  %>%
  tab_footnote("Source Note: Go on Top"
               ) %>%
  tab_footnote("Footnote: Go Below",
               locations =  cells_body(columns = mpg,
                                       rows = c(5)))

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM