Shiny slider examples with the intrval R package

[This article was first published on Peter Solymos - R related posts, 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.

The intrval R package is lightweight (~11K), standalone (apart from importing from graphics, has exactly 0 non-base dependency), and it has a very narrow scope: it implements relational operators for intervals — very well aligned with the tiny manifesto. In this post we will explore the use of the package in two shiny apps with sliders.

The first example uses a regular slider that returns a single value. To make that an interval, we will use standard deviation (SD, sigma) in a quality control chart (QCC). The code is based on the pistonrings data set from the qcc package. The Shewhart chart sets 3-_sigma_ limit to indicate state of control. The slider is used to adjusts the sigma limit and the GIF below plays is as an animation.

<span class="n">library</span><span class="p">(</span><span class="n">shiny</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">intrval</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">qcc</span><span class="p">)</span><span class="w">

</span><span class="n">data</span><span class="p">(</span><span class="n">pistonrings</span><span class="p">)</span><span class="w">
</span><span class="n">mu</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">mean</span><span class="p">(</span><span class="n">pistonrings</span><span class="o">$</span><span class="n">diameter</span><span class="p">[</span><span class="n">pistonrings</span><span class="o">$</span><span class="n">trial</span><span class="p">])</span><span class="w">
</span><span class="n">SD</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">sd</span><span class="p">(</span><span class="n">pistonrings</span><span class="o">$</span><span class="n">diameter</span><span class="p">[</span><span class="n">pistonrings</span><span class="o">$</span><span class="n">trial</span><span class="p">])</span><span class="w">
</span><span class="n">x</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">pistonrings</span><span class="o">$</span><span class="n">diameter</span><span class="p">[</span><span class="o">!</span><span class="n">pistonrings</span><span class="o">$</span><span class="n">trial</span><span class="p">]</span><span class="w">

</span><span class="c1">## UI function</span><span class="w">
</span><span class="n">ui</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">fluidPage</span><span class="p">(</span><span class="w">
  </span><span class="n">plotOutput</span><span class="p">(</span><span class="s2">"plot"</span><span class="p">),</span><span class="w">
  </span><span class="n">sliderInput</span><span class="p">(</span><span class="s2">"x"</span><span class="p">,</span><span class="w"> </span><span class="s2">"x SD:"</span><span class="p">,</span><span class="w">
    </span><span class="n">min</span><span class="o">=</span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="n">max</span><span class="o">=</span><span class="m">5</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="o">=</span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="n">step</span><span class="o">=</span><span class="m">0.1</span><span class="p">,</span><span class="w">
    </span><span class="n">animate</span><span class="o">=</span><span class="n">animationOptions</span><span class="p">(</span><span class="m">100</span><span class="p">)</span><span class="w">
  </span><span class="p">)</span><span class="w">
</span><span class="p">)</span><span class="w">

</span><span class="c1"># Server logic</span><span class="w">
</span><span class="n">server</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">input</span><span class="p">,</span><span class="w"> </span><span class="n">output</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">output</span><span class="o">$</span><span class="n">plot</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">renderPlot</span><span class="p">({</span><span class="w">
    </span><span class="n">Main</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">paste</span><span class="p">(</span><span class="s2">"Shewhart quality control chart"</span><span class="p">,</span><span class="w">
        </span><span class="s2">"diameter of piston rings"</span><span class="p">,</span><span class="w"> </span><span class="n">sprintf</span><span class="p">(</span><span class="s2">"+/- %.1f SD"</span><span class="p">,</span><span class="w"> </span><span class="n">input</span><span class="o">$</span><span class="n">x</span><span class="p">),</span><span class="w">
        </span><span class="n">sep</span><span class="o">=</span><span class="s2">"\n"</span><span class="p">)</span><span class="w">
    </span><span class="n">iv</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">mu</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">input</span><span class="o">$</span><span class="n">x</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="o">-</span><span class="n">SD</span><span class="p">,</span><span class="w"> </span><span class="n">SD</span><span class="p">)</span><span class="w">
    </span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">pch</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">19</span><span class="p">,</span><span class="w"> </span><span class="n">col</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">%)(%</span><span class="w"> </span><span class="n">iv</span><span class="w"> </span><span class="m">+1</span><span class="p">,</span><span class="w"> </span><span class="n">type</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"b"</span><span class="p">,</span><span class="w">
        </span><span class="n">ylim</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mu</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">5</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="o">-</span><span class="n">SD</span><span class="p">,</span><span class="w"> </span><span class="n">SD</span><span class="p">),</span><span class="w"> </span><span class="n">main</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Main</span><span class="p">)</span><span class="w">
    </span><span class="n">abline</span><span class="p">(</span><span class="n">h</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mu</span><span class="p">)</span><span class="w">
    </span><span class="n">abline</span><span class="p">(</span><span class="n">h</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">iv</span><span class="p">,</span><span class="w"> </span><span class="n">lty</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2</span><span class="p">)</span><span class="w">
  </span><span class="p">})</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="c1">## Run shiny app</span><span class="w">
