Wrap text for PDF

Hello,

I'm currently working on printing a tibble onto a PDF, but I've ran into a formatting problem:

The tibble has 2 character columns, and one of them can sometimes be very long like the length of a paragraph. The result almost always takes more than one page of pdf, so I wrote a janky piece of code that calculates page number based on the number of newlines. I've provided the code used currently to create the PDF. Is there a way to wrap the text so that it formats nicely on the PDF?

The following is one page of my current result:

full pdf:
somethings.pdf (36.9 KB)

The code I use right now to create this is here:

# Use to create objects to use in example
new_objs <- data.frame(type = character(),
                       id = character(),
                       main_text = character(),
                       stringsAsFactors = FALSE)

for (i in seq_along(1:100)){
big_txt = "The filter() function is used to subset a data frame, retaining all rows that satisfy your conditions. To be retained, the row must produce a value of TRUE for all conditions. Note that when a condition evaluates to NA the row will be dropped, unlike base subsetting with [."
type_ = "exercise_submission"
id_ = paste0("thing_", i)
new_objs <- rbind(new_objs, c(type = type_, id = id_, main_text = big_txt))
}

# Actual Function
createPDF <- function(objs, fname){
    pdf_df <- data.frame(
        type_ = character(),
        id_ = character(),
        user_input = character(),
        page_num = integer(),
        stringsAsFactors=FALSE
    )
    
    # Threshold of number of text rows in one page
    
    page_threshold <- 20
    
    curr_rows <- 0
    
    curr_page <- 0
    
    
    for(i in 1:nrow(objs)) {
        obj <- objs[i,]
        # If we are at or past the threshold, we have enough data for one page
        # so we append the current df to df_list and reset the df
        
        obj_type <- obj$type[[1]]
        
        obj_answer <- ""
        
        if (obj_type == "exercise_submission"){
            obj_answer <- stringr::str_wrap(trimws(obj$main_text[[1]]), 45)
        } else{
            obj_answer <-  stringr::str_wrap(trimws(obj$main_text[[1]]), 45)
        }
        
        # Increment curr_rows by number of newlines
        
        curr_rows <- curr_rows + stringr::str_count(obj_answer, "\n")
        
        if (curr_rows >= page_threshold){
            curr_page <- curr_page + 1
            curr_rows <- 0
        }
        
        obj_id <- obj$id[[1]]
        
        pdf_df <- rbind(pdf_df, c(type_ = obj_type, id_ = obj_id, user_input = obj_answer, page_num = curr_page))
    }
    
    colnames(pdf_df) <- c("type_", "id_", "user_input", "page_num")
    pdf_df$page_num <- factor(pdf_df$page_num)
    
    # Create PDF
    
    grDevices::pdf(fname, height = 11, width = 20)
    
    # Write dataframe to corresponding page on PDF
    
    for (num in sort(parse_integer(levels(pdf_df$page_num)))){
        print(num)
        grid::grid.newpage()
        
        gridExtra::grid.table(dplyr::select(dplyr::filter(pdf_df, pdf_df$page_num == toString(num)), -page_num))
        
        
    }
    
    # Close PDF
    
    grDevices::dev.off()
}


# Calling the function to create the PDF
createPDF(new_objs, "/path/to/location.pdf")

Thank you in advance for your help!

Wrapping columns is mentioned in
https://stackoverflow.com/questions/29425499/wrap-long-text-in-kable-table-column
with aid of one of the packages pander or kableExtra .

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.