{"id":4357,"date":"2018-10-05T22:09:28","date_gmt":"2018-10-05T20:09:28","guid":{"rendered":"https:\/\/engel-wolf.com\/?p=4357"},"modified":"2019-10-27T09:57:44","modified_gmt":"2019-10-27T07:57:44","slug":"7-easy-steps-to-custom-inputs-in-shiny","status":"publish","type":"post","link":"https:\/\/engel-wolf.com\/?p=4357","title":{"rendered":"7 easy steps to custom inputs in shiny"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\" id=\"ab38\">A step by step guide on how to include custom inputs into R Shiny. This guide is going through an example of a custom input build from jQuery. After the tutorial you will be able to: Build your own front-end elements in shiny AND receive their values in&nbsp;R.<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"aae0\">Why a second guide on building custom shiny&nbsp;inputs?<\/h3>\n\n\n\n<p>For more than two years we are developing one of the largest&nbsp;<a href=\"https:\/\/shiny.rstudio.com\/\" rel=\"noreferrer noopener\" target=\"_blank\">Shiny<\/a>&nbsp;apps in the world. It\u2019s large in terms of interaction items and lines of code. I already explained this in one of my former&nbsp;<a href=\"https:\/\/medium.com\/@zappingseb\/how-to-build-a-shiny-truck-3ea077a4dc84\" target=\"_blank\" rel=\"noreferrer noopener\">blog entries on how to write such an app<\/a>. I used some techniques in<a href=\"https:\/\/www.amazon.de\/gp\/product\/1466586966\/ref=as_li_tl?ie=UTF8&amp;camp=1638&amp;creative=6742&amp;creativeASIN=1466586966&amp;linkCode=as2&amp;tag=zappingseb-21&amp;linkId=174feedba7721f76cdacaaaca66f34bf\" rel=\"noreferrer noopener\" target=\"_blank\">&nbsp;Advanced R<\/a>&nbsp;to build it.<\/p>\n\n\n\n<p>Our customers wanted a lot of different custom inputs inside our app. They see a website on the internet and want exactly the same inside our shiny app. \u201cCan we get this feature into the app?\u201d. Upon this question I start reading the<a href=\"https:\/\/shiny.rstudio.com\/articles\/building-inputs.html\" rel=\"noreferrer noopener\" target=\"_blank\">&nbsp;tutorial about custom inputs<\/a>&nbsp;from RStudio again and often again. Don\u2019t understand me wrong, it\u2019s a great tutorial. But people tell me, they are missing the key message. I am missing a clear definition of steps you have to follow, to get your input ready to work.<\/p>\n\n\n\n<p>For me it is sometimes really hard to reproduce what I did for one custom input in another custom input. This is the main reason why I am now creating this tutorial. There are key elements in implementing custom inputs that you shall think about. Some of them you will already be keen on, so you can ignore one or the other paragraph. But the one\u2019s you do not know about, please read them carefully. And whenever you are building a new input, make sure you checked the 7 steps I will explain below.<\/p>\n\n\n\n<p>The full outcome of this tutorial can be found in the<a href=\"https:\/\/github.com\/zappingseb\/customshinyinput\" rel=\"noreferrer noopener\" target=\"_blank\">&nbsp;github repository zappingseb\/customshinyinput.<\/a><\/p>\n\n\n\n<p>The final app runs at:&nbsp;<a href=\"https:\/\/sebastianwolf.shinyapps.io\/customshinyinput\/\" rel=\"noreferrer noopener\" target=\"_blank\">https:\/\/sebastianwolf.shinyapps.io\/customshinyinput\/<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"ac0a\">What is important for a custom shiny&nbsp;input?<\/h3>\n\n\n\n<p>These are the chapters I will go through to build an app with custom shiny inputs and the things you need to know to do it yourself:<\/p>\n\n\n\n<ol><li><a href=\"#c822\">Know how to find fancy and easy to implement inputs<\/a><\/li><li><a href=\"#ad5b\">Know some basic HTML and JS for the setup<\/a><\/li><li><a href=\"#2123\">Know how to start with the JavaScript InputBinding file<\/a><\/li><li><a href=\"#87c7\">Know what your&nbsp;<em>getValue<\/em>&nbsp;function looks like<\/a><\/li><li><a href=\"#b150\">Know what your&nbsp;<em>subscribe&nbsp;<\/em>function looks like<\/a><\/li><li><a href=\"#0fa4\">Know how to handle the input<\/a><\/li><li><a href=\"#8f4d\">Don\u2019t build reactive stuff about you input in R, do as much as possible in JavaScript<\/a><\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"be2c\">The example<\/h3>\n\n\n\n<p>My example is pretty artificial, but there is a reason for that. I want to make it hard to get the value of the input and use jQuery, because it has a lot of really cool features and is already included in shiny&nbsp;. So let\u2019s imagine you want to define two colors. One is your bar border color, the other is your bar fill color. Both shall be entered using a color picker. Upon being too close (e.g. red and dark red) there shall be a little note on this. I will do this using the&nbsp;<a href=\"https:\/\/shiny.rstudio.com\/gallery\/telephones-by-region.html\" rel=\"noreferrer noopener\" target=\"_blank\">Telephones by region App&nbsp;<\/a>from the Shiny Gallery because it includes a&nbsp;<code>barplot<\/code>. But let\u2019s start from scratch<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"c822\">1 Know how to find fancy and easy to implement inputs<\/h3>\n\n\n\n<p>The app shall get two inputs that allow you to choose colors. Such inputs are called color pickers. This tutorial will deal with jQuery inputs. JQuery is easy to learn and already included in shiny. So let\u2019s start searching for inputs by googling the following: \u201ccolorpicker jquery\u201d&nbsp;.These are my results:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*yfCRPHu5fmVxHd4OkBmQQQ.png\" alt=\"\"\/><figcaption>Results for googling jQuery colorpicker<\/figcaption><\/figure>\n\n\n\n<p>Let\u2019s double check the first two. The ColorPicker\u200a\u2014\u200ajQuery plugin looks like that from the code description (<a href=\"https:\/\/www.eyecon.ro\/colorpicker\" rel=\"noreferrer noopener\" target=\"_blank\">https:\/\/www.eyecon.ro\/colorpicker<\/a>):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$('#colorpickerHolder').ColorPicker({flat: true});<\/pre>\n\n\n\n<p>or on the page \u201cImplement\u201d<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$('input').ColorPicker(options);<\/pre>\n\n\n\n<p>The Spectrum color picker (<a href=\"https:\/\/bgrins.github.io\/spectrum\/\" rel=\"noreferrer noopener\" target=\"_blank\">https:\/\/bgrins.github.io\/spectrum\/<\/a>) documentation looks like that from the code description:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;input type='text' id=\"custom\" \/&gt;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;script&gt;<br>$(\"#custom\").spectrum({<br>    color: \"#f00\"<br>});<br>&lt;\/script&gt;<\/pre>\n\n\n\n<p>You can already see a difference in documentation. While spectrum gives you the HTML element to use + the JavaScript code, ColorPicker leaves you with some pure JavaScript code. This is something you should be aware of. If you use the first one, you might have to find out what the HTML has to look like on your own.<\/p>\n\n\n\n<p>Second of all you shall be aware that it might be hard to derive the value of the color picker. Later in this tutorial I will talk about the&nbsp;<em>getValue<\/em>&nbsp;function of R-Shiny custom elements. In this case it is actually kind of easy, as the inputs work as real&nbsp;<code>input<\/code>&nbsp;elements.&nbsp;<code>input<\/code>&nbsp;elements can be read in jQuery using<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$('input').val()<\/pre>\n\n\n\n<p>So please try to always use custom inputs that can be designed as html&nbsp;<code>&lt;input&gt;<\/code>&nbsp;elements. Additionally if the input like for spectrum is just a basic&nbsp;<code>&lt;input type=\"text\"&gt;<\/code>&nbsp;you can stick to shiny standard inputs in your code, meaning you can reuse something like the shiny function&nbsp;<code><a href=\"https:\/\/shiny.rstudio.com\/reference\/shiny\/0.14\/textInput.html\" rel=\"noreferrer noopener\" target=\"_blank\">textinput<\/a><\/code>as it also produces a&nbsp;<code>&lt;input type=\"text\"&gt;<\/code>&nbsp;element, but covers additional features like a label.<\/p>\n\n\n\n<p>So for reasons of simplicity and documentation I will use the spectrum.js color picker.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Takeaway #1: Search for jQuery based tools that provide HTML in their tutorials<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"ad5b\">2 Know some basic HTML and JS for the&nbsp;setup<\/h3>\n\n\n\n<p>This chapter is about the front-end setup of your custom input element. Therefore you need to know some basic HTML. First thing to know: what is a&nbsp;<a href=\"https:\/\/www.w3schools.com\/html\/html_classes.asp\" rel=\"noreferrer noopener\" target=\"_blank\">class&nbsp;<\/a>and what is an&nbsp;<a href=\"https:\/\/www.w3schools.com\/html\/html_id.asp\" rel=\"noreferrer noopener\" target=\"_blank\">I<\/a>D. Please read this at the desired links on&nbsp;<a href=\"https:\/\/www.w3schools.com\/html\/html_id.asp\" rel=\"noreferrer noopener\" target=\"_blank\">w3schools<\/a>&nbsp;if you do not know the difference.<\/p>\n\n\n\n<p>Your custom input shall have both. An ID to make it easy to use the spectrum.js, or any other JavaScript feature you will stumble upon. Most of such special input features use IDs. Additionally you shall use a class, to make it reproducible. You may want to have multiple of your custom input elements in your final shiny app. Therefore they shall all get one class to look the same or behave the same.<\/p>\n\n\n\n<p>The element we want to build shall be a&nbsp;<code>div<\/code>&nbsp;element with a specific ID and two color pickers inside. From the spectrum.js website we know one color picker looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;input type='text' id=\"custom\" \/&gt;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;script&gt;<br>$(\"#custom\").spectrum({<br>    color: \"#f00\"<br>});<br>&lt;\/script&gt;<\/pre>\n\n\n\n<p>So the things we have to replace are:<\/p>\n\n\n\n<ul><li>the id<\/li><li>the default color<\/li><\/ul>\n\n\n\n<p>Additionally I already mentioned a&nbsp;<code>shiny::textInput<\/code>&nbsp;also generates this code inside e.g.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">shiny::textInput(inputId = \"custom\")<\/pre>\n\n\n\n<p>produces<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;input type='text' id=\"custom\" \/&gt;<\/pre>\n\n\n\n<p>as one part of its code. Meaning we can build a shiny + spectrum.js input like this in R:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: r; title: ; notranslate\" title=\"\">\ndiv(\n  shiny::textInput(inputId = &quot;custom&quot;),\n  HMTL(&quot;&lt;script&gt;\n     $(&#039;#custom&#039;).spectrum({\n       color: &#039;#f00&#039;\n     });\n   &lt;\/script&gt;&quot;\n  )\n)\n<\/pre><\/div>\n\n\n<p><a href=\"https:\/\/gist.github.com\/zappingseb\/f76b938815f98eeed128c3610550e387\/raw\/4ba43cf71adf2050e47dd7f7635196eea49106f9\/custom_inputs_lessonone_textInput.R\">view raw<\/a><a href=\"https:\/\/gist.github.com\/zappingseb\/f76b938815f98eeed128c3610550e387#file-custom_inputs_lessonone_textinput-r\">custom_inputs_lessonone_textInput.R<\/a>&nbsp;hosted with \u2764 by&nbsp;<a href=\"https:\/\/github.com\">GitHub<\/a><\/p>\n\n\n\n<p>Of course you do not want to hard code the word \u201ccustom\u201d here. Using the&nbsp;<code><a href=\"https:\/\/github.com\/tidyverse\/glue\" rel=\"noreferrer noopener\" target=\"_blank\">glue<\/a><\/code>-package we can generate a pretty nice id parser. Additionally wrapping our two&nbsp;<code>textInputs<\/code>&nbsp;inside a&nbsp;<code>div<\/code>&nbsp;function and use&nbsp;<code>glue<\/code>&nbsp;to also parse the JavaScript code inside. Finally we get a function that will generate an input element including two text inputs where one is shiny made and one is custom made by HTML code (see below). Additionally the necessary JavaScript code for spectrum.js is parsed underneath the elements. We use the value&nbsp;<code>preferredFormat: 'hex'<\/code>&nbsp;as we like to get hex color values in R as a return.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: r; title: ; notranslate\" title=\"\">\n#&#039; Function to return a DoubleColorPicker input\n#&#039; @param id (\\code{character}) A web element ID, shall not contain dashes or underscores or colon\n#&#039; @param col_border (\\code{character}) A hex code for the border color default value\n#&#039; @param col_fill (\\code{character}) A hex code for the fill color default value\n#&#039; @return An \\code{shiny::div} element with two color pickers\n#&#039; @author Sebastian Wolf \\email{sebastian@@engel-wolf.com}\nDoubleColorPickerInput &lt;- function(id=&quot;fancycolorpicker&quot;, col_border = &quot;#f00&quot;, col_fill=&quot;#00f&quot;){\n  \n  # Return a div element of class &quot;doubleclorpicker&quot;\n  div(\n    id=id,\n    class=&quot;doublecolorpicker&quot;,\n    \n    # Include two shiny textInputs\n    textInput(inputId=glue(&quot;{id}-input-border&quot;),label=&quot;Border Color&quot;,value = col_border),\n    tags$label(&quot;Fill Color&quot;),\n    HTML(glue(&quot;&lt;input type=&#039;text&#039; id=&#039;{id}-input-fill&#039; value=&#039;{col_fill}&#039;\/&gt;&quot;)),\n    \n    # Include the Javascript code given by the spectrum.js website\n    HTML(\n      glue(\n        &quot;&lt;script&gt;\n          $(&#039;#{id}-input-border&#039;).spectrum({{\n            color: &#039;{col_border}&#039;,\n            preferredFormat: &#039;hex&#039;\n          }});\n          $(&#039;#{id}-input-fill&#039;).spectrum({{\n            color: &#039;{col_fill}&#039;,\n            preferredFormat: &#039;hex&#039;\n          }}); \n          &lt;\/script&gt;&quot;\n        \n      )\n    )# HTML\n  )#div\n  \n}\n<\/pre><\/div>\n\n\n<p><a href=\"https:\/\/gist.github.com\/zappingseb\/a35c25729ac13284fbe3e570dad42801\/raw\/6ecbbf06e9ca3aa4808ce6ccafa940a9d41e3095\/DoubleColorPickerInput.R\">view raw<\/a><a href=\"https:\/\/gist.github.com\/zappingseb\/a35c25729ac13284fbe3e570dad42801#file-doublecolorpickerinput-r\">DoubleColorPickerInput.R<\/a>&nbsp;hosted with \u2764 by&nbsp;<a href=\"https:\/\/github.com\">GitHub<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/600\/1*7XGjzFMW-6oR83caq22z_A.png\" alt=\"\"\/><figcaption>Placing the spectrum.js and spectrum.css files at the right&nbsp;place<\/figcaption><\/figure>\n\n\n\n<p>Now inside your shiny app there are just two files missing. You can find both in the spectrum.js tutorial. You need to source the JS and CSS files. This is necessary for the app to work. Therefore you download spectrum.js from their&nbsp;<a href=\"https:\/\/bgrins.github.io\/spectrum\/\" rel=\"noreferrer noopener\" target=\"_blank\">website<\/a>&nbsp;and place the \u201cspectrum.js\u201d and \u201cspectrum.css\u201d files in the \u201cwww\u201d directory of your app (here \u201ccustomshinyinput\u201d).<\/p>\n\n\n\n<p>Now you can source both files using the html sourcing procedures:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;script src='<a href=\"http:\/\/bgrins.github.com\/spectrum\/spectrum.js\" rel=\"noreferrer noopener\" target=\"_blank\">spectrum.js<\/a>'&gt;&lt;\/script&gt;<br>&lt;link rel='stylesheet' href='<a href=\"http:\/\/bgrins.github.com\/spectrum\/spectrum.css\" rel=\"noreferrer noopener\" target=\"_blank\">spectrum.css<\/a>' \/&gt;<\/pre>\n\n\n\n<p>In shiny it looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">tags$head(<br>    tags$link(rel = \"stylesheet\", type = \"text\/css\", <br>              href = \"spectrum.css\"),<br>    tags$script(src=\"spectrum.js\")<br>)<\/pre>\n\n\n\n<p>The&nbsp;<strong>full app with finished user interface<\/strong>&nbsp;you can then create would include just your custom input in the UI:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: r; title: ; notranslate\" title=\"\">\nlibrary(shiny)\nlibrary(glue)\n\nsource(&quot;DoubleColorPickerInput.R&quot;)\n\nui &lt;- fluidPage(\n  \n  tags$head(\n    tags$link(rel = &quot;stylesheet&quot;, type = &quot;text\/css&quot;, href = &quot;spectrum.css&quot;),\n    tags$script(src=&quot;spectrum.js&quot;)\n  ),\n  \n  # Generate a row with a sidebar\n  sidebarLayout(      \n    \n    # Define the sidebar with one custom input\n    sidebarPanel(\n      DoubleColorPickerInput()\n    ),\n    \n    mainPanel(\n    )\n    \n  )\n)\n\nserver &lt;- function(input, output) {\n\n}\nshinyApp(ui, server)\n<\/pre><\/div>\n\n\n<p><a href=\"https:\/\/gist.github.com\/zappingseb\/97c4f7f45fa34ec17523b3cbc38ce8ad\/raw\/6d0b9951224bdc80ff363001feea63d900d6c58c\/custom_inputs_lessonone.R\">view raw<\/a><a href=\"https:\/\/gist.github.com\/zappingseb\/97c4f7f45fa34ec17523b3cbc38ce8ad#file-custom_inputs_lessonone-r\">custom_inputs_lessonone.R<\/a>&nbsp;hosted with \u2764 by&nbsp;<a href=\"https:\/\/github.com\">GitHub<\/a><\/p>\n\n\n\n<p>and look like this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*dREwTFpDBaBCwp_2eeTX3A.png\" alt=\"\"\/><figcaption>Image of custom_inputs_lessonone.R Shiny&nbsp;app<\/figcaption><\/figure>\n\n\n\n<p>Now you can see that spectrum.js took over your&nbsp;<code>shiny::textInput<\/code>&nbsp;and made it a color picker input. This is not really a custom input. But you can already see it is a nice feature that you can have a simple text input that allows you to read colors.<\/p>\n\n\n\n<p>Note: This is not a custom shiny input, as we do not receive the input values, yet. In the next chapter we will make it a real custom input.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Takeaway #2: Try to use Shiny Input Elements inside your custom elements.<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Takeaway #3: Do not forget to source the JavaScript and CSS files of your custom&nbsp;element.<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Takeaway #4: Put a&nbsp;<code>div<\/code>&nbsp;container around your element and give it a special&nbsp;class.<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2123\">3 Know how to start with the JavaScript InputBinding file<\/h3>\n\n\n\n<p>The first step to make your input a real custom input is creating a JavaScript file as described in the<a href=\"https:\/\/shiny.rstudio.com\/articles\/building-inputs.html\" rel=\"noreferrer noopener\" target=\"_blank\">&nbsp;tutorial by RStudio<\/a>. It has to contain three main elements.<\/p>\n\n\n\n<ol><li>The&nbsp;<em>creation<\/em>&nbsp;of your custom InputBinding<\/li><li>The&nbsp;<em>extension<\/em>&nbsp;of your custom InputBinding<\/li><li>The&nbsp;<em>registration&nbsp;<\/em>of your custom InputBinding<\/li><\/ol>\n\n\n\n<p>#1 and #3 are easy to do. Just give it a name and use the two functions<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ #1<br>var DoubleColorPickerBinding = new Shiny.InputBinding();<br>\/\/ #3<br>Shiny.inputBindings.register(DoubleColorPickerBinding);<\/pre>\n\n\n\n<p>The hard part comes for Part #2. Part #2 shall contain 5 functions<\/p>\n\n\n\n<ol><li>find<\/li><li>getValue<\/li><li>setValue<\/li><li>subscribe<\/li><li>unsubscribe<\/li><\/ol>\n\n\n\n<p>Let\u2019s start with the&nbsp;<code>find<\/code>&nbsp;function. The&nbsp;<code>find<\/code>&nbsp;function allows you to call your custom input inside all other (2\u20135) functions by the&nbsp;<code>el<\/code>&nbsp;variable. This is pretty nice to work with, as you don\u2019t have to find the element again and again and again. In our case we defined that each custom input shall have the HTML class&nbsp;<code>doublecolorpicker<\/code>&nbsp;. To find the element, we just use:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">find: function(scope) {<br>    return $(scope).find(\".doublecolorpicker\");<br> }<\/pre>\n\n\n\n<p>where&nbsp;<code>scope<\/code>&nbsp;is the website you are working on and&nbsp;<code><a rel=\"noreferrer noopener\" href=\"http:\/\/api.jquery.com\/find\/\" target=\"_blank\">find<\/a><\/code><a rel=\"noreferrer noopener\" href=\"http:\/\/api.jquery.com\/find\/\" target=\"_blank\">&nbsp;is a function that comes with jQuery<\/a>&nbsp;and the&nbsp;<code>.<\/code>&nbsp;operator of CSS calls for classes. Do not mess up the two different&nbsp;<code>find<\/code>&nbsp;functions. One is defined inside your JavaScript file, the other one comes from jQuery. Now you\u2019ve got it. You found your custom input element again and can work inside it. The whole JavaScript file you need to add to your app now looks like this, in future we call it&nbsp;<code>DoubleColorPickerInput.js<\/code>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/\/ DoubleColorPicker Input Binding\n\/\/\n\/\/ Definition of a Shiny InputBinding extension to\n\/\/ get the values of two color pickers.\n\/\/\n\/\/ https:\/\/github.com\/zappingseb\/customshinyinput\n\/\/ Author: Sebastian Wolf\n\/\/ License: MIT\n\n\/\/ -----------------------------------------------------\n\n\/\/ Create a shiny input binding\n\/\/ Each custom input needs to be a shiny input binding\n\/\/ that is defined by the JavaScript class &quot;Shiny&quot; and\n\/\/ using the method &quot;InputBinding&quot;\n\/\/ The name can be chosen, here it is &quot;DoubleColorPickerBinding&quot;\nvar DoubleColorPickerBinding = new Shiny.InputBinding();\n\n\/\/ Extend the binding with the functions seen here\n$.extend(DoubleColorPickerBinding, {\n  \n  \/\/ The scope defines how the element is described in HTML code\n  \/\/ The best way to find the scope \n  find: function(scope) {\n    return $(scope).find(&quot;.doublecolorpicker&quot;);\n  },\n  getValue: function(el) {\n   \/\/ todo\n  },\n  setValue: function(el, value) {\n   \/\/ todo\n  },\n  subscribe: function(el, callback) {\n    \/\/ todo\n  },\n  unsubscribe: function(el) {\n    \/\/ todo\n  }\n});\n\n\/\/ Registering the shiny input\n\/\/\n\/\/ The following function call is used to tell shiny that\n\/\/ there now is a new Shiny.InputBinding that it shall\n\/\/ deal with and that it&#039;s functionality can be found in\n\/\/ the variable &quot;DoubleColorPickerBinding&quot;\nShiny.inputBindings.register(DoubleColorPickerBinding);\n<\/pre><\/div>\n\n\n<p><a href=\"https:\/\/gist.github.com\/zappingseb\/6f8f61963094f1e4c13437fd06d4ba31\/raw\/459d3ccad55273845c308561575e968d91e4a3ac\/custom_inputs_lessontwo.js\">view raw<\/a><a href=\"https:\/\/gist.github.com\/zappingseb\/6f8f61963094f1e4c13437fd06d4ba31#file-custom_inputs_lessontwo-js\">custom_inputs_lessontwo.js<\/a>&nbsp;hosted with \u2764 by&nbsp;<a href=\"https:\/\/github.com\">GitHub<\/a><\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><em>Takeaway #5:&nbsp;<\/em>Use the jQuery.find() function to get your element into the Shiny.InputBinding.<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"87c7\"><strong>4 Know what your&nbsp;<em>getValue<\/em>&nbsp;function looks&nbsp;like<\/strong><\/h3>\n\n\n\n<p>Here comes the hard part. We now want to access the value of our custom input and hand it over to R. Therefore we need to work on the&nbsp;<code>getValue<\/code>&nbsp;function inside the JavaScript file. This function basically returns the values of your custom input element into your shiny&nbsp;<code>input<\/code>&nbsp;variable.<\/p>\n\n\n\n<p>The example I constructed makes it a bit harder than what you would expect. In this case we do not want to include just the value of one input element, but two. Therefore the best way to handle two values between JavaScript and R is the&nbsp;<a href=\"http:\/\/www.json.org\/\" rel=\"noreferrer noopener\" target=\"_blank\">json-Format<\/a>. It allows you to send dictionaries as well as arrays. Here we would like to get an array with dictionaries that tells us something like this: border_color=red, fill_color=blue. In JSON this would look like this<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[<br>  {name:'border',<br>   color:'red'},<br>  {name:'fill',<br>   color:'blue'}<br>]<\/pre>\n\n\n\n<p>It means you get an array of two objects that both have the attributes&nbsp;<em>name&nbsp;<\/em>and the attributes&nbsp;<em>color<\/em>. We can do this now inside our&nbsp;<code>getValue<\/code>&nbsp;function by using some basic jQuery Tools.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\ngetValue: function(el) {\n  \/\/ create an empty output array\n  var output = &#91;]\n\n  \/\/ go over each input element inside the\n  \/\/ defined InputBinding and add the ID\n  \/\/ and the value as a dictionary to the output\n  $(el).find(&quot;input&quot;).each(function(inputitem){\n\n    output.push({\n        name:   $(this).attr(&quot;id&quot;),\n        value:  $(this).val()\n    });\n  });\n\n  \/\/ return the output as JSON \n  return(JSON.stringify(output))\n\n}\n<\/pre><\/div>\n\n\n<p><a href=\"https:\/\/gist.github.com\/zappingseb\/862fa1daef483a89c9f4818679250449\/raw\/7e65dab7be7e234243b45502212a0bf97fdc9c75\/custom_inputs_lessonthree.js\">view raw<\/a><a href=\"https:\/\/gist.github.com\/zappingseb\/862fa1daef483a89c9f4818679250449#file-custom_inputs_lessonthree-js\">custom_inputs_lessonthree.js<\/a>&nbsp;hosted with \u2764 by&nbsp;<a href=\"https:\/\/github.com\">GitHub<\/a><\/p>\n\n\n\n<p>The functions you need to know are&nbsp;<code>each<\/code>,&nbsp;<code>attr<\/code>and&nbsp;<code>val<\/code>&nbsp;. These three functions allow you to iterate over your inputs (<code>each<\/code>), to derive their ID (<code>attr<\/code>) and to derive their value (<code>val<\/code>).<\/p>\n\n\n\n<p>In the R Code of&nbsp;<code><a href=\"https:\/\/gist.github.com\/zappingseb\/a35c25729ac13284fbe3e570dad42801\" rel=\"noreferrer noopener\" target=\"_blank\">DoubleColorPickerInput.<\/a>R<\/code>we encoded the IDs an a way that they represent border color or for the fill color. The IDs make a good name by this encoding.<\/p>\n\n\n\n<p>In case you want a more difficult input you might have to look up how to get the value of an element. It can be different than basically using the&nbsp;<code>val<\/code>&nbsp;function or you need a different&nbsp;<code>find<\/code>&nbsp;call. For example using a&nbsp;<em>textarea&nbsp;<\/em>input you cannot find it using the&nbsp;<code>$(el).find('input')<\/code>&nbsp;statement. You will have to use&nbsp;<code>$(el).find('textarea')<\/code>&nbsp;and also append the value of this element to your output array.<\/p>\n\n\n\n<p>To make the output array readable in R you have to return it as a JSON string. Inside JavaScript the&nbsp;<code>JSON.stringify<\/code>&nbsp;function will do it for you.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Takeaway #6: Derive input element Values by jQuery.val()<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Takeaway #7: Use JSON arrays to derive multiple input&nbsp;values<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"b150\">5 Know what your subscribe function looks&nbsp;like<\/h3>\n\n\n\n<p>Now we have a custom input using two color pickers and we are able to derive the value of this input. Next we need shiny to also react upon changes of this input. This is what is called&nbsp;<em>subscribe.<\/em><\/p>\n\n\n\n<p>There are two ways how you can subscribe to an input. The first one will use no rate policy, meaning you send everything that changes immediately to R. The second one can use a rate policy, meaning you wait a certain time for inputs to change and then send the values. The second one is really important for text input fields. In case somebody types a word, you do not want shiny to react upon each letter the person types, but on the whole word. Therefore you will need a rate policy. The rate policy is explained in detail inside the<a href=\"https:\/\/shiny.rstudio.com\/articles\/building-inputs.html#write-an-input-binding\" rel=\"noreferrer noopener\" target=\"_blank\">&nbsp;RStudio tutorial<\/a>.<\/p>\n\n\n\n<p>Here we would like our app to change on any color change we have in our DoubleColorPicker. As each element inside our DoubleColorPicker is an input, we can basically check for changes of those. Therefore jQuery contains a function that is called&nbsp;<code>change<\/code>&nbsp;that notices any change of the value of an element or changes of classes, too. Other candidates in jQuery for such change recognition of changes are:&nbsp;<code>keyup<\/code>&nbsp;,&nbsp;<code>keydown<\/code>&nbsp;,&nbsp;<code>keypress<\/code>&nbsp;.<\/p>\n\n\n\n<p>Our subscription to the color pickers will look like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nsubscribe: function(el, callback) {\n    \/\/ the jQuery &quot;change&quot; function allows you\n    \/\/ to notice any change to your input elements\n   $(el).on(&#039;change.input&#039;, function(event) {\n     callback(false);\n       \/\/ When called with false, it will NOT use the rate policy,\n\t  \/\/ so changes will be sent immediately\n   }); \n  }\n<\/pre><\/div>\n\n\n<p><a href=\"https:\/\/gist.github.com\/zappingseb\/93009ba6800b13c288a090fcac4dd541\/raw\/10423517f05b58457507fc0f3099407806fbed4e\/custom_inputs_lessonfour_part.js\">view raw<\/a><a href=\"https:\/\/gist.github.com\/zappingseb\/93009ba6800b13c288a090fcac4dd541#file-custom_inputs_lessonfour_part-js\">custom_inputs_lessonfour_part.js<\/a>&nbsp;hosted with \u2764 by&nbsp;<a href=\"https:\/\/github.com\">GitHub<\/a><\/p>\n\n\n\n<p><code>change.input<\/code>&nbsp;seems like an easy thing to check on. But you might have more difficult inputs inside your shiny custom input element. Therefore I would recommend to give each of your custom inputs an additional class OR find out what is the class of your shiny-input. I built a shiny input out of shiny&nbsp;<code>selectInput<\/code>&nbsp;functions. They all have the class&nbsp;<code>selectized<\/code>&nbsp;. So you can make a subscribe function like that:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$(el).on('change.selectized', function(event) {<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">     callback(false);<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">});<\/pre>\n\n\n\n<p>Instead of&nbsp;<code>selectized<\/code>&nbsp;you can insert your own class that you assign to each of your single elements. Assigning an own class like&nbsp;<code>doublecolorpickerItem<\/code>&nbsp;will also allow you to derive the values easier because you do not care for the type of your input element. The code of your app would look really different then, but you could do it and I recommend it. In case of this app here, it is not possible as&nbsp;<code>shiny::textInput<\/code>&nbsp;does not allow to set a custom class.<\/p>\n\n\n\n<p>Though I prepared an example of such a complicated input for anyone who is interested as a<a href=\"https:\/\/gist.github.com\/zappingseb\/40cb90836b870969c72921f5d2745ee8\" rel=\"noreferrer noopener\" target=\"_blank\">&nbsp;Gist (<strong>Example of a difficult multi input custom input)<\/strong>.<\/a><\/p>\n\n\n\n<p>The whole JavaScript file now looks like that:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: r; title: ; notranslate\" title=\"\">\n\/\/ DoubleColorPicker Input Binding\n\/\/\n\/\/ Definition of a Shiny InputBinding extension to\n\/\/ get the values of two color pickers.\n\/\/\n\/\/ https:\/\/github.com\/zappingseb\/customshinyinput\n\/\/ Author: Sebastian Wolf\n\/\/ License: MIT\n\n\/\/ -----------------------------------------------------\n\n\/\/ Create a shiny input binding\n\/\/ Each custom input needs to be a shiny input binding\n\/\/ that is defined by the JavaScript class &quot;Shiny&quot; and\n\/\/ using the method &quot;InputBinding&quot;\n\/\/ The name can be chosen, here it is &quot;DoubleColorPickerBinding&quot;\nvar DoubleColorPickerBinding = new Shiny.InputBinding();\n\n\/\/ Extend the binding with the functions seen here\n$.extend(DoubleColorPickerBinding, {\n  \n  \/\/ The scope defines how the element is described in HTML code\n  \/\/ The best way to find the scope \n  find: function(scope) {\n    return $(scope).find(&quot;.doublecolorpicker&quot;);\n  },\n  getValue: function(el) {\n    \/\/ create an empty output array\n    var output = &#91;]\n  \n    \/\/ go over each input element inside the\n    \/\/ defined InputBinding and add the ID\n    \/\/ and the value as a dictionary to the output\n    $(el).find(&quot;input&quot;).each(function(inputitem){\n  \n      output.push({\n          name:   $(this).attr(&quot;id&quot;),\n          value:  $(this).val()\n      });\n    });\n  \n    \/\/ return the output as JSON \n    return(JSON.stringify(output))\n\n  },\n  setValue: function(el, value) {\n   \/\/ todo\n  },\n  subscribe: function(el, callback) {\n    \/\/ the jQuery &quot;change&quot; function allows you\n    \/\/ to notice any change to your input elements\n  \t$(el).on(&#039;change.input&#039;, function(event) {\n  \t  callback(false);\n  \t});\t\n  },\n  unsubscribe: function(el) {\n    $(el).off(&#039;.doublecolorpicker&#039;);\n  }\n});\n\n\/\/ Registering the shiny input\n\/\/\n\/\/ The following function call is used to tell shiny that\n\/\/ there now is a new Shiny.InputBinding that it shall\n\/\/ deal with and that it&#039;s functionality can be found in\n\/\/ the variable &quot;DoubleColorPickerBinding&quot;\nShiny.inputBindings.register(DoubleColorPickerBinding);\n<\/pre><\/div>\n\n\n<p><a href=\"https:\/\/gist.github.com\/zappingseb\/c51fb0dfe845065804c53b1edf4d9d98\/raw\/3526c9250d8c8d2f5af8d58f2c06871bfe62b383\/custom_inputs_lessonfour.js\">view raw<\/a><a href=\"https:\/\/gist.github.com\/zappingseb\/c51fb0dfe845065804c53b1edf4d9d98#file-custom_inputs_lessonfour-js\">custom_inputs_lessonfour.js<\/a>&nbsp;hosted with \u2764 by&nbsp;<a href=\"https:\/\/github.com\">GitHub<\/a><\/p>\n\n\n\n<p>it also contains an unsubscribe function, which you can always do in the same way using the&nbsp;<code>off<\/code>&nbsp;function.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Takeaway #8: Use the jQuery.change() function to derive changes of&nbsp;inputs.<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Takeaway #9: Try to give each element inside your custom input a desired HTML\/CSS&nbsp;class.<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"0fa4\">6 Know how to handle the&nbsp;input<\/h3>\n\n\n\n<p>Now we have a custom input that allows you to read two color pickers into one JSON string. This JSON string is available inside R by a shiny input. Additionally changes on one of the two colors will do a&nbsp;<em>callback<\/em>&nbsp;to shiny and inform it, that it has to react. We are finished with our custom input? No, we want it to come out of the JavaScript interaction not as a JSON string, but as a nice and readable list like:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[\"fill\"]]<br>\"#f00000\"<br>[[\"border\"]]<br>\"#00000f\"<\/pre>\n\n\n\n<p>Therefore we need to add a&nbsp;<code><a href=\"https:\/\/shiny.rstudio.com\/reference\/shiny\/1.1.0\/registerInputHandler.html\" rel=\"noreferrer noopener\" target=\"_blank\">registerInputHandler<\/a><\/code>function call to our shiny application. This will allow you to not only have a custom input, but also a custom value derived from your custom input. It needs two things in the setup.<\/p>\n\n\n\n<ol><li>change the JavaScript file to allow it<\/li><li>add the function call before your server.R is source<\/li><\/ol>\n\n\n\n<p>1 The first step is easy. You basically have to change the JavaScript functions to contain the following:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\ngetType: function(el) {\n    return &quot;DoubleColorPickerBinding&quot;;\n }\n<\/pre><\/div>\n\n\n<p>Now shiny will know which type you defined for your custom input. To see the whole input binding now, look at this&nbsp;<a href=\"https:\/\/gist.github.com\/zappingseb\/60fff0372f8ae69534491a7bb718722e\" rel=\"noreferrer noopener\" target=\"_blank\">Github Gist (adding getType for InputBinding in shiny)<\/a>.<\/p>\n\n\n\n<p>2 The second part is just coding a function to get a list out of a JSON string in R. This can be done by&nbsp;<code>jsonlite<\/code>&nbsp;easily.&nbsp;<code>jsonlite<\/code>&nbsp;will return a data.frame that you can parse into a list. All of this has to happen inside the&nbsp;<code><a href=\"https:\/\/shiny.rstudio.com\/reference\/shiny\/1.1.0\/registerInputHandler.html\" rel=\"noreferrer noopener\" target=\"_blank\">registerInputHandler<\/a><\/code>function call. You can add this code snippet to the file where you defined the custom input and it will be sourced upon starting the app.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: r; title: ; notranslate\" title=\"\">\n# Try to remove the input Handler as shiny does not allow double input\n# handlers\ntry({ removeInputHandler(&quot;DoubleColorPickerBinding&quot;) })\n\n# Use the shiny registerInputHandler function to\n# register a way to deal with the inputs coming\nshiny::registerInputHandler(\n  &quot;DoubleColorPickerBinding&quot;, \n  function(x, shinysession, name) {\n    \n    if(is.null(x)) {\n      return(x)\n      \n    } else {\n      # Parse return value from JSON into R format dataframe\n      colors_df &lt;- jsonlite::fromJSON(x)\n      \n      # Extract the values of the data frame as a list\n      res &lt;- list()  \n    \n      res&#91;&#91;&quot;fill&quot;]] &lt;- colors_df$value&#91;\n        which(grepl(colors_df$name,pattern=&quot;fill&quot;))\n        ]\n      res&#91;&#91;&quot;border&quot;]] &lt;- colors_df$value&#91;\n        which(grepl(colors_df$name,pattern=&quot;border&quot;))\n        ]\n      \n      return(res)\n    }\n  }\n)\n<\/pre><\/div>\n\n\n<p><a href=\"https:\/\/gist.github.com\/zappingseb\/83f0972a4a0c964e9bb5d76d0d42ae2b\/raw\/73b71c96fdab9b9c7e18d3bf201eecd80bbe49b8\/custom_inputs_lessonfive.R\">view raw<\/a><a href=\"https:\/\/gist.github.com\/zappingseb\/83f0972a4a0c964e9bb5d76d0d42ae2b#file-custom_inputs_lessonfive-r\">custom_inputs_lessonfive.R<\/a>&nbsp;hosted with \u2764 by&nbsp;<a href=\"https:\/\/github.com\">GitHub<\/a><\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Takeaway #10: Use registerInputHandler function to parse the response from your custom input over into something useful in&nbsp;R.<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8f4d\">7 Don\u2019t build reactive stuff about you input in R, do as much as possible in JavaScript<\/h3>\n\n\n\n<p>Actually, you are done with the application. Get the shiny&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/shiny.rstudio.com\/gallery\/telephones-by-region.html\" target=\"_blank\">Telephones by Region example<\/a>&nbsp;and make sure your&nbsp;<em>ui.R<\/em>&nbsp;sources&nbsp;<em>DoubleColorPicker.R&nbsp;<\/em>and contains&nbsp;<code>DoubleColorPickerInput(\"myid\")<\/code>&nbsp;inside your&nbsp;<code>sidePanel<\/code>&nbsp;. Additionally your&nbsp;<em>ui.R&nbsp;<\/em>should have<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: r; title: ; notranslate\" title=\"\">\ntags$head(\n    tags$link(rel = &quot;stylesheet&quot;, type = &quot;text\/css&quot;, href = &quot;spectrum.css&quot;),\n    tags$script(src=&quot;spectrum.js&quot;),\n    tags$script(src=&quot;DoubleColorPickerInput.js&quot;)\n)\n<\/pre><\/div>\n\n\n<p>in it to source the Javascript files. You can access the colors in your&nbsp;<em>server.R&nbsp;<\/em>by:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: r; title: ; notranslate\" title=\"\">\ncolor_list &lt;- input$myid\nbarplot(WorldPhones&#91;,input$region]*1000, \n            main=input$region,\n            ylab=&quot;Number of Telephones&quot;,\n            xlab=&quot;Year&quot;,\n            col = color_list&#91;&#91;&quot;fill&quot;]],,\n            border = color_list&#91;&#91;&quot;border&quot;]],\n            lwd=3\n )\n<\/pre><\/div>\n\n\n<p>and receive this app:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*Mqnc2uvNZcwJMq5PFI8e6Q.png\" alt=\"\"\/><figcaption>Final solution application<\/figcaption><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>for the full example please see&nbsp;<a href=\"https:\/\/github.com\/zappingseb\/customshinyinput\" rel=\"noreferrer noopener\" target=\"_blank\">https:\/\/github.com\/zappingseb\/customshinyinput<\/a><\/p><\/blockquote>\n\n\n\n<p>At the beginning of this tutorial I told you about the one feature I want to have, that is easy to do in shiny, but you should not do it. It\u2019s double checking if the two inserted colors are too close as e.g. red and dark red. This feature comes along with calculating something from your input, that does&nbsp;<strong>not&nbsp;<\/strong>change your R calls or R calculations, but your user interface. For example you want to give a warning upon the two colors being too close to each other. Of course you can do it in R, but this means the following:<\/p>\n\n\n\n<p>Pick the color \u2192 check the value with the inputBinding \u2192 send it to shiny \u2192 use the inputHandler \u2192 run R code on it \u2192 change a shiny warning output<\/p>\n\n\n\n<p>You can get this much faster using pure JavaScript<\/p>\n\n\n\n<p>Pick the color \u2192 check the value \u2192 change a warning item<\/p>\n\n\n\n<p>Of course the 5 step process via shiny runs fast and you wont\u2019t notice any difficulties if you have one custom input, but upon having maybe 20 or 30 of those inputs, we noticed in our apps, that they get kind of slow. This could be due to our bad R code or our bad JavaScript code. But we have 5 places where we can do something wrong. The second way just has 3 places to do something wrong. Therefore whenever you want to do something simple with your custom input that does not influence R calculations,&nbsp;<strong>do it in JavaScript.<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>My solution for dealing with colors comparison came from a&nbsp;<a href=\"https:\/\/stackoverflow.com\/questions\/22692134\/detect-similar-colours-from-hex-values\" rel=\"noreferrer noopener\" target=\"_blank\">stackoverflow entry<\/a>.<\/p><\/blockquote>\n\n\n\n<p>As the response we need a warning HTML item (class=\u2019warning\u2019) and a function call to check the item by its ID. Both can be setup in shiny. The warning shall not be shown at the beginning (display: none) and have red text (color:red). The function call shall check for changes of the item using the&nbsp;<code>jQuery.change<\/code>&nbsp;function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: r; title: ; notranslate\" title=\"\">\nHTML(glue(&quot;\n  &lt;script&gt;\n    $(&#039;#{id}&#039;).on(&#039;change&#039;, \n       function(){{\n         check_doublecolorpicker(&#039;{id}&#039;);\n       }});\n   &lt;\/script&gt;&quot;\n)),\nHTML(&quot;\n   &lt;div class=&#039;warning&#039;\n     style=&#039;color:red;display:none&#039;&gt;\n     Please choose more different colors\n   &lt;\/div&gt;&quot;\n)\n<\/pre><\/div>\n\n\n<p><a href=\"https:\/\/gist.github.com\/zappingseb\/0ebb9a1279487a7990e651ca3e8fff4e\/raw\/a349f05c58286744d5b2c1d218d0396bd94a372d\/custom_inputs_lessonsix.R\">view raw<\/a><a href=\"https:\/\/gist.github.com\/zappingseb\/0ebb9a1279487a7990e651ca3e8fff4e#file-custom_inputs_lessonsix-r\">custom_inputs_lessonsix.R<\/a>&nbsp;hosted with \u2764 by&nbsp;<a href=\"https:\/\/github.com\">GitHub<\/a><\/p>\n\n\n\n<p>The function&nbsp;<code>check_doublecolorpicker<\/code>&nbsp;needs to be set up in JavaScript. You basically need a function that gets the two color values, calculates the closeness and changes a warning HTML item. For calculating the closeness&nbsp;<code>hexColorDelta<\/code>&nbsp;was found in the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/stackoverflow.com\/questions\/22692134\/detect-similar-colours-from-hex-values\" target=\"_blank\">stackoverflow entry<\/a>&nbsp;and the rest is using the&nbsp;<code>jQuery.val<\/code>&nbsp;function as we did before. The&nbsp;<code>jQuery.hide<\/code>&nbsp;and&nbsp;<code>jQuery.show<\/code>&nbsp;function allow you to change the&nbsp;<code>display<\/code>&nbsp;property of your&nbsp;<em>warning&nbsp;<\/em>item. You have to use&nbsp;<code>.warning<\/code>&nbsp;as it\u2019s class is warning and the&nbsp;<code>.<\/code>&nbsp;operator of CSS allows you to access class elements.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/\/ Function to check a doublecolorpicker\n\/\/ item by id\nfunction check_doublecolorpicker(id){\n  \n  \/\/ derive an empty array of two inputs with\n  \/\/ colors\n  values = &#91;];\n  \n  \/\/ push the two colors into the array\n  $(&quot;#&quot;+id).find(&#039;input&#039;).each(function(item){\n  \n    values.push($(this).val());\n    \n  });\n  \n  \/\/ derive the closeness of the two colors\n  \/\/ delete the # sign by substring(1)\n  closeness = hexColorDelta(values&#91;0].substring(1),values&#91;1].substring(1));\n\n  \/\/ If the colros are too close, show a warning inside the \n  \/\/ id element\n  if(closeness&gt;0.8){\n    $(&#039;#&#039;+id + &#039; div.warning&#039;).show();\n  }else{\n    $(&#039;#&#039;+id + &#039; div.warning&#039;).hide();\n  }\n}\n<\/pre><\/div>\n\n\n<p><a href=\"https:\/\/gist.github.com\/zappingseb\/2b3b51b01b204854434b10db59a39855\/raw\/c93c90c5aea4782f3f9c3f4dcad7b0989ce21de8\/custom_inputs_lessonsix.js\">view raw<\/a><a href=\"https:\/\/gist.github.com\/zappingseb\/2b3b51b01b204854434b10db59a39855#file-custom_inputs_lessonsix-js\">custom_inputs_lessonsix.js<\/a>&nbsp;hosted with \u2764 by&nbsp;<a href=\"https:\/\/github.com\">GitHub<\/a><\/p>\n\n\n\n<p>I know if your programming a lot in R and especially in R-Shiny, this does not suite you. The code in JavaScript looks different from code in R. But as I explained before, it may save you time and troubles. The outcome message will now show up in your app as this and we are done.&nbsp;\ud83d\ude42<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*exA5SnSCn65-I58F4meZgA.png\" alt=\"\"\/><figcaption>Warning message upon inserting two equal&nbsp;colors<\/figcaption><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Takeaway #11: JavaScript can save you calculation steps in your user interface<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3970\">Summary<\/h3>\n\n\n\n<p>In summary I have to say that my blog post shall be named \u201c7 steps to get custom inputs in shiny as easy as possible\u201d. It is not easy to insert a custom input element into R-Shiny. It seems nearly impossible if you do not understand any JavaScipt. This tutorial should allow you to go through building up a new R-Shiny input by 7 steps. If you follow the 7 steps and do not forget to check one, you will succeed in building up new user interface elements. Those new elements will allow your users to insert something into your app they never thought about. We enabled things like a drag&amp;drop input, an input to rearrange a bunch of plots on a grid, fancier check boxes, on-off switches, anything you can imagine. You can read whole&nbsp;<a href=\"https:\/\/www.amazon.de\/gp\/product\/B00TJ6UWYK\/ref=as_li_tl?ie=UTF8&amp;camp=1638&amp;creative=6742&amp;creativeASIN=B00TJ6UWYK&amp;linkCode=as2&amp;tag=zappingseb-21&amp;linkId=5c1af6a57251d518abcbaf4f65bcfb9e\" rel=\"noreferrer noopener\" target=\"_blank\">books on fancy inputs&nbsp;<\/a>or check websites like&nbsp;<a href=\"https:\/\/bootsnipp.com\/\" rel=\"noreferrer noopener\" target=\"_blank\">https:\/\/bootsnipp.com\/<\/a>&nbsp;or&nbsp;<a href=\"https:\/\/freefrontend.com\" rel=\"noreferrer noopener\" target=\"_blank\">https:\/\/freefrontend.com<\/a>.<\/p>\n\n\n\n<p>The main reason for such custom inputs shall be your user requirements. Of course you can put them into your app just to learn some JavaScript, but I don\u2019t know if your boss will like it. But whenever your customer comes to you and shows you a website and says: \u201cCan we get this feature into the app?\u201d, now you should be able to answer:&nbsp;<strong>\u201cYes we can!\u201d<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"b2e2\">Takeaways<\/h3>\n\n\n\n<ol><li>Search for jQuery based tools that provide HTML tutorials<\/li><li>Try to use Shiny Input Elements inside your custom elements.<\/li><li>Do not forget to source the JavaScript and CSS files of your custom element.<\/li><li>Put a div container around your element and give it a special class.<\/li><li>Use the jQuery.find() function to get your element into the Shiny.InputBinding.<\/li><li>Derive input element Values by jQuery.val()<\/li><li>Use JSON arrays to derive multiple input values<\/li><li>Use the jQuery.change() function to derive changes of inputs.<\/li><li>Try to give each element inside your custom input a desired HTML class.<\/li><li>Use registerInputHandler function to parse the response from your custom input over into something useful in R.<\/li><li>JavaScript can save you calculation steps in your user interface<\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>A step by step guide on how to include custom inputs into R Shiny. This guide is going through an example of a custom input build from jQuery. After the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4358,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[390,392,393,391,384,385,381,389],"_links":{"self":[{"href":"https:\/\/engel-wolf.com\/index.php?rest_route=\/wp\/v2\/posts\/4357"}],"collection":[{"href":"https:\/\/engel-wolf.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/engel-wolf.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/engel-wolf.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/engel-wolf.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4357"}],"version-history":[{"count":3,"href":"https:\/\/engel-wolf.com\/index.php?rest_route=\/wp\/v2\/posts\/4357\/revisions"}],"predecessor-version":[{"id":4380,"href":"https:\/\/engel-wolf.com\/index.php?rest_route=\/wp\/v2\/posts\/4357\/revisions\/4380"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/engel-wolf.com\/index.php?rest_route=\/wp\/v2\/media\/4358"}],"wp:attachment":[{"href":"https:\/\/engel-wolf.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/engel-wolf.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/engel-wolf.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}