最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how to do the 2 group using gt package in R - Stack Overflow

programmeradmin3浏览0评论

hy. can someone help me with this code to produce in gt. the data is here

data <- tibble(
  Function = c("distinct()","count()","group_by()",
               "rename_with()","slice_min()","slice_sample()"),
  Description = c("keep only unique rows from a data frame",
                  "count the unique values of one or more var",
                  "group by one or more variables",
                  "renames columns using a function",
                  "select rows with the smallest values of a variable",
                  "randomly selects rows"),
  arguments = list(
    list("the data argument doesn't have Data Masking or Tidy Selection", 
         "the ... use data masking",
         "the .keep_all arg  doesn't have Data Masking or Tidy Selection"),
    list("x arg  doesn't have Data Masking or Tidy Selection",
         "the ... arg have data masking",
         "the wt arg have data masking",
         "sort arg  doesn't have Data Masking or Tidy Selection",
         "name arg  doesn't have Data Masking or Tidy Selection",
         ".drop arg  doesn't have Data Masking or Tidy Selection"),
    list(".data arg  doesn't have Data Masking or Tidy Selection",
         "the ... arg  doesn't have Data Masking or Tidy Selection",
         ".add arg doesn't have Data Masking or Tidy Selection",
         ".drop arg  doesn't have Data Masking or Tidy Selection",
         "x arg  doesn't have Data Masking or Tidy Selection"),
    list(".data arg  doesn't have Data Masking or Tidy Selection",
         "the ... arg have tidy selection",
         ".fn arg  doesn't have Data Masking or Tidy Selection",
         ".cols arg have tidy selection"),
    list(".data arg  doesn't have Data Masking or Tidy Selection",
         "order_by arg have data masking",
         "the ... arg have data masking",
         "n arg  doesn't have Data Masking or Tidy Selection",
         "prop arg  doesn't have Data Masking or Tidy Selection",
         "by arg have tidy selection",
         "with_ties arg  doesn't have Data Masking or Tidy Selection",
         "na_rm arg  doesn't have Data Masking or Tidy Selection"),
    list(".data arg  doesn't have Data Masking or Tidy Selection",
         "the ... arg have data masking",
         "n arg  doesn't have Data Masking or Tidy Selection",
         "by arg have tidy selection",
         "weight_by arg have data masking",
         "replace arg doesn't have Data Masking or Tidy Selection")
  )
) 

i want to produce table like below

enter image description here

i use the pivot_wider() like this

    data |> unnest_longer(arguments,keep_empty = TRUE) |> 
   mutate(id=row_number()) |> 
pivot_wider(
  names_from = id,
  values_from=arguments,
  
)

but still confuse from using the 2 group in gt()..

hy. can someone help me with this code to produce in gt. the data is here

data <- tibble(
  Function = c("distinct()","count()","group_by()",
               "rename_with()","slice_min()","slice_sample()"),
  Description = c("keep only unique rows from a data frame",
                  "count the unique values of one or more var",
                  "group by one or more variables",
                  "renames columns using a function",
                  "select rows with the smallest values of a variable",
                  "randomly selects rows"),
  arguments = list(
    list("the data argument doesn't have Data Masking or Tidy Selection", 
         "the ... use data masking",
         "the .keep_all arg  doesn't have Data Masking or Tidy Selection"),
    list("x arg  doesn't have Data Masking or Tidy Selection",
         "the ... arg have data masking",
         "the wt arg have data masking",
         "sort arg  doesn't have Data Masking or Tidy Selection",
         "name arg  doesn't have Data Masking or Tidy Selection",
         ".drop arg  doesn't have Data Masking or Tidy Selection"),
    list(".data arg  doesn't have Data Masking or Tidy Selection",
         "the ... arg  doesn't have Data Masking or Tidy Selection",
         ".add arg doesn't have Data Masking or Tidy Selection",
         ".drop arg  doesn't have Data Masking or Tidy Selection",
         "x arg  doesn't have Data Masking or Tidy Selection"),
    list(".data arg  doesn't have Data Masking or Tidy Selection",
         "the ... arg have tidy selection",
         ".fn arg  doesn't have Data Masking or Tidy Selection",
         ".cols arg have tidy selection"),
    list(".data arg  doesn't have Data Masking or Tidy Selection",
         "order_by arg have data masking",
         "the ... arg have data masking",
         "n arg  doesn't have Data Masking or Tidy Selection",
         "prop arg  doesn't have Data Masking or Tidy Selection",
         "by arg have tidy selection",
         "with_ties arg  doesn't have Data Masking or Tidy Selection",
         "na_rm arg  doesn't have Data Masking or Tidy Selection"),
    list(".data arg  doesn't have Data Masking or Tidy Selection",
         "the ... arg have data masking",
         "n arg  doesn't have Data Masking or Tidy Selection",
         "by arg have tidy selection",
         "weight_by arg have data masking",
         "replace arg doesn't have Data Masking or Tidy Selection")
  )
) 

i want to produce table like below

enter image description here

i use the pivot_wider() like this

    data |> unnest_longer(arguments,keep_empty = TRUE) |> 
   mutate(id=row_number()) |> 
pivot_wider(
  names_from = id,
  values_from=arguments,
  
)

but still confuse from using the 2 group in gt()..

Share Improve this question asked Mar 19 at 17:21 lie ardilie ardi 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

gt package exports a function fmt_markdown() that renders markdown text in a table.

You could modify your arguments column to add the following markdown syntax.

  • code formatting: `your code`
  • bulleted list: - item1\n - item2\n ...
  • text color: <a style='color:your_color'>some_text</a>
  • text highlight: <a style='backgroud-color:your_color'>some_text</a>
  • bold text: **some_text**
library(gt)
library(dplyr)

data |> 
  mutate(arguments = sapply(arguments, function(x) paste0("- ", x, collapse="\n")),
         arguments = gsub("data argument", "<a style='color:purple;background-color:#F0F0F0'>**`data`**</a> argument", arguments),
         arguments = gsub("(\\.data|\\.cols|\\.{3}|\\.keep_all)",
                          "<a style='color:purple;background-color:#F0F0F0'>**`\\1`**</a>",
                          arguments),
         Function = paste0("<a style='color:purple;background-color:#F0F0F0'>`", Function, "`</a>")) |> 
  gt() |> 
  fmt_markdown()

发布评论

评论列表(0)

  1. 暂无评论