</span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nf">interactive</span><span class="p">())</span><span class="w"> </span><span class="n">shinyApp</span><span class="p">(</span><span class="n">ui</span><span class="p">,</span><span class="w"> </span><span class="n">server</span><span class="p">)</span><span class="w">
</span>

regular slider

The second example uses range slider returning two values, which is our interval. To spice things up a bit, we combine intervals on two axes to color some random points. The next range slider defines a distance interval and colors the random points inside the ring.

<span class="n">library</span><span class="p">(</span><span class="n">shiny</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">intrval</span><span class="p">)</span><span class="w">

</span><span class="n">set.seed</span><span class="p">(</span><span class="m">1</span><span class="p">)</span><span class="w">
</span><span class="n">n</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">10</span><span class="o">^</span><span class="m">4</span><span class="w">
</span><span class="n">x</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">round</span><span class="p">(</span><span class="n">runif</span><span class="p">(</span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="m">-2</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">),</span><span class="w"> </span><span class="m">2</span><span class="p">)</span><span class="w">
</span><span class="n">y</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">round</span><span class="p">(</span><span class="n">runif</span><span class="p">(</span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="m">-2</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">),</span><span class="w"> </span><span class="m">2</span><span class="p">)</span><span class="w">
</span><span class="n">d</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">round</span><span class="p">(</span><span class="nf">sqrt</span><span class="p">(</span><span class="n">x</span><span class="o">^</span><span class="m">2</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">y</span><span class="o">^</span><span class="m">2</span><span class="p">),</span><span class="w"> </span><span class="m">2</span><span class="p">)</span><span class="w">

