<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>web development Archives - Ger Inberg</title>
	<atom:link href="https://gerinberg.com/category/web-dev/feed/" rel="self" type="application/rss+xml" />
	<link>https://gerinberg.com/category/web-dev/</link>
	<description>data science developer</description>
	<lastBuildDate>Thu, 11 Aug 2022 18:09:06 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>

<image>
	<url>https://gerinberg.com/wp-content/uploads/2017/05/favicon-150x150.jpg</url>
	<title>web development Archives - Ger Inberg</title>
	<link>https://gerinberg.com/category/web-dev/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Multi page shiny apps</title>
		<link>https://gerinberg.com/2022/08/11/multi-page-shiny-apps/</link>
		
		<dc:creator><![CDATA[Ger]]></dc:creator>
		<pubDate>Thu, 11 Aug 2022 18:09:05 +0000</pubDate>
				<category><![CDATA[data viz]]></category>
		<category><![CDATA[shiny]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[donation]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[R]]></category>
		<guid isPermaLink="false">https://gerinberg.com/?p=1834</guid>

					<description><![CDATA[<p>Web applications can have rich functionality nowadays. For example a website of an E-commerce shop has a page about the products they are selling, a page about their conditions, a shopping cart page and an order page. Furthermore it can handle different HTTP requests from [&#8230;]</p>
<p>The post <a href="https://gerinberg.com/2022/08/11/multi-page-shiny-apps/">Multi page shiny apps</a> appeared first on <a href="https://gerinberg.com">Ger Inberg</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Web applications can have rich functionality nowadays. For example a website of an E-commerce shop has a page about the products they are selling, a page about their conditions, a shopping cart page and an order page. Furthermore it can handle different HTTP requests from the user. A GET request is used to retrieve a page (e.g. products) from the server whereas a POST request is used to send information to the server (e.g. make order).</p>
<h4>Problem</h4>
<p>Shiny apps are, by default, a bit limited when looking at it from this perspective.  It can handle only GET requests by default, unless you are a technical expert in this field, see <a href="https://stackoverflow.com/questions/25297489/accept-http-request-in-r-shiny-application" target="_blank" rel="noopener">this stack overflow</a> post. Furthermore shiny apps can have just one entry-point &#8220;/&#8221;. So you can&#8217;t have another entry-point &#8220;/page2&#8221;. Thus, the e-commerce shop is not possible out of the box in R shiny.</p>
<h4>Solution</h4>
<p>There are multiple solutions to support multiple pages. The one that I am using since a while is the package <a href="https://github.com/ColinFay/brochure" target="_blank" rel="noopener">brochure </a>developed by Colin Fay. It is still in development, so you might encounter some issues but I haven&#8217;t found any major bugs yet. A brochure app consists of a series of pages that are defined by an endpoint/path, a UI and a server function. Thus each page has its own shiny session, its own UI, and its own server! This is important to keep in mind.  A separate session for each page has some advantages but also some disadvantages (e.g. how to pass user data between pages?). A very simple brochureApp looks like this:</p>
<pre class="highlight"><code><span class="n">library(shiny)
library(brochure)

brochureApp</span><span class="p">(</span>
  <span class="c1"># First page</span>
  <span class="n">page</span><span class="p">(</span>
    <span class="n">href</span> <span class="o">=</span> <span class="s2">"/"</span><span class="p">,</span>
    <span class="n">ui</span> <span class="o">=</span> <span class="n">fluidPage</span><span class="p">(</span>
      <span class="n">h1</span><span class="p">(</span><span class="s2">"My first page"</span><span class="p">),</span> 
      <span class="n">plotOutput</span><span class="p">(</span><span class="s2">"plot"</span><span class="p">)</span>
    <span class="p">),</span>
    <span class="n">server</span> <span class="o">=</span> <span class="k">function</span><span class="p">(</span><span class="n">input</span><span class="p">,</span> <span class="n">output</span><span class="p">,</span> <span class="n">session</span><span class="p">){</span>
      <span class="n">output</span><span class="o">$</span><span class="n">plot</span> <span class="o">&lt;-</span> <span class="n">renderPlot</span><span class="p">({</span>
        <span class="n">plot</span><span class="p">(</span><span class="n">iris</span><span class="p">)</span>
      <span class="p">})</span>
    <span class="p">}</span>
  <span class="p">),</span> 
  <span class="c1"># Second page, no server-side function</span>
  <span class="n">page</span><span class="p">(</span>
    <span class="n">href</span> <span class="o">=</span> <span class="s2">"/page2"</span><span class="p">,</span> 
    <span class="n">ui</span> <span class="o">=</span>  <span class="n">fluidPage</span><span class="p">(</span>
      <span class="n">h1</span><span class="p">(</span><span class="s2">"My second page"</span><span class="p">)</span>
    <span class="p">)</span>
  <span class="p">)</span>
<span class="p">)</span></code></pre>
<h4>Donation app</h4>
<p>Coming back to the E-commerce shop example, I have developed an app where one can sponsor me for my open source work on R packages. The app has an integration with Stripe to make a donation and a thank you and error page. When calling Stripe you have to give the two endpoints for these pages and by using brochure, I am able to setup these endpoints. See the app on <a href="https://ginberg.shinyapps.io/donate/" target="_blank" rel="noopener">shinyapps.io</a> and of course I would appreciate it, if you use the app!:-)</p>
<p>The post <a href="https://gerinberg.com/2022/08/11/multi-page-shiny-apps/">Multi page shiny apps</a> appeared first on <a href="https://gerinberg.com">Ger Inberg</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>REST API with R</title>
		<link>https://gerinberg.com/2020/05/04/r-rest-api/</link>
		
		<dc:creator><![CDATA[Ger]]></dc:creator>
		<pubDate>Mon, 04 May 2020 16:06:00 +0000</pubDate>
				<category><![CDATA[web development]]></category>
		<guid isPermaLink="false">https://gerinberg.com/?p=1686</guid>

					<description><![CDATA[<p>REST APIs are everywhere around us. Software engineers use them to develop backend logic, and data scientists use them to deploy machine learning models. In this blog you’ll learn how to make a REST API with R and the plumber package. Introduction to REST APIs [&#8230;]</p>
<p>The post <a href="https://gerinberg.com/2020/05/04/r-rest-api/">REST API with R</a> appeared first on <a href="https://gerinberg.com">Ger Inberg</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><span data-preserver-spaces="true">REST APIs are everywhere around us. Software engineers use them to develop backend logic, and data scientists use them to deploy machine learning models. In this blog you’ll learn how to make a REST API with R and the <code>plumber</code> package.</span></p>
<h4 id="introduction"><span data-preserver-spaces="true">Introduction to REST APIs</span></h4>
<p><span data-preserver-spaces="true">REST stands for “Representational State Transfer”. In simpler words, it represents a set of rules that developers follow when creating APIs. The most important rule is that you should get a response (data) whenever you make a request to a particular URL.</span></p>
<p><span data-preserver-spaces="true">The request you make consists of four components:</span></p>
<ul>
<li><strong><span data-preserver-spaces="true">Endpoint</span></strong><span data-preserver-spaces="true"> – a part of the URL you visit. For example, the endpoint of the URL </span><em><span data-preserver-spaces="true"><a class="vglnk" href="https://gerinberg.com/predict" rel="nofollow">https://gerinberg.com/predict</a></span></em><span data-preserver-spaces="true"> is </span><em><span data-preserver-spaces="true">/predict</span></em></li>
<li><strong><span data-preserver-spaces="true">Method</span></strong><span data-preserver-spaces="true"> – a type of request you’re sending, can be either GET, POST, PUT, PATCH, and DELETE. They are used to perform one of these actions: Create, Read, Update, Delete (CRUD)</span></li>
<li><strong><span data-preserver-spaces="true">Headers</span></strong><span data-preserver-spaces="true"> – used for providing information (think authentication credentials, for example). They are provided as key-value pairs</span></li>
<li><strong><span data-preserver-spaces="true">Body</span></strong><span data-preserver-spaces="true"> – information that is sent to the server. Used only when not making GET requests.</span></li>
</ul>
<p><span data-preserver-spaces="true">Most of the time, the response returned after making a request is in JSON format. The alternative format is XML, but JSON is more common. You can also return other objects, such as </span><strong><span data-preserver-spaces="true">images</span></strong><span data-preserver-spaces="true"> instead. </span></p>
<p><span data-preserver-spaces="true">R allows you to develop REST APIs with the plumber package. You can read the official documentation </span><a class="editor-rtfLink" href="https://www.rplumber.io/" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">here</span></a><span data-preserver-spaces="true">.</span></p>
<p><span data-preserver-spaces="true">It’s easy to rewrite any R script file to an API with plumber, because you only have to decorate your functions with comments. You’ll see that in the next section.</span></p>
<h4 id="develop"><span data-preserver-spaces="true">Develop a Simple REST API with R &amp; Plumber</span></h4>
<p><span data-preserver-spaces="true">To start, create an empty R script file. You’ll need a couple of packages:</span></p>
<ul>
<li><span data-preserver-spaces="true">plumber – to develop the API</span></li>
<li><span data-preserver-spaces="true">dplyr– to filter datasets based on certain fields</span></li>
<li><span data-preserver-spaces="true">gapminder – the dataset that we will use for the API.</span></li>
</ul>
<p><span data-preserver-spaces="true">You can place two comments for specifying API title and description. These two aren’t mandatory, but it&#8217;s good to document the API call. </span><span data-preserver-spaces="true">Let&#8217;s create our endpoint now.</span></p>
<h4><span data-preserver-spaces="true">Endpoint /countries</span></h4>
<p><span data-preserver-spaces="true">The idea behind this endpoint is that it should return countries and their respective data after a couple of filters are applied. To be more precise, this endpoint accepts parameter values for continent, life expectancy, and population. Value for continent must be exact, and values for the other two parameters filter data so that only rows with greater values are returned.</span></p>
<p><span data-preserver-spaces="true">If you want to do things right, it’ll require many comments, as you’ve seen previously. It’s a good practice to write a short description, list the parameters, and it’s mandatory to specify the request type and the endpoint.</span></p>
<p><span data-preserver-spaces="true">Below the comments, you’ll place a function that performs the necessary logic and returns the results.</span></p>
<p><span data-preserver-spaces="true">Let’s think about the endpoint parameters for a second. You’ll need:</span></p>
<ul>
<li><strong><span data-preserver-spaces="true">continent</span></strong><span data-preserver-spaces="true"> – column </span><em><span data-preserver-spaces="true">continent</span></em></li>
<li><strong><span data-preserver-spaces="true">life expectancy</span></strong><span data-preserver-spaces="true"> – column </span><em><span data-preserver-spaces="true">lifeExp</span></em></li>
<li><strong><span data-preserver-spaces="true">population</span></strong><span data-preserver-spaces="true"> – column </span><em><span data-preserver-spaces="true">pop</span></em></li>
</ul>
<p><span data-preserver-spaces="true">All three are optional, and you can do the filtering based on the parameter values with the dplyr package. This endpoint will return data for the most recent year only, which is 2007.</span></p>
<p><span data-preserver-spaces="true">Here’s the <a href="https://github.com/ginberg/r-api/blob/main/api.R">complete code</a> so far:</span></p>
<div class="envira-gallery-feed-output"><img decoding="async" class="envira-gallery-feed-image" src="https://gerinberg.com/wp-content/uploads/2021/02/api-1-640x480.png" title="api" alt="" /></div>
<p><span data-preserver-spaces="true">If you were to run the API now, this is what you’d see:</span></p>
<div id="attachment_6415" class="wp-caption aligncenter">
<p><div class="envira-gallery-feed-output"><img decoding="async" class="envira-gallery-feed-image" src="https://gerinberg.com/wp-content/uploads/2021/02/swagger-2-1024x356-640x480.png" title="swagger" alt="" /></div><img decoding="async" class="alignnone size-full wp-image-1698" src="https://gerinberg.com/wp-content/uploads/2021/02/swagger.png" alt="" width="1" height="1" /><img decoding="async" class="alignnone size-full wp-image-1698" src="https://gerinberg.com/wp-content/uploads/2021/02/swagger.png" alt="" width="1" height="1" /></p>
</div>
<p><span data-preserver-spaces="true">The endpoint on the bottom of the image (blue box) is clickable. Clicking on it expands a new section:</span></p>
<div id="attachment_6416" class="wp-caption aligncenter"><div class="envira-gallery-feed-output"><img decoding="async" class="envira-gallery-feed-image" src="https://gerinberg.com/wp-content/uploads/2021/02/countries_call-2-1024x598-640x480.png" title="countries_call" alt="" /></div></div>
<p><span data-preserver-spaces="true">You can click on the “Try it out” button to make a request straight from the browser.  As you can see, the parameters have some values filled in already. </span><span style="font-size: inherit;">Once you click on the “Execute” button, the response will be shown at the bottom. Here’s how it looks like for the default parameters:</span></p>
<div id="attachment_6418" class="wp-caption aligncenter"><div class="envira-gallery-feed-output"><img decoding="async" class="envira-gallery-feed-image" src="https://gerinberg.com/wp-content/uploads/2021/02/countries-1-1024x324-640x480.png" title="countries" alt="" /></div></div>
<p><span data-preserver-spaces="true">Great, you have created your first endpoint! It takes data in and returns data out. You can also return other outputs like an image.</span></p>
<h4 id="conclusion"><span data-preserver-spaces="true">Conclusion</span></h4>
<p><span data-preserver-spaces="true">In this blog post you have learned what REST APIs are, what’s the use of the plumber package, and how to use it to build basic APIs in R. </span></p>
<p><span data-preserver-spaces="true">APIs in R are commonly developed for exposing the result of machine learning models.  Another option to share your data analysis results is to create a dashboard or a report with Shiny or Markdown.</span></p>
<h4><span data-preserver-spaces="true">Learn more about R</span></h4>
<p><a href="https://gerinberg.com/2020/06/01/r-linear-regression/">Lineair Regression with R</a></p>

<p>The post <a href="https://gerinberg.com/2020/05/04/r-rest-api/">REST API with R</a> appeared first on <a href="https://gerinberg.com">Ger Inberg</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
