<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Shiny Application on rdata.lu Blog | Data science with R</title>
    <link>/categories/shiny-application/</link>
    <description>Recent content in Shiny Application on rdata.lu Blog | Data science with R</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <copyright>Copyright (c) rdata.lu. All rights reserved. &lt;br&gt; Content reblogged by &lt;a href=&#39;https://www.r-bloggers.com/&#39; target=&#39;_blank&#39;&gt;R-bloggers&lt;/a&gt; &amp; &lt;a href=&#39;http://www.rweekly.org/&#39; target=&#39;_blank&#39;&gt;RWeekly&lt;/a&gt;</copyright>
    <lastBuildDate>Tue, 20 Feb 2018 00:00:00 +0000</lastBuildDate>
    
        <atom:link href="/categories/shiny-application/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>BIKE SERVICES API &#43; SHINY = NICE APP</title>
      <link>/post/2018-02-20-api-shiny-nice-app/</link>
      <pubDate>Tue, 20 Feb 2018 00:00:00 +0000</pubDate>
      
      <guid>/post/2018-02-20-api-shiny-nice-app/</guid>
      <description>&lt;!--
words 941
&lt;style type=&#34;text/css&#34;&gt;
pre code, pre, code {
white-space: pre !important;
overflow-x: scroll !important;
overflow-y: scroll !important;
word-break: keep-all !important;
word-wrap: initial !important;
max-height:30vh !important;
}
p img{
width:100%; !important;
}
&lt;/style&gt;
--&gt;
&lt;p&gt;Hi everyone,&lt;/p&gt;
&lt;p&gt;In this blog post, I will be short and I will introduce our &lt;a href=&#34;http://blog.rdata.lu/visualization/bike/&#34;&gt;shiny application&lt;/a&gt; on bike self-service stations. &lt;/br&gt;&lt;/p&gt;
&lt;div class=video&gt;
&lt;video width=&#34;600&#34; height=&#34;400&#34; controls autoplay&gt;
&lt;source src=&#34;/video/demo_bike.mp4&#34; type=&#34;video/mp4&#34;&gt;
Your browser does not support the video tag. &lt;/video&gt;
&lt;/div&gt;
&lt;p&gt;The code is in 2 parts, the &lt;a href=&#34;https://github.com/krosamont/shiny_bike/blob/master/ui.R&#34;&gt;ui.R&lt;/a&gt; file for the interface and the &lt;a href=&#34;https://github.com/krosamont/shiny_bike/blob/master/server.R&#34;&gt;server.R&lt;/a&gt; file for the backend. You can check the code on &lt;a href=&#34;https://github.com/krosamont/shiny_bike/&#34;&gt;GitHub&lt;/a&gt; if you want to download the code and run it on your computer. &lt;/br&gt;&lt;/br&gt;&lt;/br&gt; JCDecaux provides an API that gives us real time information on each bike self-service station. This infomation is:&lt;/br&gt; •Station id &lt;/br&gt; •Station name &lt;/br&gt; •Address &lt;/br&gt; •Position latitude/longitude &lt;/br&gt; •Presence of a payment terminal &lt;/br&gt; •Presence of a bonus station &lt;/br&gt; •If the station is open &lt;/br&gt; •How many bike stands are in the station &lt;/br&gt; •how many bike stands are available in the station (no bikes on the stand) &lt;/br&gt; •How many bikes are available &lt;/br&gt; •Time of the last api update &lt;/br&gt;&lt;/p&gt;
&lt;p&gt;The JCDecaux API gives the data under the following format:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;{
  &amp;quot;number&amp;quot;: 123,
  &amp;quot;contract_name&amp;quot; : &amp;quot;Paris&amp;quot;,
  &amp;quot;name&amp;quot;: &amp;quot;stations name&amp;quot;,
  &amp;quot;address&amp;quot;: &amp;quot;address of the station&amp;quot;,
  &amp;quot;position&amp;quot;: {
    &amp;quot;lat&amp;quot;: 48.862993,
    &amp;quot;lng&amp;quot;: 2.344294
  },
  &amp;quot;banking&amp;quot;: true,
  &amp;quot;bonus&amp;quot;: false,
  &amp;quot;status&amp;quot;: &amp;quot;OPEN&amp;quot;,
  &amp;quot;bike_stands&amp;quot;: 20,
  &amp;quot;available_bike_stands&amp;quot;: 15,
  &amp;quot;available_bikes&amp;quot;: 5,
  &amp;quot;last_update&amp;quot;: &amp;lt;timestamp&amp;gt;
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Hence, our shiny application gets real time information on bike stations in 27 cities.&lt;/p&gt;
&lt;p&gt;We also used the modern open-source library leaflet to display city map (open street map). &lt;/br&gt;&lt;/br&gt;&lt;/p&gt;
&lt;p&gt;This application works better on computer than on smartphone because shiny is not fully smartphone friendly. However, Shiny has a user-friendly interface.&lt;/p&gt;
&lt;p&gt;If you want to try your own Shiny app, I advice you to check this &lt;a href=&#34;https://shiny.rstudio.com/gallery/&#34;&gt;gallery&lt;/a&gt;. It contains a lot of examples that will serve as a good introduction.&lt;/p&gt;
&lt;p&gt;
Don’t hesitate to follow us on twitter &lt;a href=&#34;https://twitter.com/rdata_lu&#34; target=&#34;_blank&#34;&gt;&lt;span class=&#34;citation&#34;&gt;@rdata_lu&lt;/span&gt;&lt;/a&gt; &lt;!-- or &lt;a href=&#34;https://twitter.com/brodriguesco&#34;&gt;@brodriguesco&lt;/a&gt; --&gt; and to &lt;a href=&#34;https://www.youtube.com/channel/UCbazvBnJd7CJ4WnTL6BI6qw?sub_confirmation=1&#34; target=&#34;_blank&#34;&gt;subscribe&lt;/a&gt; to our youtube channel. &lt;br&gt; You can also contact us if you have any comments or suggestions. See you for the next post!
&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Launching your shiny app in 2 clicks</title>
      <link>/post/2017-12-26-launching-your-shiny-app-in-2-clicks/</link>
      <pubDate>Tue, 26 Dec 2017 00:00:00 +0000</pubDate>
      
      <guid>/post/2017-12-26-launching-your-shiny-app-in-2-clicks/</guid>
      <description>&lt;!--```{r, echo=FALSE}
knitr::include_graphics(&#34;/images/what-if.jpg&#34;)
```--&gt;
&lt;p style=&#34;text-align:center&#34;&gt;
&lt;img src=&#34;/images/what-if.jpg&#34; style=&#34;width:60vh; &#34;&gt;
&lt;/p&gt;
&lt;p&gt;Hello everyone,&lt;/p&gt;
&lt;p&gt;Why we always want to take the red pill when we can take the blue one? That’s the question! &lt;/br&gt; In this post I will explain how to launch a shiny application from a shortcut. Just like that: &lt;iframe width=&#34;100%&#34; height=&#34;100%&#34; src=&#34;https://www.youtube.com/embed/jyGuPpbYhWk&#34; frameborder=&#34;0&#34; allowfullscreen style = &#34;max-width:100%; height:55vh;&#34;&gt;&lt;/iframe&gt;&lt;/p&gt;
&lt;p&gt;It’s fast and useful if you work with colleagues that don’t have a clue about R and just want to use your shiny app.&lt;/p&gt;
&lt;h4&gt;
If you are on macOS:
&lt;/h4&gt;
&lt;p&gt;Open a text editor and write the following lines :&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; Rscript &amp;#39;&amp;amp;FullPath/&amp;amp;file.R&amp;#39; &amp;amp;
 open http://127.0.0.1:&amp;amp;PortNumber/;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;&amp;amp;&lt;/strong&gt; allows you to write more command lines even if the shell is still busy with the previous line. The second line opens your web browser with the chosen address. A concrete example gives this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Rscript &amp;#39;/Users/user/Desktop/Clef_USB/data_science/fret_dashboard.R&amp;#39; &amp;amp;
open http://127.0.0.1:7990/;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On the second line, &lt;strong&gt;7990&lt;/strong&gt; refers to the chosen port in your shiny app. If you didn’t choose one, add the following to your shinyApp: &lt;strong&gt;options = list(port= 7990)&lt;/strong&gt;, just like that:&lt;br&gt; &lt;strong&gt;shinyApp(ui=ui,server=server, options = list(port=7990))&lt;/strong&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;If you use a &lt;strong&gt;.Rmd&lt;/strong&gt; document, you should write this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; Rscript -e  &amp;quot;rmarkdown::run(&amp;#39;&amp;amp;FullPath/&amp;amp;file.Rmd&amp;#39;, shiny_args = list(port=&amp;amp;PortNumber))&amp;quot; &amp;amp;
 open http://127.0.0.1:&amp;amp;PortNumber/&amp;amp;file.Rmd;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Replace &amp;amp;FullPath, &amp;amp;file and &amp;amp;PortNumber by the file path, the file name and the chosen port. &lt;br&gt; &lt;br&gt; Then save it with the &lt;strong&gt;“.command”&lt;/strong&gt; extension and try to launch your shiny application from the shortcut. If you have a message saying that the file could not be executed because you do not have appropriate access privileges, open your terminal and write the following line:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;chmod u+x &amp;amp;FullPath/&amp;amp;file.command&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then it should work :) &lt;br&gt; We are almost done, now we have to add another shortcut icon to make it look more friendly. For that, go on google and download free &lt;strong&gt;.icns&lt;/strong&gt; icons. I have download some icons on &lt;a href=&#34;http://www.easyicon.net/&#34;&gt;easyicon&lt;/a&gt;. To change the icon you just need to go in “get info” and drag your new icon to the previous one, just like below:&lt;br&gt;&lt;/p&gt;
&lt;iframe width=&#34;100%&#34; height=&#34;100%&#34; src=&#34;https://www.youtube.com/embed/j_Vp0HL2b90&#34; frameborder=&#34;0&#34; allowfullscreen style=&#34;max-width:100%; height:55vh;&#34;&gt;
&lt;/iframe&gt;
&lt;p&gt;&lt;br&gt;&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
If you are on Windows:
&lt;/h4&gt;
&lt;p&gt;Open a text editor and write the following lines:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;start &amp;quot;&amp;quot; &amp;quot;&amp;amp;FullPath1\Rscript.exe&amp;quot; &amp;amp;FullPath2\&amp;amp;file.R  /k
start &amp;quot;Name&amp;quot; &amp;quot;http://127.0.0.1:&amp;amp;PortNumber/&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;/k&lt;/strong&gt; allows you to write something in the command prompt even if the Prompt is busy with the previous line. A concrete example gives that :&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;start &amp;quot;&amp;quot; &amp;quot;C:\Program Files\R\R-3.3.3\bin\Rscript.exe&amp;quot; C:\Users\CGP462\Documents\Shiny_app\iris.R  /k
start &amp;quot;iris&amp;quot; &amp;quot;http://127.0.0.1:7924/&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then you can save your file with a &lt;strong&gt;.bat&lt;/strong&gt; extension. &lt;br&gt; Be carefull, if you want to launch a &lt;strong&gt;.Rmd&lt;/strong&gt; you should use this code instead :&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;start &amp;quot;&amp;amp;FullPath1\R.exe&amp;quot; -e &amp;quot;Sys.setenv(RSTUDIO_PANDOC=&amp;#39;C:/Program Files/RStudio/bin/pandoc&amp;#39;); rmarkdown::run(&amp;#39;&amp;amp;FullPath2/&amp;amp;file.Rmd&amp;#39;, shiny_args = list(port =&amp;amp;PortNumber, launch.browser = FALSE))&amp;quot; /k
start &amp;quot;&amp;amp;file&amp;quot; &amp;quot;http://127.0.0.1:7924/&amp;amp;file.Rmd&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;if it doesn’t work, check your pandoc location.&lt;/p&gt;
&lt;p&gt;It’s almost done. Now we have to change the icon. First you have to download an icon with the &lt;code&gt;.ico&lt;/code&gt; extension. Then you have to act in 4 steps : &lt;/br&gt; 1. Right click on your file and go in &lt;strong&gt;Properties&lt;/strong&gt;. &lt;/br&gt; 2. Choose the &lt;strong&gt;Shortcut&lt;/strong&gt; tab. &lt;/br&gt; 3. Then go in &lt;strong&gt;Change Icon…&lt;/strong&gt;. &lt;/br&gt; 4. In Browse, select the icon you want to see instead. &lt;/br&gt; &lt;img src=&#34;/images/micon_change.png&#34; style = &#34;max-width:100%;&#34;&gt;&lt;/p&gt;
&lt;p&gt;And now it’s done! &lt;br&gt;&lt;/p&gt;
&lt;p&gt;
Don’t hesitate to follow us on twitter &lt;a href=&#34;https://twitter.com/rdata_lu&#34; target=&#34;_blank&#34;&gt;&lt;span class=&#34;citation&#34;&gt;@rdata_lu&lt;/span&gt;&lt;/a&gt; &lt;!-- or &lt;a href=&#34;https://twitter.com/brodriguesco&#34;&gt;@brodriguesco&lt;/a&gt; --&gt; and to &lt;a href=&#34;https://www.youtube.com/channel/UCbazvBnJd7CJ4WnTL6BI6qw?sub_confirmation=1&#34; target=&#34;_blank&#34;&gt;subscribe&lt;/a&gt; to our youtube channel. &lt;br&gt; You can also contact us if you have any comments or suggestions. See you for the next post!
&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Communication between R and d3js</title>
      <link>/post/2017-09-16-communication-between-r-and-d3js/</link>
      <pubDate>Sat, 16 Sep 2017 00:00:00 +0000</pubDate>
      
      <guid>/post/2017-09-16-communication-between-r-and-d3js/</guid>
      <description>&lt;p&gt;Hello everyone,&lt;/p&gt;
&lt;p&gt;In this post we will focus on how to send a message from R to javascript. More precisely, we will focus on the basic keywords and knowledge to integrate a d3.js graph into shiny, just like &lt;a href=&#34;blog.rdata.lu/visualization/fret/&#34;&gt;here&lt;/a&gt;. If you have any issue to make shiny and javascript communicate after reading this post, I suggest you to go back to this simple &lt;a href=&#34;http://shiny.rstudio.com/gallery/server-to-client-custom-messages.html&#34;&gt;shiny app&lt;/a&gt; from Rstudio.&lt;br&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;First of all, create a folder where you will put your data file (&lt;code&gt;.csv&lt;/code&gt;, &lt;code&gt;.txt&lt;/code&gt;, etc..) and your &lt;code&gt;app.R&lt;/code&gt; (or &lt;code&gt;ui.R&lt;/code&gt; and &lt;code&gt;server.R&lt;/code&gt;). In this folder, you will create a new folder that you will name “www”. Put your &lt;code&gt;.css&lt;/code&gt; and &lt;code&gt;.js&lt;/code&gt; file inside the &lt;code&gt;www&lt;/code&gt; folder. &lt;br&gt;&lt;br&gt; &lt;img src=&#34;/img/folder_www.png&#34; style = &#34;max-width:100%;&#34;&gt; &lt;br&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;To make R and d3.js communicate, you will have to put some keywords in the server part, the ui part and the &lt;code&gt;.js&lt;/code&gt; file.&lt;br&gt;&lt;br&gt; In the &lt;code&gt;server.R&lt;/code&gt; file, write which data you want to send to javascript in an &lt;code&gt;observe({ })&lt;/code&gt; function. Just like below:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;function(input, output, session) {
    observe({
        dataSendToJs = &amp;quot;Hello World&amp;quot;
        session$sendCustomMessage(type = &amp;#39;nameOfTheMessage&amp;#39;,dataSendToJs)
    })
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;session$sendCustomMessage( )&lt;/code&gt; allows you to send your data from shiny to javascript. &lt;br&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;In the &lt;strong&gt;ui.R&lt;/strong&gt; file, source the javascript document with a tag function.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;fluidPage(
  titlePanel(&amp;quot;Example&amp;quot;),
  fluidRow(
  #Shiny way to integrate html code in the web page
  tags$head(tags$script(src = &amp;quot;message.js&amp;quot;))
  )
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;Your &lt;code&gt;.js&lt;/code&gt; file has to begin with &lt;code&gt;Shiny.addCustomMessageHandler(&amp;quot;nameOfTheMessage&amp;quot;, function(message) {&lt;/code&gt; like in the following example. Don’t forget that the message represents data from R that javascript will read so it’s better to transform the data to json data. You can do it in R via the function &lt;code&gt;jsonlite::toJSON()&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Shiny.addCustomMessageHandler(&amp;quot;nameOfTheMessage&amp;quot;,
                                      function(message) {
                                              alert(JSON.stringify(message));
                                      });&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now it should work! You can find the whole process in this repository. We started with something simple but you can do more complex things by applyng the same logic.&lt;/p&gt;
&lt;p&gt;Be careful, if you’re working locally, you have to make some changes: First, you need to have a webserver. I use &lt;a href=&#34;https://chrome.google.com/webstore/detail/web-server-for-chrome/ofhbbkphhbklhfoeikjpcbhemlocgigb?hl=en&#34;&gt;web server for chrome&lt;/a&gt;. If you choose to use the same web server, it’s very simple. &lt;br&gt;&lt;/p&gt;
&lt;p&gt;First, in “CHOOSE FOLDER”, you have to choose the &lt;code&gt;www&lt;/code&gt; folder that you made previously, then a web server url will appear. Click on it, then a new web page with the different files from &lt;code&gt;www&lt;/code&gt; should pop-up.&lt;br&gt;&lt;br&gt; &lt;img src=&#34;/image/webserver_source.png&#34; style = &#34;max-width:100%;&#34;&gt; &lt;br&gt;&lt;br&gt; In &lt;code&gt;ui.R&lt;/code&gt; (or the ui part of your single-file shiny app), source &lt;code&gt;d3.min.js&lt;/code&gt; and all &lt;code&gt;.css&lt;/code&gt; and &lt;code&gt;.js&lt;/code&gt; files as follow:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#http://127.0.0.1:8887 represents the web server URL
tags$head(tags$script(src = &amp;quot;http://127.0.0.1:8887/d3.min.js&amp;quot;))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The following video may help you if you have issues to make your application run locally: &lt;iframe width=&#34;100%&#34; height=&#34;100%&#34; src=&#34;https://www.youtube.com/embed/GHRZaiYh2Ac&#34; frameborder=&#34;0&#34; allowfullscreen style = &#34;max-width:100%; height:55vh;&#34;&gt;&lt;/iframe&gt;&lt;/p&gt;
&lt;p&gt;Now it should work :)&lt;/p&gt;
&lt;p&gt;&lt;br&gt;&lt;br&gt; You can see a complete and reproducible example over &lt;a href=&#34;https://github.com/krosamont/fret_Lux&#34;&gt;here&lt;/a&gt;.&lt;br&gt;&lt;/p&gt;
&lt;p&gt;
Don’t hesitate to follow us on twitter &lt;a href=&#34;https://twitter.com/rdata_lu&#34; target=&#34;_blank&#34;&gt;&lt;span class=&#34;citation&#34;&gt;@rdata_lu&lt;/span&gt;&lt;/a&gt; &lt;!-- or &lt;a href=&#34;https://twitter.com/brodriguesco&#34;&gt;@brodriguesco&lt;/a&gt; --&gt; and to &lt;a href=&#34;https://www.youtube.com/channel/UCbazvBnJd7CJ4WnTL6BI6qw?sub_confirmation=1&#34; target=&#34;_blank&#34;&gt;subscribe&lt;/a&gt; to our youtube channel. &lt;br&gt; You can also contact us if you have any comments or suggestions. See you for the next post!
&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>
