<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/forcing-yourself-to-make-your-life-easier/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://ropensci.org/blog/2018/04/12/ijtiff/"> rOpenSci - open tools for open science</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h3>The general struggle</h3>
<p>Something that will make life easier in the long-run can be the most difficult thing to do <em>today</em>. For coders, prioritising the long term may involve an overhaul of current practice and the learning of a new skill. This can be painful for a number of reasons:</p>
<ol>
<li>We have to admit to ourselves that we’ve been doing things inefficiently (i.e. wasting time). This makes us feel stupid and fosters a sense of missed opportunity: we could’ve done something cool with the time we’d have saved (e.g. vacation).</li>
<li>We’re fond of our existing methods, probably because we’re used to them and they’ve served us pretty well thus far.</li>
<li>The learning we have to do may seem beneath us: “I’m an expert in R, I’ve even written my own package. Surely ‘Version Control for Beginners’ wasn’t intended for the likes of me.” This kind of thought permits us to dismiss good ideas.</li>
<li>We’re tired. Overhauling workflows and learning new skills takes energy and concentration. Today, we’ll go through the motions just like we did yesterday. And hey, no one complained about the work we did yesterday.</li>
<li>We feel like the task is actually beyond us. This is almost never true (so long as we’re good at asking for help).</li>
<li>In the context of a work environment, when we’re concentrated on learning, our apparent output drops (often to zero) until the learning period ends. A bad manager/supervisor who doesn’t appreciate the worth of taking the time to learn how to do things better may not forgive this short-term drop in apparent output.</li>
</ol>
<blockquote>
<p>Something that will make life easier in the long-run can be the most difficult thing to do <em>today</em>.</p>
</blockquote>
<h3>One specific struggle: the <em>ImageJ</em> TIFF problem</h3>
<p>I’m doing a PhD in image analysis, working with a lot of microscopy images of cells, all of which are in the <a rel="nofollow" href="https://en.wikipedia.org/wiki/TIFF" target="_blank">TIFF</a> format. <a rel="nofollow" href="https://imagej.nih.gov/ij/" target="_blank"><em>ImageJ</em></a> is a nice software for image viewing and processing. R is a nice software for image processing but is not as good as <em>ImageJ</em> for viewing and playing around. For me, as an R enthusiast, it was ideal to do my image processing in R and my viewing in <em>ImageJ</em>.</p>
<p>On <a rel="nofollow" href="https://cran.r-project.org/" target="_blank">CRAN</a>, the <a rel="nofollow" href="https://cran.r-project.org/package=tiff" target="_blank"><code>tiff</code></a> and <a rel="nofollow" href="https://cran.r-project.org/package=magick" target="_blank"><code>magick</code></a> packages can both read TIFF files, and on <a rel="nofollow" href="https://www.bioconductor.org/" target="_blank">Bioconductor</a>, there’s <a rel="nofollow" href="https://bioconductor.org/packages/EBImage/" target="_blank"><code>EBImage</code></a>. However, all of these packages sometimes struggle with TIFF files written from <em>ImageJ</em> in that they wrongly perceive some images to have only one channel when in fact they have many (channels encode colour information: <em>colour</em> images have 3 channels – red, green and blue – whereas <em>black and white</em> or <em>greyscale</em> images have one channel – grey). Once the images are read into R, I am able to rejig them (with a combination of <code>aperm()</code> and <code>abind()</code>) into the format I want. However, the mistakes that the packages make vary, and thus the images require different rejigs. Every time I wanted to read an image into R, the process was <em>read, check, rejig, check</em>. This is a lot longer than just <em>read</em>. Nonetheless, this lengthy reading process still wasn’t that long in absolute terms: I could read in an image and have it in the format I wanted in about a minute. I processed thousands of images over the following two years (with various image analysis techniques). For each one, I went through <em>read, check, rejig, check</em> …</p>
<p>In 2016, I attended Bioconductor’s <a rel="nofollow" href="http://www-huber.embl.de/csama2018/" target="_blank">CSAMA</a> event in Italy. <a rel="nofollow" href="https://twitter.com/JennyBryan" target="_blank">Jenny Bryan</a> was there advocating the <a rel="nofollow" href="https://www.tidyverse.org/" target="_blank">tidyverse</a>, <a rel="nofollow" href="http://happygitwithr.com/" target="_blank">happygitwithr</a> and various other good ideas. At that time, I wasn’t much into the R scene, so I’d never heard of Jenny nor of many of the things she mentioned, but I was very taken by her teaching (she grabbed my attention with the tip that <code>Alt</code>+<code>-</code> in RStudio gets you the assignment operator <code>&lt;-</code>). The advice that resonated most was that <em>NOW is the time to do that thing that you know you ought to do eventually.</em> We all know that this is good advice, but that doesn’t mean we don’t need to be told it frequently.</p>
<p>Jenny advised that every regular R user should write a <em>package</em> containing the functions that they always use (rather than <code>source()</code>ing them from some random place atop every <code>.R</code> file). So I learned git, made a GitHub account and wrote my first R package. I decided to start easy, gathering together all of the functions that I use to rename and organise my files. This became (the terribly named) <a rel="nofollow" href="https://cran.r-project.org/package=filesstrings" target="_blank"><code>filesstrings</code></a> (now on CRAN). The most useful thing it can do is to extract numbers from strings with functions like <code>first_number()</code>. <code>filesstrings</code> used <code>Rcpp</code>, so I got comfortable writing R packages that use C++. Having completed <code>filesstrings</code>, I looked more seriously into writing a package that solved my <em>ImageJ</em> TIFF issue. It was clear that I would require the <code>libtiff</code> C library, and thus would need to use R’s C interface, so I looked for documentation of it. The section in Hadley Wickham’s <a rel="nofollow" href="http://adv-r.had.co.nz/C-interface.html" target="_blank"><em>Advanced R</em></a> and his GitHub repo <a rel="nofollow" href="https://github.com/hadley/r-internals" target="_blank">r-internals</a> are both good resources, but I still feel that this facet of R is under-documented. As a result, I couldn’t really get to grips with R’s C interface (at least not within a week as I’d hoped), so I gave up and returned to <em>read, check, rejig, check</em>.</p>
<h3>Eventually</h3>
<p>In the end, I did write <code>ijtiff</code>—the package I had been longing for—using <code>libtiff</code> and R’s C interface. What happened that pushed me over the edge to learn everything I needed to know about C and R’s interface to it and to toil away for six weeks with <code>clang</code> errors and RStudio crashes and <code>valgrind</code> in Linux virtual machines? Nothing in particular. I was doing yet another <em>read, check, rejig, check</em> and I had had enough. I decided to take another crack at <code>ijtiff</code>. This time, with the help of what I’d learned during all of my previous failed attempts, I made it over the line.</p>
<p>So of course I was able to do it all along, it just took longer than I thought (it’s just reading a TIFF file into an array, right?). Most things take longer than we think, so that shouldn’t necessarily discourage us so immediately. Of course I should’ve done it right at the start of my PhD (rather than at the start of my 4th and final year), but for a combination of the silly reasons outlined above, I hesitated. <em>I hesitated for three years.</em> Anyway, it’s done now and I’m very happy with it. My feeling of delight that it’s now done easily trumps the feeling that I wasted time by waiting so long to do it. My R life is more efficient and less frustrating now. To check out <code>ijtiff</code> for yourself, see the <a rel="nofollow" href="https://cran.r-project.org/package=ijtiff" target="_blank">CRAN</a> page, the <a rel="nofollow" href="https://github.com/ropensci/ijtiff" target="_blank">GitHub</a> or the <a rel="nofollow" href="https://cran.r-project.org/web/packages/ijtiff/vignettes/ijtiff.html" target="_blank">vignette</a>.</p>
<blockquote>
<p>With the help of what I’d learned during all of my previous failed attempts, I made it over the line.</p>
</blockquote>
<h3>Immediate future</h3>
<p>As mentioned previously, there is a lack of accessible information on R’s C interface. The demand on a few experts like <a rel="nofollow" href="http://hadley.nz/" target="_blank">Hadley Wickham</a>, <a rel="nofollow" href="https://kevinushey.github.io/" target="_blank">Kevin Ushey</a>, <a rel="nofollow" href="https://github.com/jimhester" target="_blank">Jim Hester</a> etc. to provide resources for the rest of us is huge. Although these people are very generous and well-appreciated, they only have a finite amount of time. As such, I want to contribute to the documentation on R’s C interface.</p>
<h3>Conclusions</h3>
<ol>
<li>Find and heed all of <a rel="nofollow" href="https://twitter.com/JennyBryan" target="_blank">Jenny Bryan</a>’s advice.</li>
<li>If there’s something that will make your life easier in the long-run but seems like a bit of a hurdle right now, do it. If necessary, take the time to explain to others around you why it’s a good idea and <em>do it</em>. If it takes longer than you thought to learn how to do it, don’t conclude from that that you can’t do it, just conclude that it’s not very easy and get all the help you can find.</li>
<li>If it’s a lack of skills that prevents you from doing what you wish you could do, take the time to learn those skills. They’ll make your life easier again and again in future.</li>
<li>If you figure out how to do something that you wish was made easier in documentation, please—for the likes of me—contribute that new knowledge to existing documentation.</li>
</ol>
<h4>Thanks</h4>
<ul>
<li>Thanks to <a rel="nofollow" href="https://ropensci.org/" target="_blank">rOpenSci</a> for their very helpful review of the <code>ijtiff</code> package. This type of review where the reviewers actively help you as well as objectively evaluating your work is a revelation. Getting the advice and help of people like <a rel="nofollow" href="https://github.com/jonclayden" target="_blank">Jon Clayden</a> and <a rel="nofollow" href="https://github.com/jeroen" target="_blank">Jeroen Ooms</a> was invaluable. Thanks also to editor <a rel="nofollow" href="https://github.com/sckott" target="_blank">Scott Chamberlain</a> and to community manager <a rel="nofollow" href="https://twitter.com/StefanieButland" target="_blank">Stefanie Butland</a> who is helping me with this blog.</li>
<li>Thanks to <a rel="nofollow" href="https://twitter.com/JennyBryan" target="_blank">Jenny Bryan</a> for great advice.</li>
<li>Thanks to <a rel="nofollow" href="http://hadley.nz/" target="_blank">Hadley Wickham</a>, <a rel="nofollow" href="https://kevinushey.github.io/" target="_blank">Kevin Ushey</a>, <a rel="nofollow" href="https://github.com/jimhester" target="_blank">Jim Hester</a> and others for making R usable for the rest of us.</li>
<li>Thanks to my supervisor <a rel="nofollow" href="https://www.researchgate.net/profile/Sergi_Padilla-Parra2" target="_blank">Sergi Padilla-Parra</a> and the other members of his group for having great ideas, believing in my ideas and making my PhD fun and stress free.</li>
<li>Thanks to my lovely fiancée Naomi for her continued support throughout my PhD and for editing my lacklustre writing.</li>
</ul>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://ropensci.org/blog/2018/04/12/ijtiff/"> rOpenSci - open tools for open science</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/vMPfYj-2w24" height="1"></a></div>