<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/streamline-your-analyses-linking-r-to-sas-the-workflower-experiment/"></div></div><div><a data-url="https://www.r-bloggers.com/streamline-your-analyses-linking-r-to-sas-the-workflower-experiment/" data-counturl="https://www.r-bloggers.com/streamline-your-analyses-linking-r-to-sas-the-workflower-experiment/" data-count="horizontal" data-text="Streamline your analyses linking R to SAS: the workfloweR experiment" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://datascienceplus.com/streamline-your-analyses-linking-r-to-sas/"> DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>We all know R is the first choice for statistical analysis and data visualisation, but what about big data munging? <a target="_blank" rel="nofollow" href="https://blog.rstudio.org/2016/09/15/tidyverse-1-0-0/">tidyverse</a> (or we’d better say hadleyverse) has been doing a lot in this field, nevertheless it is often the case this kind of activities being handled from some other coding language. Moreover, sometimes you get as an input pieces of analyses performed with other kind of languages or, what is worst, piece of databases packed in proprietary format (like .dta .xpt and other). So let’s assume you are an R enthusiast like I am, and you do with R all of your work, reporting included, wouldn’t be great to have some nitty gritty way to merge together all these languages in a streamlined workflow?<br>
Yes, we all know great products like microsoft Azure and sas viya, but you know what? They don’t come free, and this can sometime become an obstacle. Moreover all of them involve some kind of sophisticated setup to go trough. But what if we could reach some useful results just leveraging a useful r package and a cleaver setup? We actually can do this and I’ll show you how within coming paragraphs.</p>
<h2>The main character: rio package</h2>
<p>I met rio package some years ago, and since then, I never stopped using it. What rio package basically does is automagically guessing the file type you are trying to import and subsequently calling the proper function to effectively import within your R workspace. All you have to do is running the <code>import()</code> function, enclosing in brackets the complete path to your data file, or relative path if within your working directory. be sure to include file extension within the path string.<br>
As <code>import(),</code> rio also comes with an <code>export()</code> function, which does exactly what you are guessing: exporting your r object into a user-defined file. to complete the suite we find <code>convert()</code> which takes a file as input and convert it into a user-defined output file.<br>
How all this comes in help for our purposes? this is actually our main block: we are going to use rio to transform output from one given language into the input for R scripts or any other language. so what about the second block, our cleaver setup?</p>
<h2>The ancillary character: a cleaver setup</h2>
<p>So at the moment we know how to take non-R output as R-input and how to export R output to non-R languages, but how do we structure this in an ordered and clear manner? I came up with the following logical flow:</p>
<p><a title="" data-rel="lightbox-0" target="_blank" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/09/schermata.png"><img sizes="(max-width: 490px) 100vw, 490px" alt="schermata" class="wp-image-4795" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/09/schermata-490x224.png?resize=456%2C208" data-recalc-dims="1"></a></p>
<h3>The data step</h3>
<p>As you can see we first have a data step. this logical step involves keeping all data pieces in one physical location which all languages script will have to reference to get and release data files. We are going to teach to every language to point to that location when data loading or production is involved.</p>
<h3>The coding step</h3>
<p>The second step is represented by some languages logos. it stands for the logical step involving actual code production from different languages. within this step we consider different scripts, each of them performing its tasks pointing to the common data location. For sure we have to recognise the R script prominence, since within this script we convert, if needed, data coming from for instance from sas to data eatable from spss. it could even be the case that a proper R script is set aside only to perform this kind of task, without impacting our “real” R script where real analyses are performed.</p>
<p>Why do we have arrows coming and going from first and second step? simply because every language can take and put files into data location.</p>
<h3>The reporting step</h3>
<p> last step is the reporting one: your analyses are finalised and you want to share them with your stakeholder, what are you going to use for it? If you are a real R enthusiast you will for sure fire up an Rmarkdown file, and our logical flow is here to help.<br>
<a target="_blank" rel="nofollow" href="http://rmarkdown.rstudio.com/">Rmarkdown</a> is a powerful tool which combines main advantages of markdown with powerful features of R languages. You can actually dynamically embeed R code results within you markdown file, having the R code recompiled every time you compile the main file. This means that if your analyses changes your report will change as well, and everything will always be in sync without requiring you to get trapped within the copy and paste swamp.</p>
<p>Where your report will take the needed data from? from <code>data</code> location, of course. and this complete our workflow. But now let’s have a look to a real example:</p>
<h2>A real example: linking togheter R and sas</h2>
<p>We start our analysis munging some heavy data within sas environment. We read our data from a txt/csv file into sas, creating two sas7bdat tables which we need to merge togheter in order to associate dimensional data to some kind of customer list. so we run our two or three data step and we end up having one complete table. This table, in was language will be referenced as data.complete_data. We now want to apply some cool and trending statistical models on this data, just to show we are real data scientists, where should we do this if not in R? So here we go, creating a new R script and loading our data in. How do we do it? Leveraging rio! we will have just to run the following line of code, leaving all the hard job to the package:</p>
<pre>complete_data &lt;- import('data/complete_data.sas7bdat')</pre>
<p>This line of code will create a data.frame object ready to be analysed with our favourite language. and now it’s time to go for the last step: reporting analyses results. this is actually an easy step, since we could also merge it with the r script one: once we can read our sas-munged data into R why not readying them directly within the Rmarkdown file? Anyway, we can handle the last step in different complementary ways:</p>
<ul>
<li>producing all final results within a separate R script and saving them to an Rdata object to be loaded within the Rmarkdown file</li>
<li>producing pieces of results in sas and R joining them only within the Rmarkdown file through the import() function.</li>
<li>performing the whole analyses from munged data within the Rmarkdown file.</li>
</ul>
<p>The trade-off you have to consider when choosing within this continuum is between weight and flexibility: loading final results into rmarkdown will may results in lighter objects then raw datasets but will also generally reduce the possibility to let the user interact with your data, for instance with interactive components like the ones from shiny framework. as is often the case, the experience will teach you how to move from one side to the other.</p>
<p>Ok, so everything seems finalised, doesn’t it? not quite, since we have a bonus track: the workfloweR experiment.</p>
<h2>Bonus track: the workfloweR experiment</h2>
<p>Since I often use more than one programming language when performing my <a target="_blank" rel="nofollow" href="https://it.linkedin.com/in/cirilloandrea">job</a> and I am a lazy programmer, I decided to automize a bit all the workflow described within previous paragraph, that is where workfloweR comes from.</p>
<p>What it does is asking you ( through a simple shiny app) to define a path where you would like to initialize the analysis workspace and select languages you are going to use for your analyses. Once the selection is done and the usual initialize button is pressed, workfloweR takes care of creating within submitted path all needed data folders and languages scripts. It morover adds proper references to data folder within different scripts, for instance employing the <code>libname</code> statement to create a sas library within the sas script. finally, worflower creates an rmarkdown report ready to be employed to share results of your wonderful work.</p>
<h2>How to try workflower</h2>
<p>An updated version of workflower is currently hosted on Github an you can try it following this steps:</p>
<p>Clone or download the repository from <a target="_blank" rel="nofollow" href="https://github.com/AndreaCirilloAC/workfloweR">Github</a></p>
<p><a title="" data-rel="lightbox-1" target="_blank" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/09/github-2.png"><img sizes="(max-width: 490px) 100vw, 490px" alt="github-2" class="wp-image-4798" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/09/github-2-490x223.png?resize=456%2C208" data-recalc-dims="1"></a></p>
<p> source ‘app.R’</p>
<p><a title="" data-rel="lightbox-2" target="_blank" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/09/github-3.png"><img sizes="(max-width: 490px) 100vw, 490px" alt="github-3" class="wp-image-4799" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/09/github-3-490x318.png?resize=456%2C296" data-recalc-dims="1"></a></p>
<p> specify where to initialize your analysis workspace</p>
<p><a title="" data-rel="lightbox-3" target="_blank" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/09/github4.png"><img sizes="(max-width: 490px) 100vw, 490px" alt="github4" class="wp-image-4800" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/09/github4-490x175.png?resize=456%2C163" data-recalc-dims="1"></a></p>
<p> specify the languages you are going to use for your analyses</p>
<p><a title="" data-rel="lightbox-4" target="_blank" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/09/github-5.png"><img sizes="(max-width: 490px) 100vw, 490px" alt="github-5" class="wp-image-4801" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/09/github-5-490x233.png?resize=456%2C217" data-recalc-dims="1"></a></p>
<p> initialize the workspace</p>
<p><a title="" data-rel="lightbox-5" target="_blank" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/10/github-6.png"><img alt="github-6" class="wp-image-4807" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/10/github-6.png?resize=283%2C170" data-recalc-dims="1"></a></p>
<p> enjoy !</p>
<p>Just open up your pre-initialised R or sas scripts and start performing you analyses. When you will be done with your work an Rmarkdown file will be waiting for you within the deliverable folder.</p>
<h2>May I ask for your help?</h2>
<p>If you enjoy the idea of workflower, would you mind give me an help to further expand it? As mentioned you can find the project on github, feel free to clone it and work on it. I can see two main developement paths:</p>
<ul>
<li>Move the all stuff into an R package, which will make its use really easy directly from Github via devtools</li>
<li>Add more languages beside R and sas.</li>
</ul>
<p>Languages integration is a powerfull way to let best feature of each one shine, that’s why I thought it was a good idea to share this pragmating and simple approach to exchange data an sum up results from analyses performed with R and other languages. Let me know if you find it useful or if you have got any other similar trick.</p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p>
<ol>
<li><a title="Permanent Link toR Programming – Pitfalls to avoid (Part 1)" target="_blank" rel="bookmark" href="http://datascienceplus.com/r-programming-pitfalls-to-avoid-part-1/">R Programming – Pitfalls to avoid (Part 1)</a></li>
<li><a title="Permanent Link toEclipse – an alternative to RStudio – part 2" target="_blank" rel="bookmark" href="http://datascienceplus.com/eclipse-an-alternative-to-rstudio-part-2/">Eclipse – an alternative to RStudio – part 2</a></li>
<li><a title="Permanent Link toEclipse – an alternative to RStudio – part 1" target="_blank" rel="bookmark" href="http://datascienceplus.com/eclipse-an-alternative-to-rstudio-part-1/">Eclipse – an alternative to RStudio – part 1</a></li>
<li><a title="Permanent Link toHow to use R for matching samples (propensity score)" target="_blank" rel="bookmark" href="http://datascienceplus.com/how-to-use-r-for-matching-samples-propensity-score/">How to use R for matching samples (propensity score)</a></li>
<li><a title="Permanent Link toR for Publication by Page Piccinini: Lesson 1 – R Basics" target="_blank" rel="bookmark" href="http://datascienceplus.com/r-for-publication-by-page-piccinini-lesson-1-r-basics/">R for Publication by Page Piccinini: Lesson 1 – R Basics</a></li>
</ol></strong>
<div>
<a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=I5hVByaahCk:KO7PICHbiIg:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/datascienceplus?i=I5hVByaahCk:KO7PICHbiIg:D7DqB2pKExk" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=I5hVByaahCk:KO7PICHbiIg:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=qj6IDK7rITs" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=I5hVByaahCk:KO7PICHbiIg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=yIl2AUoC8zA" border="0"></a>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/streamline-your-analyses-linking-r-to-sas-the-workflower-experiment/"></div></div><div><a data-url="https://www.r-bloggers.com/streamline-your-analyses-linking-r-to-sas-the-workflower-experiment/" data-counturl="https://www.r-bloggers.com/streamline-your-analyses-linking-r-to-sas-the-workflower-experiment/" data-count="vertical" data-text="Streamline your analyses linking R to SAS: the workfloweR experiment" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://datascienceplus.com/streamline-your-analyses-linking-r-to-sas/"> DataScience+</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/y7f9g1ZjmA0"></a></div>