<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/a-shiny-app-to-draw-curves-based-on-l-system/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/"> R – Fronkonstin</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div class="twitter-share"><a rel="nofollow" href="https://twitter.com/intent/tweet?url=https%3A%2F%2Ffronkonstin.com%2F2017%2F06%2F26%2Fa-shiny-app-to-draw-curves-based-on-l-systems%2F&via=aschinchon" class="twitter-share-button" target="_blank">Tweet</a></div>
<blockquote><p>Don’t worry about a thing ’cause every little thing gonna be alright (Three Little Birds, Bob Marley)</p>
</blockquote>
<p>One of my favourite books is <a rel="nofollow" href="https://mitpress.mit.edu/books/computational-beauty-nature" target="_blank"><em>The Computational Beauty of Nature</em></a> by Gary William Flake where there is a fantastic chapter about fractals in which I discovered the <a rel="nofollow" href="https://en.wikipedia.org/wiki/L-system" target="_blank">L-Systems</a>.</p>
<p>L-Systems were conceived  in 1968 by Aristide Lindenmayer, a Hungarian biologist, as a mathematical description of plant growth. Apart from the Wikipedia, there are many places on the Internet where you can read about them. If you are interested, don’t miss The Algorithmic Beauty of Plants, an awesome book by Przemysław Prusinkiewicz that you can obtain <a rel="nofollow" href="http://algorithmicbotany.org/papers/#abop" target="_blank">here</a> for free.</p>
<p>Roughly speaking, a L-System is a very efficient way to <em>make drawings</em>. In its simplest way consists in two different actions: <em>draw a straigh line</em> and <em>change the angle</em>. This is just what you need, for example, to draw a square: draw a straigh line of  any length, turn 90 degrees (without drawing), draw another straigh line of the same length, turn 90 degrees in the same direction, draw, turn and draw again. Denoting <code>F</code> as the action of <em>drawing a line of length d</em> and <code>+</code> as <em>turning 90 degrees right</em>, the whole process to draw a square can be represented as <code>F+F+F+F</code>.</p>
<p>L-Systems are quite simple to program in R. You only need to substitute the rules iteratively into the axiom (I use <code>gsubfn</code> function to do it) and split the resulting chain into parts with <code>str_extract_all</code>, for example. The result is a set of very simple actions (<em>draw</em> or <em>turn</em>) that can be visualized with <code>ggplot</code> and its path geometry. There are four important parameters in L-Systems:</p>
<ul>
<li>The seed of the drawing, called <strong>axiom</strong></li>
<li>The <em>substitutions</em> to be applied iteratively, called <strong>rules</strong></li>
<li>How many times to apply substitutions, called <strong>depth</strong></li>
<li><strong>Angle</strong> of each turning</li>
</ul>
<p>For example, let’s define the next L-System:</p>
<ul>
<li><strong>Axiom:</strong> <code>F-F-F-F</code></li>
<li><strong>Rule:</strong> <code>F → F−F+F+FF−F−F+F</code></li>
</ul>
<p>The rule means that every <code>F</code> must be replaced by <code>F−F+F+FF−F−F+F</code> while <code>+</code> means right turning and <code>-</code> left one. After one iteration, the axiom is replaced by <code>F-F+F+FF-F-F+F-F-F+F+FF-F-F+F-F-F+F+FF-F-F+F-F-F+F+FF-F-F+F</code> and iterating again, the new string is <code>F-F+F+FF-F-F+F-F-F+F+FF-F-F+F+F-F+F+FF-F-F+F+F-F+F+FF-F-F+FF-F+F+FF-F-F+F-F-F+F+FF-F-F+F-F-F+F+FF-F-F+F+F-F+F+FF-F-F+F-F-F+F+FF-F-F+F-F-F+F+FF-F-F+F+F-F+F+FF-F-F+F+F-F+F+FF-F-F+FF-F+F+FF-F-F+F-F-F+F+FF-F-F+F-F-F+F+FF-F-F+F+F-F+F+FF-F-F+F-F-F+F+FF-F-F+F-F-F+F+FF-F-F+F+F-F+F+FF-F-F+F+F-F+F+FF-F-F+FF-F+F+FF-F-F+F-F-F+F+FF-F-F+F-F-F+F+FF-F-F+F+F-F+F+FF-F-F+F-F-F+F+FF-F-F+F-F-F+F+FF-F-F+F+F-F+F+FF-F-F+F+F-F+F+FF-F-F+FF-F+F+FF-F-F+F-F-F+F+FF-F-F+F-F-F+F+FF-F-F+F+F-F+F+FF-F-F+F</code>. As you can see, the length of the string grows exponentially. Converting last string into <em>actions</em>, produces this drawing, called Koch Island:</p>
<p><img data-orig-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/KOCH2.png?fit=350%2C350&amp;ssl=1" data-orig-size="350,350" data-medium-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/KOCH2.png?fit=300%2C300&amp;ssl=1" data-attachment-id="4163" data-image-meta="{" data-image-description="" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/KOCH2.png?resize=350%2C350&ssl=1" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/koch2/" data-large-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/KOCH2.png?fit=350%2C350&amp;ssl=1" alt="" data-image-title="KOCH2" sizes="(max-width: 350px) 100vw, 350px" data-comments-opened="1" class="wp-image-4163" data-recalc-dims="1">It is funny how different axioms and rules produce very different drawings. I have done a Shiny App to play with L-systems. Although it is quite simple, it has two interesting features I would like to undeline:</p>
<ul>
<li><strong>Delay reactions</strong> with <code>eventReactive</code> to allow to set depth and angle values before refreshing the plot</li>
<li><strong>Build a dynamic UI that reacts to user input</strong> depending on the curve choosen</li>
</ul>
<p>There are twelve curves in the application: Koch Island (and 6 variations), cuadratic snowflake, Sierpinsky triangle, hexagonal Gosper, quadratic Gosper and Dragon curve. These are their plots:</p>
<p><a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/cuadratic-gosper/" target="_blank"><img data-orig-file="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Cuadratic-Gosper.png?fit=1240%2C742&amp;ssl=1" data-orig-size="1240,742" data-medium-file="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Cuadratic-Gosper.png?fit=300%2C180&amp;ssl=1" data-attachment-id="4104" data-image-meta="{" src="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Cuadratic-Gosper.png?fit=300%2C180&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/cuadratic-gosper/" data-large-file="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Cuadratic-Gosper.png?fit=604%2C362&amp;ssl=1" alt="" data-image-title="Cuadratic Gosper" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="180"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/cuadratic-koch/" target="_blank"><img data-orig-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Cuadratic-Koch.png?fit=1240%2C742&amp;ssl=1" data-orig-size="1240,742" data-medium-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Cuadratic-Koch.png?fit=300%2C180&amp;ssl=1" data-attachment-id="4105" data-image-meta="{" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Cuadratic-Koch.png?fit=300%2C180&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/cuadratic-koch/" data-large-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Cuadratic-Koch.png?fit=604%2C362&amp;ssl=1" alt="" data-image-title="Cuadratic Koch" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="180"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/dragon-curve/" target="_blank"><img data-orig-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Dragon-Curve.png?fit=1240%2C742&amp;ssl=1" data-orig-size="1240,742" data-medium-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Dragon-Curve.png?fit=300%2C180&amp;ssl=1" data-attachment-id="4106" data-image-meta="{" src="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Dragon-Curve.png?fit=300%2C180&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/dragon-curve/" data-large-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Dragon-Curve.png?fit=604%2C362&amp;ssl=1" alt="" data-image-title="Dragon Curve" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="180"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/hexagonal-gosper/" target="_blank"><img data-orig-file="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Hexagonal-Gosper.png?fit=1218%2C825&amp;ssl=1" data-orig-size="1218,825" data-medium-file="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Hexagonal-Gosper.png?fit=300%2C203&amp;ssl=1" data-attachment-id="4107" data-image-meta="{" src="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Hexagonal-Gosper.png?fit=300%2C203&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/hexagonal-gosper/" data-large-file="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Hexagonal-Gosper.png?fit=604%2C409&amp;ssl=1" alt="" data-image-title="Hexagonal Gosper" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="203"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/koch-curve/" target="_blank"><img data-orig-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-Curve.png?fit=1240%2C742&amp;ssl=1" data-orig-size="1240,742" data-medium-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-Curve.png?fit=300%2C180&amp;ssl=1" data-attachment-id="4108" data-image-meta="{" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-Curve.png?fit=300%2C180&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/koch-curve/" data-large-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-Curve.png?fit=604%2C362&amp;ssl=1" alt="" data-image-title="Koch Curve" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="180"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/koch-v1/" target="_blank"><img data-orig-file="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V1.png?fit=1240%2C742&amp;ssl=1" data-orig-size="1240,742" data-medium-file="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V1.png?fit=300%2C180&amp;ssl=1" data-attachment-id="4109" data-image-meta="{" src="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V1.png?fit=300%2C180&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/koch-v1/" data-large-file="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V1.png?fit=604%2C362&amp;ssl=1" alt="" data-image-title="Koch V1" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="180"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/koch-v2/" target="_blank"><img data-orig-file="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V2.png?fit=1240%2C742&amp;ssl=1" data-orig-size="1240,742" data-medium-file="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V2.png?fit=300%2C180&amp;ssl=1" data-attachment-id="4110" data-image-meta="{" src="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V2.png?fit=300%2C180&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/koch-v2/" data-large-file="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V2.png?fit=604%2C362&amp;ssl=1" alt="" data-image-title="Koch V2" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="180"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/koch-v3/" target="_blank"><img data-orig-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V3.png?fit=1240%2C742&amp;ssl=1" data-orig-size="1240,742" data-medium-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V3.png?fit=300%2C180&amp;ssl=1" data-attachment-id="4111" data-image-meta="{" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V3.png?fit=300%2C180&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/koch-v3/" data-large-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V3.png?fit=604%2C362&amp;ssl=1" alt="" data-image-title="Koch V3" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="180"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/koch-v4/" target="_blank"><img data-orig-file="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V4.png?fit=1240%2C742&amp;ssl=1" data-orig-size="1240,742" data-medium-file="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V4.png?fit=300%2C180&amp;ssl=1" data-attachment-id="4112" data-image-meta="{" src="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V4.png?fit=300%2C180&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/koch-v4/" data-large-file="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V4.png?fit=604%2C362&amp;ssl=1" alt="" data-image-title="Koch V4" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="180"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/koch-v5/" target="_blank"><img data-orig-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V5.png?fit=1218%2C825&amp;ssl=1" data-orig-size="1218,825" data-medium-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V5.png?fit=300%2C203&amp;ssl=1" data-attachment-id="4113" data-image-meta="{" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V5.png?fit=300%2C203&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/koch-v5/" data-large-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V5.png?fit=604%2C409&amp;ssl=1" alt="" data-image-title="Koch V5" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="203"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/koch-v6/" target="_blank"><img data-orig-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V6.png?fit=1240%2C742&amp;ssl=1" data-orig-size="1240,742" data-medium-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V6.png?fit=300%2C180&amp;ssl=1" data-attachment-id="4114" data-image-meta="{" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V6.png?fit=300%2C180&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/koch-v6/" data-large-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Koch-V6.png?fit=604%2C362&amp;ssl=1" alt="" data-image-title="Koch V6" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="180"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/sierpinsky/" target="_blank"><img data-orig-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Sierpinsky.png?fit=1240%2C742&amp;ssl=1" data-orig-size="1240,742" data-medium-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Sierpinsky.png?fit=300%2C180&amp;ssl=1" data-attachment-id="4115" data-image-meta="{" src="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Sierpinsky.png?fit=300%2C180&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/sierpinsky/" data-large-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/Sierpinsky.png?fit=604%2C362&amp;ssl=1" alt="" data-image-title="Sierpinsky" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="180"></a></p>
<p>The definition of all these curves (axiom and rules) can be found in the <a rel="nofollow" href="http://algorithmicbotany.org/papers/abop/abop-ch1.pdf" target="_blank">first chapter of the Prusinkiewicz’s book</a>. The <em>magic</em> comes when you modify angles and colors. These are some examples among the infinite number of possibilities that can be created:</p>
<p><a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve17/" target="_blank"><img data-orig-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve17.png?fit=1240%2C742&amp;ssl=1" data-orig-size="1240,742" data-medium-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve17.png?fit=300%2C180&amp;ssl=1" data-attachment-id="4143" data-image-meta="{" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve17.png?fit=300%2C180&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve17/" data-large-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve17.png?fit=604%2C362&amp;ssl=1" alt="" data-image-title="curve17" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="180"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve16/" target="_blank"><img data-orig-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve16.png?fit=1240%2C742&amp;ssl=1" data-orig-size="1240,742" data-medium-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve16.png?fit=300%2C180&amp;ssl=1" data-attachment-id="4142" data-image-meta="{" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve16.png?fit=300%2C180&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve16/" data-large-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve16.png?fit=604%2C362&amp;ssl=1" alt="" data-image-title="curve16" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="180"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve15/" target="_blank"><img data-orig-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve15.png?fit=1240%2C742&amp;ssl=1" data-orig-size="1240,742" data-medium-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve15.png?fit=300%2C180&amp;ssl=1" data-attachment-id="4141" data-image-meta="{" src="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve15.png?fit=300%2C180&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve15/" data-large-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve15.png?fit=604%2C362&amp;ssl=1" alt="" data-image-title="curve15" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="180"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve12/" target="_blank"><img data-orig-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve12.png?fit=1240%2C742&amp;ssl=1" data-orig-size="1240,742" data-medium-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve12.png?fit=300%2C180&amp;ssl=1" data-attachment-id="4138" data-image-meta="{" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve12.png?fit=300%2C180&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve12/" data-large-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve12.png?fit=604%2C362&amp;ssl=1" alt="" data-image-title="curve12" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="180"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve10/" target="_blank"><img data-orig-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve10.png?fit=1240%2C742&amp;ssl=1" data-orig-size="1240,742" data-medium-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve10.png?fit=300%2C180&amp;ssl=1" data-attachment-id="4136" data-image-meta="{" src="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve10.png?fit=300%2C180&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve10/" data-large-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve10.png?fit=604%2C362&amp;ssl=1" alt="" data-image-title="curve10" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="180"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve-b-10/" target="_blank"><img data-orig-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-10.png?fit=1240%2C742&amp;ssl=1" data-orig-size="1240,742" data-medium-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-10.png?fit=300%2C180&amp;ssl=1" data-attachment-id="4125" data-image-meta="{" src="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-10.png?fit=300%2C180&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve-b-10/" data-large-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-10.png?fit=604%2C362&amp;ssl=1" alt="" data-image-title="curve B 10" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="180"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve6/" target="_blank"><img data-orig-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve6.png?fit=1240%2C742&amp;ssl=1" data-orig-size="1240,742" data-medium-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve6.png?fit=300%2C180&amp;ssl=1" data-attachment-id="4132" data-image-meta="{" src="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve6.png?fit=300%2C180&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve6/" data-large-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve6.png?fit=604%2C362&amp;ssl=1" alt="" data-image-title="curve6" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="180"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve7/" target="_blank"><img data-orig-file="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve7.png?fit=1240%2C742&amp;ssl=1" data-orig-size="1240,742" data-medium-file="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve7.png?fit=300%2C180&amp;ssl=1" data-attachment-id="4133" data-image-meta="{" src="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve7.png?fit=300%2C180&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve7/" data-large-file="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve7.png?fit=604%2C362&amp;ssl=1" alt="" data-image-title="curve7" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="180"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve-b-7/" target="_blank"><img data-orig-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-7.png?fit=1240%2C742&amp;ssl=1" data-orig-size="1240,742" data-medium-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-7.png?fit=300%2C180&amp;ssl=1" data-attachment-id="4122" data-image-meta="{" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-7.png?fit=300%2C180&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve-b-7/" data-large-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-7.png?fit=604%2C362&amp;ssl=1" alt="" data-image-title="curve B 7" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="180"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve-b-1/" target="_blank"><img data-orig-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-1.png?fit=1218%2C825&amp;ssl=1" data-orig-size="1218,825" data-medium-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-1.png?fit=300%2C203&amp;ssl=1" data-attachment-id="4116" data-image-meta="{" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-1.png?fit=300%2C203&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve-b-1/" data-large-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-1.png?fit=604%2C409&amp;ssl=1" alt="" data-image-title="curve B 1" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="203"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve-b-4/" target="_blank"><img data-orig-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-4.png?fit=1240%2C742&amp;ssl=1" data-orig-size="1240,742" data-medium-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-4.png?fit=300%2C180&amp;ssl=1" data-attachment-id="4119" data-image-meta="{" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-4.png?fit=300%2C180&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve-b-4/" data-large-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-4.png?fit=604%2C362&amp;ssl=1" alt="" data-image-title="curve B 4" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="180"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve-b-2/" target="_blank"><img data-orig-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-2.png?fit=1218%2C825&amp;ssl=1" data-orig-size="1218,825" data-medium-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-2.png?fit=300%2C203&amp;ssl=1" data-attachment-id="4117" data-image-meta="{" src="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-2.png?fit=300%2C203&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve-b-2/" data-large-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-2.png?fit=604%2C409&amp;ssl=1" alt="" data-image-title="curve B 2" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="203"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve-b-3/" target="_blank"><img data-orig-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-3.png?fit=1234%2C825&amp;ssl=1" data-orig-size="1234,825" data-medium-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-3.png?fit=300%2C201&amp;ssl=1" data-attachment-id="4118" data-image-meta="{" src="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-3.png?fit=300%2C201&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve-b-3/" data-large-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve-B-3.png?fit=604%2C404&amp;ssl=1" alt="" data-image-title="curve B 3" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="201"></a><br>
<a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve3/" target="_blank"><img data-orig-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve3.png?fit=1218%2C825&amp;ssl=1" data-orig-size="1218,825" data-medium-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve3.png?fit=300%2C203&amp;ssl=1" data-attachment-id="4129" data-image-meta="{" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve3.png?fit=300%2C203&ssl=1" data-image-description="" data-permalink="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/curve3/" data-large-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/06/curve3.png?fit=604%2C409&amp;ssl=1" alt="" data-image-title="curve3" sizes="(max-width: 300px) 100vw, 300px" width="300" data-comments-opened="1" height="203"></a></p>
<p>I enjoyed a lot doing and playing with the app. You can try it <a rel="nofollow" href="http://91.134.134.147:3838/curves/" target="_blank">here</a>. If you do a nice drawing, please let me know in Twitter or dropping me an email. This is the code of the App:</p>
<p><strong><code>ui.R</code></strong>:</p>
<pre>
library(shiny)

shinyUI(fluidPage(
  titlePanel(&quot;Curves based on L-systems&quot;),
  
  sidebarLayout(
    sidebarPanel(
      selectInput(&quot;cur&quot;, &quot;Choose a curve:&quot;,
                  c(&quot;&quot;,&quot;Koch Island&quot;,
                    &quot;Cuadratic Snowflake&quot;,
                    &quot;Koch Variation 1&quot;,
                    &quot;Koch Variation 2&quot;,
                    &quot;Koch Variation 3&quot;,
                    &quot;Koch Variation 4&quot;,
                    &quot;Koch Variation 5&quot;,
                    &quot;Koch Variation 6&quot;,
                    &quot;Sierpinsky Triangle&quot;,
                    &quot;Dragon Curve&quot;,
                    &quot;Hexagonal Gosper Curve&quot;,
                    &quot;Quadratic Gosper Curve&quot;),
                  selected = &quot;&quot;),
      
      conditionalPanel(
        condition = &quot;input.cur != \&quot;\&quot;&quot;,
        uiOutput(&quot;Iterations&quot;)),
      
      conditionalPanel(
        condition = &quot;input.cur != \&quot;\&quot;&quot;,
        uiOutput(&quot;Angle&quot;)),
      
      conditionalPanel(
        condition = &quot;input.cur != \&quot;\&quot;&quot;,
        selectInput(&quot;lic&quot;, label = &quot;Line color:&quot;, choices = colors(), selected = &quot;black&quot;)),
      
      
      conditionalPanel(
        condition = &quot;input.cur != \&quot;\&quot;&quot;,
        selectInput(&quot;bac&quot;, label = &quot;Background color:&quot;, choices = colors(), selected = &quot;white&quot;)),
      
      conditionalPanel(
        condition = &quot;input.cur != \&quot;\&quot;&quot;,
        actionButton(inputId = &quot;go&quot;, label = &quot;Go!&quot;, 
                     style=&quot;color: #fff; background-color: #337ab7; border-color: #2e6da4&quot;))
      
      
      
      
    ), 
    mainPanel(plotOutput(&quot;curve&quot;, height=&quot;550px&quot;, width = &quot;100%&quot;))
  )
  
))
</pre>
<p><strong><code>server.R</code></strong>:</p>
<pre>
library(shiny)
library(gsubfn)
library(stringr)
library(dplyr)
library(ggplot2)
library(rlist)

shinyServer(function(input, output) {
   
  curves=list(
    list(name=&quot;Koch Island&quot;,
         axiom=&quot;F-F-F-F&quot;,
         rules=list(&quot;F&quot;=&quot;F-F+F+FF-F-F+F&quot;),
         angle=90,
         n=2,
         alfa0=90),
    list(name=&quot;Cuadratic Snowflake&quot;,
         axiom=&quot;-F&quot;,
         rules=list(&quot;F&quot;=&quot;F+F-F-F+F&quot;),
         angle=90,
         n=4,
         alfa0=90),
    list(name=&quot;Koch Variation 1&quot;,
         axiom=&quot;F-F-F-F&quot;,
         rules=list(&quot;F&quot;=&quot;FF-F-F-F-F-F+F&quot;),
         angle=90,
         n=3,
         alfa0=90),
    list(name=&quot;Koch Variation 2&quot;,
         axiom=&quot;F-F-F-F&quot;,
         rules=list(&quot;F&quot;=&quot;FF-F-F-F-FF&quot;),
         angle=90,
         n=4,
         alfa0=90),
    list(name=&quot;Koch Variation 3&quot;,
         axiom=&quot;F-F-F-F&quot;,
         rules=list(&quot;F&quot;=&quot;FF-F+F-F-FF&quot;),
         angle=90,
         n=3,
         alfa0=90),
    list(name=&quot;Koch Variation 4&quot;,
         axiom=&quot;F-F-F-F&quot;,
         rules=list(&quot;F&quot;=&quot;FF-F--F-F&quot;),
         angle=90,
         n=4,
         alfa0=90),
    list(name=&quot;Koch Variation 5&quot;,
         axiom=&quot;F-F-F-F&quot;,
         rules=list(&quot;F&quot;=&quot;F-FF--F-F&quot;),
         angle=90,
         n=5,
         alfa0=90),
    list(name=&quot;Koch Variation 6&quot;,
         axiom=&quot;F-F-F-F&quot;,
         rules=list(&quot;F&quot;=&quot;F-F+F-F-F&quot;),
         angle=90,
         n=4,
         alfa0=90),
    list(name=&quot;Sierpinsky Triangle&quot;,
         axiom=&quot;R&quot;,
         rules=list(&quot;L&quot;=&quot;R+L+R&quot;, &quot;R&quot;=&quot;L-R-L&quot;),
         angle=60,
         n=6,
         alfa0=0),
    list(name=&quot;Dragon Curve&quot;,
         axiom=&quot;L&quot;,
         rules=list(&quot;L&quot;=&quot;L+R+&quot;, &quot;R&quot;=&quot;-L-R&quot;),
         angle=90,
         n=10,
         alfa0=90),
    list(name=&quot;Hexagonal Gosper Curve&quot;,
         axiom=&quot;L&quot;,
         rules=list(&quot;L&quot;=&quot;L+R++R-L--LL-R+&quot;, &quot;R&quot;=&quot;-L+RR++R+L--L-R&quot;),
         angle=60,
         n=4,
         alfa0=60),
    list(name=&quot;Quadratic Gosper Curve&quot;,
         axiom=&quot;-R&quot;,
         rules=list(&quot;L&quot;=&quot;LL-R-R+L+L-R-RL+R+LLR-L+R+LL+R-LR-R-L+L+RR-&quot;, 
                    &quot;R&quot;=&quot;+LL-R-R+L+LR+L-RR-L-R+LRR-L-RL+L+R-R-L+L+RR&quot;),
         angle=90,
         n=2,
         alfa0=90))
  
  output$Iterations &lt;- renderUI({ if (input$cur!=&quot;&quot;) curve=list.filter(curves, name==input$cur) else curve=list.filter(curves, name==&quot;Koch Island&quot;) iterations=list.select(curve, n) %>% unlist
    numericInput(&quot;ite&quot;, &quot;Depth:&quot;, iterations, min = 1, max = (iterations+2))
  })
  
  output$Angle &lt;- renderUI({ curve=list.filter(curves, name==input$cur) angle=list.select(curve, angle) %>% unlist
    numericInput(&quot;ang&quot;, &quot;Angle:&quot;, angle, min = 0, max = 360)
  })
  
  data &lt;- eventReactive(input$go, { curve=list.filter(curves, name==input$cur) axiom=list.select(curve, axiom) %>% unlist
    rules=list.select(curve, rules)[[1]]$rules
    alfa0=list.select(curve, alfa0) %>% unlist
    
    for (i in 1:input$ite) axiom=gsubfn(&quot;.&quot;, rules, axiom)
    actions=str_extract_all(axiom, &quot;\\d*\\+|\\d*\\-|F|L|R|\\[|\\]|\\|&quot;) %>% unlist
    
    points=data.frame(x=0, y=0, alfa=alfa0)
    for (i in 1:length(actions)) 
    {
      if (actions[i]==&quot;F&quot;|actions[i]==&quot;L&quot;|actions[i]==&quot;R&quot;)
      {
        x=points[nrow(points), &quot;x&quot;]+cos(points[nrow(points), &quot;alfa&quot;]*(pi/180))
        y=points[nrow(points), &quot;y&quot;]+sin(points[nrow(points), &quot;alfa&quot;]*(pi/180))
        alfa=points[nrow(points), &quot;alfa&quot;]
        points %>% rbind(data.frame(x=x, y=y, alfa=alfa)) -> points
      }
      else{
        alfa=points[nrow(points), &quot;alfa&quot;]
        points[nrow(points), &quot;alfa&quot;]=eval(parse(text=paste0(&quot;alfa&quot;,actions[i], input$ang)))
      }
    }
    return(points)
  })
  
  output$curve &lt;- renderPlot({    
    ggplot(data(), aes(x, y)) + 
      geom_path(color=input$lic) + 
      coord_fixed(ratio = 1) +
      theme(legend.position=&quot;none&quot;,
            panel.background = element_rect(fill=input$bac),
            panel.grid=element_blank(),
            axis.ticks=element_blank(),
            axis.title=element_blank(),
            axis.text=element_blank())
  })
    
})
</pre>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/"> R – Fronkonstin</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/g7UQ7Dq-RPc" height="1"></a></div>