Javi_VM
Javi_VM

Reputation: 515

Convert list to bullet-points

If I have a list of characters: list("bannana", "apple", "orange"), I would like to get a R chunk with the output:

- bannana
- apple
- orange

Moreover, with a list with structure list(fruits=list("bannana", "apple", "orange"), meat=list("beef", "chicken")), I would like to get:

- fruits
  - bannana
  - apple
  - orange
- meat:
  - beef
  - chicken

Is this possible? I could consider also a table as output, but I think that would be more complicated.

Upvotes: 0

Views: 453

Answers (1)

W. Joel Schneider
W. Joel Schneider

Reputation: 1806

For a simple list:

l <- list("bannana", "apple", "orange")
cat(paste0("- ", l, collapse = "\n"))
  • banana
  • apple
  • orange

For a 2-level list, maybe something like this:

library(tidyverse)
list2markdown <- function(l) {
  enframe(l) %>% 
    group_by(name) %>% 
    mutate(items = paste0(
      "- ", 
      name, 
      paste0(
        "\n\t- ", 
        unlist(value), 
        collapse = ""))) %>% 
    pull(items) %>% 
    paste0(collapse = "\n") %>% 
    cat()
}

l <- list(fruits = list("bannana", "apple", "orange"), 
          meat = list("beef", "chicken"))

list2markdown(l)
  • fruits
    • bannana
    • apple
    • orange
  • meat
    • beef
    • chicken

Upvotes: 1

Related Questions