martinctc
8/23/2017 - 2:59 PM

waffle_maker: create a named list object in R with two vectors. Optimised to be used with the waffle::waffle() function for creating waffle

[Named List Creator] Create a named list object in R with two vectors. Optimised to be used with the waffle::waffle() function for creating waffle chartsx: names/labels; y: values #R

waffle_maker <-
  function(x, y){
    quostr <- function(list){
      options(useFancyQuotes = FALSE)
      list2 <- lapply(list,function(x)toString(dQuote(x)))
      list2
    }
    
    weave <- function(list1, list2, num){
      paste0(list1[num],"=",list2[num])
    }
    
    len <- length(x)
    
    temp <- sapply(1:len,weave, list1=quostr(x),list2=y)
    temp <- paste(temp, collapse = ", ")
    temp <- paste0("c(",temp,")")
    temp2 <- eval(parse(text=temp))
    temp2
  }

#Example for using it with a waffle chart
#waffle_maker(c("a","b","c","d"),c(1, 2, 3, 4)) %>% waffle(rows=2)