</span><span class="c1">## UI function</span><span class="w">
</span><span class="n">ui</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">fluidPage</span><span class="p">(</span><span class="w">
  </span><span class="n">titlePanel</span><span class="p">(</span><span class="s2">"intrval example with shiny"</span><span class="p">),</span><span class="w">
  </span><span class="n">sidebarLayout</span><span class="p">(</span><span class="w">
    </span><span class="n">sidebarPanel</span><span class="p">(</span><span class="w">
      </span><span class="n">sliderInput</span><span class="p">(</span><span class="s2">"bb_x"</span><span class="p">,</span><span class="w"> </span><span class="s2">"x value:"</span><span class="p">,</span><span class="w">
        </span><span class="n">min</span><span class="o">=</span><span class="nf">min</span><span class="p">(</span><span class="n">x</span><span class="p">),</span><span class="w"> </span><span class="n">max</span><span class="o">=</span><span class="nf">max</span><span class="p">(</span><span class="n">x</span><span class="p">),</span><span class="w"> </span><span class="n">value</span><span class="o">=</span><span class="nf">range</span><span class="p">(</span><span class="n">x</span><span class="p">),</span><span class="w">
        </span><span class="n">step</span><span class="o">=</span><span class="nf">round</span><span class="p">(</span><span class="n">diff</span><span class="p">(</span><span class="nf">range</span><span class="p">(</span><span class="n">x</span><span class="p">))</span><span class="o">/</span><span class="m">20</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">),</span><span class="w"> </span><span class="n">animate</span><span class="o">=</span><span class="kc">TRUE</span><span class="w">
      </span><span class="p">),</span><span class="w">
      </span><span class="n">sliderInput</span><span class="p">(</span><span class="s2">"bb_y"</span><span class="p">,</span><span class="w"> </span><span class="s2">"y value:"</span><span class="p">,</span><span class="w">
        </span><span class="n">min</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">min</span><span class="p">(</span><span class="n">y</span><span class="p">),</span><span class="w"> </span><span class="n">max</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">max</span><span class="p">(</span><span class="n">y</span><span class="p">),</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">range</span><span class="p">(</span><span class="n">y</span><span class="p">),</span><span class="w">
        </span><span class="n">step</span><span class="o">=</span><span class="nf">round</span><span class="p">(</span><span class="n">diff</span><span class="p">(</span><span class="nf">range</span><span class="p">(</span><span class="n">y</span><span class="p">))</span><span class="o">/</span><span class="m">20</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">),</span><span class="w"> </span><span class="n">animate</span><span class="o">=</span><span class="kc">TRUE</span><span class="w">
      </span><span class="p">),</span><span class="w">
      </span><span class="n">sliderInput</span><span class="p">(</span><span class="s2">"bb_d"</span><span class="p">,</span><span class="w"> </span><span class="s2">"radial distance:"</span><span class="p">,</span><span class="w">
        </span><span class="n">min</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="n">max</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">max</span><span class="p">(</span><span class="n">d</span><span class="p">),</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="nf">max</span><span class="p">(</span><span class="n">d</span><span class="p">)</span><span class="o">/</span><span class="m">2</span><span class="p">),</span><span class="w">
        </span><span class="n">step</span><span class="o">=</span><span class="nf">round</span><span class="p">(</span><span class="nf">max</span><span class="p">(</span><span class="n">d</span><span class="p">)</span><span class="o">/</span><span class="m">20</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">),</span><span class="w"> </span><span class="n">animate</span><span class="o">=</span><span class="kc">TRUE</span><span class="w">
      </span><span class="p">)</span><span class="w">
    </span><span class="p">),</span><span class="w">
    </span><span class="n">mainPanel</span><span class="p">(</span><span class="w">
      </span><span class="n">plotOutput</span><span class="p">(</span><span class="s2">"plot"</span><span class="p">)</span><span class="w">
    </span><span class="p">)</span><span class="w">
  </span><span class="p">)</span><span class="w">
</span><span class="p">)</span><span class="w">

</span><span class="c1"># Server logic</span><span class="w">
</span><span class="n">server</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">input</span><span class="p">,</span><span class="w"> </span><span class="n">output</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">output</span><span class="o">$</span><span class="n">plot</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">renderPlot</span><span class="p">({</span><span class="w">
    </span><span class="n">iv1</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">%[]%</span><span class="w"> </span><span class="n">input</span><span class="o">$</span><span class="n">bb_x</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">%[]%</span><span class="w"> </span><span class="n">input</span><span class="o">$</span><span class="n">bb_y</span><span class="w">
    </span><span class="n">iv2</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">%[]%</span><span class="w"> </span><span class="n">input</span><span class="o">$</span><span class="n">bb_y</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">%[]%</span><span class="w"> </span><span class="n">input</span><span class="o">$</span><span class="n">bb_x</span><span class="w">
    </span><span class="n">iv3</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">d</span><span class="w"> </span><span class="o">%()%</span><span class="w"> </span><span class="n">input</span><span class="o">$</span><span class="n">bb_d</span><span class="w">
    </span><span class="n">op</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">par</span><span class="p">(</span><span class="n">mfrow</span><span class="o">=</span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="m">2</span><span class="p">))</span><span class="w">
    </span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">,</span><span class="w"> </span><span class="n">pch</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">19</span><span class="p">,</span><span class="w"> </span><span class="n">cex</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0.25</span><span class="p">,</span><span class="w"> </span><span class="n">col</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">iv1</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">iv2</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w">
        </span><span class="n">main</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Intersecting bounding boxes"</span><span class="p">)</span><span class="w">
    </span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">,</span><span class="w"> </span><span class="n">pch</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">19</span><span class="p">,</span><span class="w"> </span><span class="n">cex</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0.25</span><span class="p">,</span><span class="w"> </span><span class="n">col</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">iv3</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w">
         </span><span class="n">main</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Deck the halls:\ndistance range from center"</span><span class="p">)</span><span class="w">  
    </span><span class="n">par</span><span class="p">(</span><span class="n">op</span><span class="p">)</span><span class="w">
  </span><span class="p">})</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="c1">## Run shiny app</span><span class="w">
</span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nf">interactive</span><span class="p">())</span><span class="w"> </span><span class="n">shinyApp</span><span class="p">(</span><span class="n">ui</span><span class="p">,</span><span class="w"> </span><span class="n">server</span><span class="p">)</span><span class="w">
</span>

range slider

If you think there are other use cases for intrval in shiny applications, let me know in the comments section!

To leave a comment for the author, please follow the link and comment on their blog: Peter Solymos - R related posts.

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)