Little useless-useful R functions – R Lorem Ipsum

[This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Lorem Ipsum is simply dummy text of the printing and typesetting industry and extensively popularized in the past 60 years.

It’s main purpose it more than obvious, in addition the fact is that a reader is not distracted by readable content and is focusing on layout, outlook or design of a page (or specimen).

Idea is to have a Lorem Ipsum text for R community, randomly generated from all the functions available in base package. You might be thinking that users might get distracted by reading the content, but randomly selected names of the functions is as much readable as Lorem Ipsum text itself. But there is a twist. You might actually spot a function that you never heard of, when merely glancing over the text or when copy and pasting it in your design.

The code is consist of two steps. First one grabs all functions from base package:

function_list <- function(){
  lw <- builtins() #(internal = FALSE)
  lw2 <- help(package="base") 
  lw3 <- ls("package:base")
  lwA <- c(lw,lw2,lw3)
  lwA <- unique(lwA)
  lwA <- trimws(gsub("[[:punct:]]", " ", lwA))
  #ltrim / rtrim
  return(lwA)
  }

And the second part generates the length of the text based on the limitations of length and approximation.

RLoremIpsum <- function(text_length, approx=TRUE){
  lw <- function_list()
  LorIps <- ''
  while (nchar(LorIps) < text_length) {
    lw <- gsub("^ *|(?<= ) | *$", "", lw, perl = TRUE)
    new_w <-  sample(lw,1, replace=TRUE)
    LorIps <- paste(LorIps, new_w, sep = " ")
    if (approx==FALSE){
    LorIps <- substr(LorIps, 1, text_length)
    }
  }
 
  last_word <- tail(strsplit(LorIps ,split=" ")[[1]],1)
     if ((nchar(last_word) == 1) == TRUE) {
    LorIps <- substr(LorIps, 1, nchar(LorIps)-1) # replace last char with blank space
    }
   return(LorIps)
}

And simply generating the text:

# generated Lorem Ipsum with 1000 characters
RLoremIpsum(10000, approx=TRUE)

There is your random text filler. But this time, forget the Latin, hello R 🙂

As always, code is available on Github.

Happy R-coding 🙂

To leave a comment for the author, please follow the link and comment on their blog: R – TomazTsql.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)