library(tidyverse)
library(gt)
library(here)
1 Hintergrund
Tabellen in Markdown sind mitunter nervig zu erstellen. Am einfachsten ist es, wenn die Daten in Form einer CSV- oder Excel-Tabelle vorliegen.
Tipp: Große Mengen von (nur) Text (keine Zahlen) sind vielleicht besser nicht in Form einer Tabelle, sondern einer Liste anzuführen.
2 Beispiel-Daten laden
daten_pfad <- "/content/post/2021-07-07-zeitungsartikel-per-api-herunterladen/results_covid1.rds"
d_raw <- read_rds(paste0(here::here(),daten_pfad))
d <-
d_raw[["results_df"]]
glimpse(d)
## Rows: 100
## Columns: 9
## $ author <chr> "Colm Quinn", NA, "soyacincau", "Papa Kuranchie", "Mia Ja…
## $ title <chr> "Saudi Arabia’s Hushed Washington Visit", "District in ce…
## $ description <chr> "The visit of Khalid bin Salman has been met with little …
## $ url <chr> "https://foreignpolicy.com/2021/07/07/saudi-arabia-washin…
## $ url_to_image <chr> "https://foreignpolicy.com/wp-content/uploads/2021/07/sau…
## $ published_at <dttm> 2021-07-07 09:53:28, 2021-07-07 09:52:57, 2021-07-07 09:…
## $ content <chr> "Here is todays Foreign Policy brief: Prince Khalid bin S…
## $ id <chr> NA, NA, NA, NA, "business-insider", NA, "independent", NA…
## $ name <chr> "Foreign Policy", "Israelnationalnews.com", "SoyaCincau.c…
3 Daten aufbereiten
Nur die ersten 50 Zeichen drucken:
d2 <-
d %>%
mutate(across(
.fns = ~ str_sub(.x, end = 50)
)) %>%
slice(1:5)
4 Tabelle 1: gt()
d2 %>%
gt()
author | title | description | url | url_to_image | published_at | content | id | name |
---|---|---|---|---|---|---|---|---|
Colm Quinn | Saudi Arabia’s Hushed Washington Visit | The visit of Khalid bin Salman has been met with l | https://foreignpolicy.com/2021/07/07/saudi-arabia- | https://foreignpolicy.com/wp-content/uploads/2021/ | 2021-07-07 09:53:28 | Here is todays Foreign Policy brief: Prince Khalid | NA | Foreign Policy |
NA | District in central Israel labeled 'Red' due to hi | Israel's Ministry of Health declared the city of K | https://www.israelnationalnews.com/News/Flash.aspx | http://u.a7.org/pictures/799/799624.jpg | 2021-07-07 09:52:57 | Israel's Ministry of Health declared the city of K | NA | Israelnationalnews.com |
soyacincau | 1 Utama: WhatsApp message claiming over 50 people | The management of 1 Utama Shopping Centre today de | https://soyacincau.com/2021/07/07/1-utama-whatsapp | https://cdnen.soyacincau.com/2020/10/201015-1utama | 2021-07-07 09:51:45 | The management of 1 Utama Shopping Centre today de | NA | SoyaCincau.com |
Papa Kuranchie | Covid 19: Kilkeel seafood factory closes due to ou | Kilkeel Seafoods in County Down has temporarily sh | https://www.cityam.com/covid-19-kilkeel-seafood-fa | https://www.cityam.com/wp-content/uploads/2021/07/ | 2021-07-07 09:51:39 | Kilkeel Seafoods in County Down has temporarily sh | NA | City A.M. |
Mia Jankowicz | Trump wanted to use the COVID-19 pandemic to call | Summary List PlacementFormer President Donald Trum | https://www.businessinsider.com/trump-wanted-call- | https://i.insider.com/60e56cefc03e9c0018ab2274?wid | 2021-07-07 09:51:24 | Former President Donald Trump suggested using the | business-insider | Business Insider |
5 Tabelle 2: kable()
d2 %>%
knitr::kable()
author | title | description | url | url_to_image | published_at | content | id | name |
---|---|---|---|---|---|---|---|---|
Colm Quinn | Saudi Arabia’s Hushed Washington Visit | The visit of Khalid bin Salman has been met with l | https://foreignpolicy.com/2021/07/07/saudi-arabia- | https://foreignpolicy.com/wp-content/uploads/2021/ | 2021-07-07 09:53:28 | Here is todays Foreign Policy brief: Prince Khalid | NA | Foreign Policy |
NA | District in central Israel labeled ‘Red’ due to hi | Israel’s Ministry of Health declared the city of K | https://www.israelnationalnews.com/News/Flash.aspx | http://u.a7.org/pictures/799/799624.jpg | 2021-07-07 09:52:57 | Israel’s Ministry of Health declared the city of K | NA | Israelnationalnews.com |
soyacincau | 1 Utama: WhatsApp message claiming over 50 people | The management of 1 Utama Shopping Centre today de | https://soyacincau.com/2021/07/07/1-utama-whatsapp | https://cdnen.soyacincau.com/2020/10/201015-1utama | 2021-07-07 09:51:45 | The management of 1 Utama Shopping Centre today de | NA | SoyaCincau.com |
Papa Kuranchie | Covid 19: Kilkeel seafood factory closes due to ou | Kilkeel Seafoods in County Down has temporarily sh | https://www.cityam.com/covid-19-kilkeel-seafood-fa | https://www.cityam.com/wp-content/uploads/2021/07/ | 2021-07-07 09:51:39 | Kilkeel Seafoods in County Down has temporarily sh | NA | City A.M. |
Mia Jankowicz | Trump wanted to use the COVID-19 pandemic to call | Summary List PlacementFormer President Donald Trum | https://www.businessinsider.com/trump-wanted-call- | https://i.insider.com/60e56cefc03e9c0018ab2274?wid | 2021-07-07 09:51:24 | Former President Donald Trump suggested using the | business-insider | Business Insider |
6 Tabelle 3: pander
Hier muss man als Chunk-Option noch setzen:
{r results = "asis"}
.
pander::pandoc.table(d2)
author | title | description |
---|---|---|
Colm Quinn | Saudi Arabia’s Hushed Washington Visit | The visit of Khalid bin Salman has been met with l |
NA | District in central Israel labeled ‘Red’ due to hi | Israel’s Ministry of Health declared the city of K |
soyacincau | 1 Utama: WhatsApp message claiming over 50 people | The management of 1 Utama Shopping Centre today de |
Papa Kuranchie | Covid 19: Kilkeel seafood factory closes due to ou | Kilkeel Seafoods in County Down has temporarily sh |
Mia Jankowicz | Trump wanted to use the COVID-19 pandemic to call | Summary List PlacementFormer President Donald Trum |
url_to_image | published_at |
---|---|
https://foreignpolicy.com/wp-content/uploads/2021/ | 2021-07-07 09:53:28 |
http://u.a7.org/pictures/799/799624.jpg | 2021-07-07 09:52:57 |
https://cdnen.soyacincau.com/2020/10/201015-1utama | 2021-07-07 09:51:45 |
https://www.cityam.com/wp-content/uploads/2021/07/ | 2021-07-07 09:51:39 |
https://i.insider.com/60e56cefc03e9c0018ab2274?wid | 2021-07-07 09:51:24 |
content | id | name |
---|---|---|
Here is todays Foreign Policy brief: Prince Khalid | NA | Foreign Policy |
Israel’s Ministry of Health declared the city of K | NA | Israelnationalnews.com |
The management of 1 Utama Shopping Centre today de | NA | SoyaCincau.com |
Kilkeel Seafoods in County Down has temporarily sh | NA | City A.M. |
Former President Donald Trump suggested using the | business-insider | Business Insider |
7 Tabelle 4: datatable()
Hier finden sich Hinweise zu den Optionen.
Mit viel Text klappt es nicht so gut aus dem Stegreif:
DT::datatable(d2,
options = list(
autoWidth = TRUE,
columnDefs = list(list(width = '200px'))))
8 Fazit:
Die Tabelle mit gt()
macht den saubersten Eindruck.
Die Links werden direkt angezeigt 👍.