Monday, April 18, 2011

pre-generate pictures of your knitting

[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.


  1. Hi, David,

    I am the 'someone on'

    The R graphics are better. The code is lovely.

    I think this might be the end of my plan to create a website that does this, though.


  2. Hi Karla!

    Thanks for writing. When Allison didn't hear back from you at Ravelry, we didn't know whether you wanted to be 'credited' with having the idea. But if you don't mind, I'd like to edit the post to reflect that this was really your idea.

    I like giving people code to do this themselves with free software (like R), but I don't know why this should should be the end of your plan to create a website for it! I'm guessing that most of the people who could enjoy these plots will never see my blog post, and also that most of the people who do see this post will never download R. So I definitely think there could be value in a website for this! I know it's possible to make web applications based on R (but I don't know how), but also I don't really know that R would be the best tool for that purpose anyway. I'd love to hear any of your thoughts on this!

    Please also feel free to get in touch with me ( if you'd like to talk outside of blog comments.

    Great idea! Thanks again.

  3. This comment has been removed by the author.

  4. I made a website for this!

  5. You made a website for an idea someone else had and was actively working with?

  6. Who are you?

    No, Karla had made a SAS script that does this, but wasn't actively working on doing anything else with it, I believe. Karla is appropriately credited with the original idea here and at the site.

  7. Thank you so much for your generosity. I hate math and this makes planned pooling so much more achievable for me.

  8. This comment has been removed by the author.

  9. Everyone wants to get unique place in the IT industry’s for that you need to upgrade your skills, your blog helps me improvise my skill set to get good career, keep sharing your thoughts with us.

    SAS Online Training