pre-generate pictures of your knitting

[This article was first published on David Chudzicki's Blog, 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.

[Update: I’ve made a website for this:]

This was a birthday present for my spouse. (Don’t worry–I also covered a lot of things — fruit/nuts/cocoa puffs/etc — in chocolate. But I think both were appreciated!)

Sometimes people knit with with yarn that has a repeating sequence of colors — for example, purple, then yellow, then green — in a more-or-less regular repeating pattern. Most frequently, the colors in the finished product combine without making much of a distinguishable pattern (somewhat like the picture in the lower right, below). But sometimes stitches of the same color group together (as in the upper right picture), and sometimes other patterns emerge.

Inspired by someone at who seems to have done the same thing in SAS, I’ve written some R code that tries to predict what sort of pattern will emerge in the finishes product based on the repeating pattern of colors in the yarn (how many stitches of each color?), the width (in stitches) of the square, and whether your knitting is “flat” (back and forth — starts next row where last one finished) or “circular” (starts the next row where the last one started).

Here’s the function call that generated these plots:

repeats = c(17,5,17,5),
colors = c(“#AC00FF”,rgb(.71,.5,.9),”green”,”gold”),
widths = c(46,52,62,82),
type = “flat”,

Here’s the code that defines this function, which also has an example function call with much more commenting to explain each of the inputs.

I’ve added functions for use in RStudio that adds a slider controlling the width:

I hope knitters may find this useful, or others may find it fun to play with. (Especially people who might want to play with any number theory that might be involved here?)

If you’re new to R, you just have to download R and run the code up through the function call above (well, really just the code defining the two functions used), and then play with it yourself.

To leave a comment for the author, please follow the link and comment on their blog: David Chudzicki's Blog. 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)