From 0766d5a32566b7844d119adb6167862a2219da07 Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Fri, 6 Apr 2018 23:15:09 -0700 Subject: [PATCH] nest_paragraphs carry over other columns --- DESCRIPTION | 2 +- R/nest_paragraphs.R | 20 ++++++++++++++++---- vignettes/different-features.Rmd | 3 ++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 60a8343..3322d74 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: ggpage Type: Package Title: Creates Page Layout Visualizations in R -Version: 0.2.0 +Version: 0.2.1 Authors@R: person("Emil", "Hvitfeldt", email = "emilhhvitfeldt@gmail.com", role = c("aut", "cre")) Description: This package facilitates the creation of page layout visualizations in which words are represented as rectangles with sizes diff --git a/R/nest_paragraphs.R b/R/nest_paragraphs.R index d92776c..163faf3 100644 --- a/R/nest_paragraphs.R +++ b/R/nest_paragraphs.R @@ -11,9 +11,21 @@ nest_paragraphs <- function(data, input, ...) { quo_input <- rlang::quo_name(rlang::enquo(input)) - data[[quo_input]] %>% + sections <- data[[quo_input]] %>% stringr::str_wrap(...) %>% - stringr::str_split("\n") %>% - unlist() %>% - data.frame(text = ., stringsAsFactors = FALSE) + stringr::str_split("\n") + + purrr::map_df(seq_len(nrow(data)), + ~ data.frame(text = sections[[.x]], stringsAsFactors = FALSE) %>% + bind_cols( + bind_rows( + replicate( + length(sections[[.x]]), + dplyr::select(data, -quo_input)[.x, ], + simplify = FALSE) + ) + ) + ) } + + diff --git a/vignettes/different-features.Rmd b/vignettes/different-features.Rmd index 498af5f..bb6ffa7 100644 --- a/vignettes/different-features.Rmd +++ b/vignettes/different-features.Rmd @@ -116,7 +116,7 @@ ggpage_build(tinderbox, align = "both") %>% Lastly for convience does `ggpage` include a way to transform a data.frame with paragraph tokens to a data.frame with line tokens. ```{r} -nest_paragraphs(tinderbox_paragraph, text) %>% +nest_paragraphs(data = tinderbox_paragraph, input = text) %>% head() nest_paragraphs(tinderbox_paragraph, text, width = 40) %>% @@ -126,3 +126,4 @@ nest_paragraphs(tinderbox_paragraph, text, width = 40) %>% +