Jekyll2019-08-12T00:55:29+01:00/feed.xmlTiposaurusTips and tricks for using Excel and WordSteve RumfordCalculate Net Present Value (NPV) in PHP2016-11-02T09:50:19+00:002016-11-02T09:50:19+00:00/php/2016/11/02/php-calcluate-npv-net-present-value<p>In this guide, we’ll show you how to calculate the Net Present Value (NPV) of a series of future cashflows using a PHP script.</p> <h2 id="net-present-value-overview">Net Present Value Overview</h2> <p>Using a little financial maths, we find the Net Present Value of a cashflow <script type="math/tex">X</script> at a future time, <script type="math/tex">t</script> and interest rate <script type="math/tex">i</script> by: $NPV = \frac{X}{(1+i)^{t}}$</p> <p>So, for example, the NPV of a payment of $100 in 2 years’ time with an (annually compounding) interest rate of 5% can be found using the above formula with the following values:</p> <ul> <li>$$X = 100$$</li> <li>$$t = 2$$</li> <li>$$i = 0.05$$</li> </ul> <p>So we have: $NPV = \frac{100}{(1.05)^{2}} = 90.70$</p> <p>This reflects the idea of compound interest where:</p> <ul> <li>$90.70 invested now would earn interest at 5% in the first year to grow to <script type="math/tex">90.70 \times 1.05 = 95.24</script> after one year</li> <li>Then if left invested, both the original amount and the first year’s interest would earn interest in a second year. This causes it to grow to <script type="math/tex">95.24 \times 1.05 = 100</script></li> <li>We could also have verified this by: <script type="math/tex">90.70 \times 1.05^2 = 100</script></li> </ul> <p>So if we can invest $90.70 now, at 5% interest, it would grow to$100 in 2 years’ time. Thus we say that $90.70 is the <em>present value</em> of$100 in 2 years’ time.</p> <p>We can implement this in PHP using the following simple script. Note we use the <code class="highlighter-rouge">pow(base, exp)</code> function to raise one number (the <em>base</em>) to the power of another (the <em>exponent</em>).</p> <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="cp">&lt;?php</span> <span class="nv">$x</span> <span class="o">=</span> <span class="mi">100</span><span class="p">;</span> <span class="nv">$i</span> <span class="o">=</span> <span class="mf">0.05</span><span class="p">;</span> <span class="nv">$t</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span> <span class="nv">$npv</span> <span class="o">=</span> <span class="nv">$x</span> <span class="o">/</span> <span class="nb">pow</span><span class="p">(</span><span class="mi">1</span><span class="o">+</span><span class="nv">$i</span><span class="p">,</span> <span class="nv">$t</span><span class="p">);</span> <span class="k">echo</span> <span class="nv">$npv</span><span class="p">;</span> <span class="c1">// Output: 90.702947845805</span> <span class="cp">?&gt;</span></code></pre></figure> <h2 id="npv-of-a-series-of-yearly-cashflows">NPV of a series of yearly cashflows</h2> <p>Expanding on the above, suppose we have cashflows <script type="math/tex">X_0, X_1, ..., X_n</script> paid at yearly intervals, starting immediately, and that the interest rate is still 5%. We can calculate the NPV of all cashflows by calculating the present value of each one individually, then adding them all up.</p> <p>$NPV = \sum_{t=0}^{n} \frac{X_t}{(1+i)^{t}}$</p> <p>We’ll build a function to do this in PHP:</p> <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="cp">&lt;?php</span> <span class="k">function</span> <span class="nf">npv</span><span class="p">(</span><span class="nv">$cashflows</span><span class="p">,</span> <span class="nv">$intRate</span><span class="p">)</span> <span class="p">{</span> <span class="nv">$npv</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nv">$year</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="c1">// First cashflow at time 0</span> <span class="k">foreach</span><span class="p">(</span><span class="nv">$cashflows</span> <span class="k">as</span> <span class="nv">$cf</span><span class="p">)</span> <span class="p">{</span> <span class="nv">$npv</span> <span class="o">+=</span> <span class="nv">$cf</span> <span class="o">/</span> <span class="nb">pow</span><span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="nv">$intRate</span><span class="p">,</span> <span class="nv">$year</span><span class="p">);</span> <span class="nv">$year</span><span class="o">++</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span><span class="p">(</span><span class="nv">$npv</span><span class="p">);</span> <span class="p">}</span> <span class="nv">$test_cf</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="mi">100</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">40</span><span class="p">,</span> <span class="mi">50</span><span class="p">];</span> <span class="k">echo</span> <span class="nx">npv</span><span class="p">(</span><span class="nv">$test_cf</span><span class="p">,</span> <span class="mf">0.05</span><span class="p">);</span> <span class="c1">// Output: 25.663934364013</span> <span class="cp">?&gt;</span></code></pre></figure> <p>Breaking it down, this function takes arguments <code class="highlighter-rouge">$cashflows</code> - an array of cashflows - and <code class="highlighter-rouge">$intRate</code> - the interest rate at which to value the cashflows. It does the following calculation steps:</p> <ul> <li>Use a variable <code class="highlighter-rouge">$npv</code> to keep track of the running total, which we initialise to zero.</li> <li>Loop through the <code class="highlighter-rouge">$cashflows</code> array, calculating the present value of each using the formula above, and adding that to the total, <code class="highlighter-rouge">$npv</code>.</li> <li>Return the value <code class="highlighter-rouge">$npv</code>.</li> </ul> <h2 id="npv-of-a-series-of-cashflows-at-different-times">NPV of a series of cashflows at different times</h2> <p>Suppose the cashflows you’re interested in don’t occur at exactly yearly intervals, but at various other points in the future. It’s simple to adjust the above example to cope with this, and we can just add an extra array <code class="highlighter-rouge">$times</code> as an argument to the function, and use this when calculating the present value of each cashflow.</p> <p>We use this function to replicate the result of our <code class="highlighter-rouge">npv()</code> function created above, and then to calculate the NPV with different timings.</p> <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="cp">&lt;?php</span> <span class="k">function</span> <span class="nf">npv_times</span><span class="p">(</span><span class="nv">$cashflows</span><span class="p">,</span> <span class="nv">$times</span><span class="p">,</span> <span class="nv">$intRate</span><span class="p">)</span> <span class="p">{</span> <span class="nv">$npv</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nv">$i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">foreach</span><span class="p">(</span><span class="nv">$cashflows</span> <span class="k">as</span> <span class="nv">$cf</span><span class="p">)</span> <span class="p">{</span> <span class="nv">$year</span> <span class="o">=</span> <span class="nv">$times</span><span class="p">[</span><span class="nv">$i</span><span class="p">];</span> <span class="nv">$npv</span> <span class="o">+=</span> <span class="nv">$cf</span> <span class="o">/</span> <span class="nb">pow</span><span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="nv">$intRate</span><span class="p">,</span> <span class="nv">$year</span><span class="p">);</span> <span class="nv">$i</span><span class="o">++</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span><span class="p">(</span><span class="nv">$npv</span><span class="p">);</span> <span class="p">}</span> <span class="nv">$test_cf</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="mi">100</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">40</span><span class="p">,</span> <span class="mi">50</span><span class="p">];</span> <span class="nv">$test_times</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">];</span> <span class="k">echo</span> <span class="nx">npv_times</span><span class="p">(</span><span class="nv">$test_cf</span><span class="p">,</span> <span class="nv">$test_times</span><span class="p">,</span> <span class="mf">0.05</span><span class="p">);</span> <span class="c1">// Output: 25.663934364013</span> <span class="nv">$test_times2</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">9</span><span class="p">];</span> <span class="k">echo</span> <span class="nx">npv_times</span><span class="p">(</span><span class="nv">$test_cf</span><span class="p">,</span> <span class="nv">$test_times2</span><span class="p">,</span> <span class="mf">0.05</span><span class="p">);</span> <span class="c1">// Output: 10.677272151561</span> <span class="cp">?&gt;</span></code></pre></figure> <p>Hope this helps. As always if you have any comments please add them in the comments box below.</p>Steve RumfordIn this guide, we’ll show you how to calculate the Net Present Value (NPV) of a series of future cashflows using a PHP script.Output HTML with your PHP Script2016-10-27T10:50:19+01:002016-10-27T10:50:19+01:00/php/2016/10/27/php-output-html<p>In this tutorial, I’ll show you how to output HTML from a PHP script.</p> <p>This is very similar to our tutorial on outputting text using PHP (first script - hello world), but we’ll be looking at HTML. HTML is the language of the web, so you’ll encounter it frequently when building websites.</p> <h2 id="the-script">The Script</h2> <p>Create a file called html.php with the following code and upload it to your webserver</p> <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="s2">"&lt;h1&gt;Hello World!&lt;/h1&gt;"</span><span class="p">;</span> <span class="cp">?&gt;</span></code></pre></figure> <p>The output of this is the following HTML:</p> <figure class="highlight"><pre><code class="language-html" data-lang="html"><span class="nt">&lt;h1&gt;</span>Hello World<span class="nt">&lt;/h1&gt;</span></code></pre></figure> <p>Which looks like the following when you view it in the browser:</p> <p><img src="/assets/hello_world_html.png" alt="Hello World!" /></p> <h2 id="explanation">Explanation</h2> <p>Here, we use the <code class="highlighter-rouge">echo</code> command to send text to the browser, but the text contains HTML tags. The browser will recognise this as HTML and format the page as you tell it - here the <code class="highlighter-rouge">&lt;h2&gt;</code> tag will mean that the text <code class="highlighter-rouge">Hello World!</code> is formatted as a heading.</p> <p>As before we saw in the previous tutorial, the <code class="highlighter-rouge"><span class="cp">&lt;?php</span></code> and <code class="highlighter-rouge">?&gt;</code> tags open execution of PHP code, the <code class="highlighter-rouge">echo</code> command says to output some text (i.e. send it to the browser), and we follow it with the string <code class="highlighter-rouge">"&lt;h1&gt;Hello World&lt;/h1&gt;"</code>, which contains HTML code.</p> <p>We should say that the above code doesn’t really utilise PHP to do anything interesting - it just prints out text, which you could have done with a static HTML page. However, we’ll be able to build on this to do more interesting things soon.</p> <h2 id="special-characters">Special Characters</h2> <p>You might need to output some characters such as quotation marks <code class="highlighter-rouge">"</code> in your HTML, but you will notice that we used those to mark the start and end of the string above. So how does PHP distinguish between the <code class="highlighter-rouge">"</code> which marks the start of the string, and that which is part of it? You can do this using a backslash (<code class="highlighter-rouge">\</code>) before the quotation mark which is part of the string: <code class="highlighter-rouge">\"</code>. This is known as an <em>escape character</em>.</p> <p>Alternatively, you can use single quotes <code class="highlighter-rouge">'</code> to mark the start and end of the string, and double quotes within the string will be output without any special treatment. You can see these in the examples below, which both result in the same HTML being output:</p> <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="s1">'&lt;img src="hello.jpg"&gt;'</span><span class="p">;</span> <span class="k">echo</span> <span class="s2">"&lt;img src=</span><span class="se">\"</span><span class="s2">hello.jpg</span><span class="se">\"</span><span class="s2">&gt;"</span><span class="p">;</span> <span class="cp">?&gt;</span></code></pre></figure> <h2 id="line-breaks">Line Breaks</h2> <p>If you want to add a new line, you can do this using the <code class="highlighter-rouge">\n</code> special character (in a string which starts and ends with <code class="highlighter-rouge">"</code>). But remember, this will only show up in the <em>source code</em> of the page (which is the same as if you press enter when writing HTML code by hand), and won’t affect how it is shown to the visitor. If you want to add a new line which the visitor can see, use HTML to format it - such as the line break <code class="highlighter-rouge">&lt;br&gt;</code>.</p> <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="s2">"&lt;p&gt;Hello </span><span class="se">\n</span><span class="s2">World&lt;/p&gt;"</span><span class="p">;</span> <span class="k">echo</span> <span class="s2">"&lt;p&gt;Hello &lt;br&gt;World&lt;/p&gt;"</span><span class="p">;</span> <span class="cp">?&gt;</span></code></pre></figure> <p>This example produces the following HTML source:</p> <figure class="highlight"><pre><code class="language-html" data-lang="html"><span class="nt">&lt;p&gt;</span>Hello World<span class="nt">&lt;/p&gt;</span> <span class="nt">&lt;p&gt;</span>Hello <span class="nt">&lt;br&gt;</span>World<span class="nt">&lt;/p&gt;</span></code></pre></figure> <p>But it looks like the following to a visitor in a web browser - note the line breaks are different to those in the source code:</p> <p><img src="/assets/hello_world_html2.png" alt="Hello World with line breaks" /></p>Steve RumfordIn this tutorial, I’ll show you how to output HTML from a PHP script.Your First PHP Script - Hello World!2016-10-26T10:50:19+01:002016-10-26T10:50:19+01:00/php/2016/10/26/php-hello-world<p>In this series of tutorials, I’ll give you a little introduction to the PHP programming language, which is used to dynamically generating web pages. If you follow them all through, you’ll be able to understand and write PHP scripts, enabling you to do many powerful things.</p> <p>We all have to start somewhere though, and no programming tutorial would be complete without a simple program to output ‘Hello World’.</p> <h2 id="the-script">The script</h2> <p>So, assuming you’ve got access to a webserver with PHP (either on your local machine, or web hosting), create a file called hello.php and add the following code to it:</p> <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="s1">'Hello World!'</span><span class="p">;</span> <span class="cp">?&gt;</span></code></pre></figure> <p>IF you load this page in a web browser, you should see get the following output:</p> <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="nx">Hello</span> <span class="nx">World</span><span class="o">!</span></code></pre></figure> <h2 id="breaking-it-down">Breaking it down</h2> <p>We’ll have a look at what each part of this does. First, the tags:</p> <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="cp">&lt;?php</span> <span class="cp">?&gt;</span></code></pre></figure> <p>This <code class="highlighter-rouge"><span class="cp">&lt;?php</span></code> tag which marks the start of some PHP code, and goes hand in hand with the tag at the end, <code class="highlighter-rouge">?&gt;</code>. These are similar to When you load this page in the browser, the server will treat everything between the <code class="highlighter-rouge"><span class="cp">&lt;?php</span></code> and <code class="highlighter-rouge">?&gt;</code> tags as PHP code. Everything outside these tags is just treated as text and will be printed to the browser, for example like HTML on any other webpage.</p> <p>The only other part in this simple script is the line:</p> <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="k">echo</span> <span class="s1">'Hello World!'</span><span class="p">;</span></code></pre></figure> <p>The <code class="highlighter-rouge">echo</code> command says to send something to the browser as text. It’s followed by <code class="highlighter-rouge">'Hello World'</code> - this is a <em>string</em> which is just text held in quotes (note that it is surrounded by single quotes <code class="highlighter-rouge">'</code>, although it could also have been surrounded by double quotes <code class="highlighter-rouge">"</code>).</p> <p>An important point is that the line ends in a semicolon, <code class="highlighter-rouge">;</code>. All lines of PHP code must end in a semicolon like this - you’ll get an error if you miss one out.</p> <h2 id="what-does-a-visitor-see">What does a visitor see?</h2> <p>Assuming everyhing works correctly, someone visiting this as a webpage in their browser won’t ever see the PHP code, even if they look at the source code of the page. This is because the PHP code is parsed and executed on the webserver, and what gets sent to a visitor is just the output of that code.</p> <p>In this case, it’s just the line <code class="highlighter-rouge">Hello World!</code></p> <h2 id="summary">Summary</h2> <p>That’s it - we use PHP tags, <code class="highlighter-rouge"><span class="cp">&lt;?php</span></code> and <code class="highlighter-rouge">?&gt;</code> to mark the start and end of PHP code in a page. The <code class="highlighter-rouge">echo</code> command says to output something as text - in this case the string <code class="highlighter-rouge">'Hello World!'</code>. And all lines of PHP code must end in a semicolon.</p> <h2 id="further-details">Further Details</h2> <p>You can mix PHP code and text within a page, and even have multiple blocks of PHP code, each within their own sets of <code class="highlighter-rouge"><span class="cp">&lt;?php</span></code> and <code class="highlighter-rouge">?&gt;</code> tags. For example:</p> <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="s1">'Hello '</span><span class="p">;</span> <span class="cp">?&gt;</span> World <span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="s2">"!"</span><span class="p">;</span> <span class="cp">?&gt;</span></code></pre></figure> <p>This produces the same output as above, namely <code class="highlighter-rouge">Hello World!</code></p> <p>You can change this to print any text you like, for example:</p> <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="s2">"Purple Monkey Dishwasher"</span><span class="p">;</span> <span class="cp">?&gt;</span></code></pre></figure> <p>The output of this is: <code class="highlighter-rouge">Purple Monkey Dishwasher</code></p>Steve RumfordIn this series of tutorials, I’ll give you a little introduction to the PHP programming language, which is used to dynamically generating web pages. If you follow them all through, you’ll be able to understand and write PHP scripts, enabling you to do many powerful things.How to save plots in R to files2016-09-27T10:50:19+01:002016-09-27T10:50:19+01:00/r/2016/09/27/save-r-plot-to-png<p>If you have generated a chart in <strong>R</strong> which you want to save as an image, simply for posterity or because you want to use in a document or presentation, this tutorial will show you how.</p> <p>For an example, we can use the example data set <code class="highlighter-rouge">faithful</code> in the <code class="highlighter-rouge">MASS</code> package to produce plot via <code class="highlighter-rouge">plot()</code>:</p> <figure class="highlight"><pre><code class="language-r" data-lang="r"><span class="n">library</span><span class="p">(</span><span class="n">MASS</span><span class="p">)</span><span class="w"> </span><span class="n">plot</span><span class="p">(</span><span class="n">faithful</span><span class="p">)</span></code></pre></figure> <p>This is just a sample dataset built into R, and the above code produces a scatter plot of the waiting time against duration of eruptions by the Old Faithful Geyser.</p> <h2 id="the-solution">The solution</h2> <p>We’ll look to save our chart to a PNG file. We do this by calling <code class="highlighter-rouge">png(file="faithful.png")</code> beforehand, where <code class="highlighter-rouge">faithful.png</code> is the file I want it to save to, and <code class="highlighter-rouge">graphics.off()</code> after.</p> <figure class="highlight"><pre><code class="language-r" data-lang="r"><span class="n">library</span><span class="p">(</span><span class="n">MASS</span><span class="p">)</span><span class="w"> </span><span class="n">png</span><span class="p">(</span><span class="n">file</span><span class="o">=</span><span class="s2">"faithful.png"</span><span class="p">)</span><span class="w"> </span><span class="n">plot</span><span class="p">(</span><span class="n">faithful</span><span class="p">)</span><span class="w"> </span><span class="n">graphics.off</span><span class="p">()</span></code></pre></figure> <p>This gives the following file:</p> <p><img src="\assets\faithful.png" alt="Plot produced" /></p> <p>That’s it - the <code class="highlighter-rouge">png()</code> function tells r to start creating the a PNG file, and <code class="highlighter-rouge">graphics.off()</code> closes and saves the file. The chart produced by the <code class="highlighter-rouge">plot()</code> command between those lines is output to the specified PNG file.</p> <p>We’ve used <code class="highlighter-rouge">plot()</code> but its worth noting that this will work identically for most other chart functions, such as <code class="highlighter-rouge">hist()</code> which produces histograms.</p> <h2 id="output-location">Output Location</h2> <p>This code saves the file to your current working folder in R. You can see what this is by typing the <code class="highlighter-rouge">getwd()</code> function in R.</p> <p>If you want the output elsewhere, you can change the working directory using the <code class="highlighter-rouge">setwd()</code> function, for example the following will set the working directory to <code class="highlighter-rouge">C:/Temp/</code> on a Windows computer (assuming that folder has already been created), before saving the chart there. Note that R requires forward slashes in the folder path, rather than backslashes:</p> <figure class="highlight"><pre><code class="language-r" data-lang="r"><span class="n">setwd</span><span class="p">(</span><span class="s2">"C:/Temp/"</span><span class="p">)</span><span class="w"> </span><span class="n">library</span><span class="p">(</span><span class="n">MASS</span><span class="p">)</span><span class="w"> </span><span class="n">png</span><span class="p">(</span><span class="n">file</span><span class="o">=</span><span class="s2">"faithful.png"</span><span class="p">)</span><span class="w"> </span><span class="n">plot</span><span class="p">(</span><span class="n">faithful</span><span class="p">)</span><span class="w"> </span><span class="n">graphics.off</span><span class="p">()</span></code></pre></figure> <p>Alternatively, you can specify the full path to where the file should be saved:</p> <figure class="highlight"><pre><code class="language-r" data-lang="r"><span class="n">library</span><span class="p">(</span><span class="n">MASS</span><span class="p">)</span><span class="w"> </span><span class="n">png</span><span class="p">(</span><span class="n">file</span><span class="o">=</span><span class="s2">"C:/Temp/faithful.png"</span><span class="p">)</span><span class="w"> </span><span class="n">plot</span><span class="p">(</span><span class="n">faithful</span><span class="p">)</span><span class="w"> </span><span class="n">graphics.off</span><span class="p">()</span></code></pre></figure> <h2 id="further-thoughts">Further thoughts</h2> <p>R supports different filetypes - the following functions exist, and work in a similar way to the <code class="highlighter-rouge">png()</code> one we used above.</p> <ul> <li><code class="highlighter-rouge">png()</code> - PNG files</li> <li><code class="highlighter-rouge">jpeg()</code> - JPEG (.jpg or .jpeg) file</li> <li><code class="highlighter-rouge">bmp()</code> - Bitmap (.bmp) files</li> <li><code class="highlighter-rouge">tiff()</code> - TIFF files</li> </ul> <p>It’s also useful sometimes to change the width and height of the chart produced, or set the background to transparent. This is done by passing arguments to the <code class="highlighter-rouge">png()</code> function, for example:</p> <figure class="highlight"><pre><code class="language-r" data-lang="r"><span class="n">library</span><span class="p">(</span><span class="n">MASS</span><span class="p">)</span><span class="w"> </span><span class="n">png</span><span class="p">(</span><span class="n">file</span><span class="o">=</span><span class="s2">"faithful.png"</span><span class="p">,</span><span class="w"> </span><span class="n">width</span><span class="o">=</span><span class="m">300</span><span class="p">,</span><span class="w"> </span><span class="n">height</span><span class="o">=</span><span class="m">250</span><span class="p">,</span><span class="w"> </span><span class="n">bg</span><span class="o">=</span><span class="s2">"transparent"</span><span class="p">)</span><span class="w"> </span><span class="n">plot</span><span class="p">(</span><span class="n">faithful</span><span class="p">)</span><span class="w"> </span><span class="n">graphics.off</span><span class="p">()</span></code></pre></figure>Steve RumfordIf you have generated a chart in R which you want to save as an image, simply for posterity or because you want to use in a document or presentation, this tutorial will show you how.Random Numbers in Excel2012-09-26T00:52:19+01:002012-09-26T00:52:19+01:00/excel/random/2012/09/26/random-numbers-in-excel<p>To generate a random number in Excel, use the <code class="highlighter-rouge">RAND()</code> function, which returns a random number between 0 and 1. It will return a different random number each time the cell is recalculated (e.g. if you re-calculate by pressing F9) or the next time you open the workbook.</p> <h2 id="random-numbers-in-a-specified-range">Random Numbers in a Specified Range</h2> <p>To get a random integer (i.e. whole number) in a particular interval, use the <code class="highlighter-rouge">RANDBETWEEN()</code> function. This has two parameters which are the lowest and highest values you want to get a random number between. For example, <code class="highlighter-rouge">=RANDBETWEEN(200,300)</code> will return a random integer between 200 and 300. Again, you’ll get a new random number each time you refresh (you can Copy and Paste Values into the same cells if you want to keep the random numbers you’ve generated).</p> <p>If you need a random number in an interval which isn’t rounded to the nearest whole number, you can use the output from the <code class="highlighter-rouge">RAND()</code> function and a bit of simple maths to get this. For example, <code class="highlighter-rouge">=200 + RAND()*(300-200)</code> will give you a random number between 200 and 300 which isn’t rounded.</p> <h2 id="example">Example</h2> <p>The output from each of these functions is shown below: <img src="/assets/excel-rand2.png" alt="Random number examples" /></p>Steve RumfordTo generate a random number in Excel, use the RAND() function, which returns a random number between 0 and 1. It will return a different random number each time the cell is recalculated (e.g. if you re-calculate by pressing F9) or the next time you open the workbook.Today’s date in Excel2012-09-20T00:52:19+01:002012-09-20T00:52:19+01:00/excel/date/2012/09/20/todays-date-in-excel<p>Working with dates is a key task in Excel. This tutorial will show you a few ways to get the current date in Excel 2003, Excel 2007 and Excel 2010.</p> <h2 id="the-now-function">The NOW() Function</h2> <p>One way to get the date is using the function <code class="highlighter-rouge">=NOW()</code>, which puts the current date and time in the cell as follows (it will show whatever the date and time is when you run it yourself):</p> <p><img src="/assets/excel-date-now.png" alt="NOW() Example" /></p> <p>You can then format the cell (right click -&gt; Format Cell, or press Ctrl+1 on a Windows PC) to display it as you like, for example as the date in the format dd/mm/yyyy.</p> <h2 id="inserting-the-date-into-formulas-or-text-fields">Inserting the date into formulas or text fields</h2> <p>Something that can be useful to insert the current date into formulas or text fields. You can do this by typing a formula in the usual way, starting with an equals sign, and using the concatenation operator, <code class="highlighter-rouge">&amp;</code>, which basically joins bits of text and numbers together. Remember to put any text in quotation marks. For example, <code class="highlighter-rouge">="Output [" &amp; 30 &amp; "]"</code> would give you a cell which says <code class="highlighter-rouge">Output </code> - not a very exciting example but you get the idea.</p> <p>On our first try, the <code class="highlighter-rouge">NOW()</code> function isn’t very useful for this, for example, the formula <code class="highlighter-rouge">="Date: " &amp; NOW()</code> gives the following:</p> <p><img src="/assets/excel-date-text.png" alt="Date in formula not working" /></p> <p>Fortunately, Excel offers another function, <code class="highlighter-rouge">TEXT()</code> which allows you to format the date in a nicer way. For example, you can use <code class="highlighter-rouge">=TEXT(NOW(), "dd/mm/yyyy")</code> to get the current date as <code class="highlighter-rouge">20/09/2019</code>. Other formats work too, such as <code class="highlighter-rouge">dd mmm yyyy</code> which would show <code class="highlighter-rouge">20 Sep 2012</code>, or just <code class="highlighter-rouge">hh:mm</code> for <code class="highlighter-rouge">10:35</code> (i.e. hours and minutes).</p> <p>In this case, we want to combine the <code class="highlighter-rouge">TEXT()</code> and <code class="highlighter-rouge">NOW()</code> functions with any other text you want to appear in the cell: <code class="highlighter-rouge">="Date: " &amp; TEXT(NOW(), "dd/mm/yyyy")</code> which gives the output shown below:</p> <p><img src="/assets/excel-date-text-better.png" alt="Date in formula" /></p> <h2 id="date-shortcuts">Date Shortcuts</h2> <p>One downside to the <code class="highlighter-rouge">NOW()</code> function is that it always displays the current date. So in the example above, if you opened the spreadsheet the following day, it would show the date as <code class="highlighter-rouge">21/09/2012</code> (the current date). This may or may not be what you want.</p> <p>One useful keyboard shortcut you can use is to press <code class="highlighter-rouge">CTRL+;</code> (hold CTRL and press the semicolon key) which inserts the current date (e.g. dd/mm/yyyy format, or your default format) into the currently selected cell. Or if you’re currently typing something into a cell, it will insert the date into what you’ve already written.</p> <p>Another shortcut is <code class="highlighter-rouge">CTRL+SHIFT+;</code> (hold both CTRL and Shift, then press the semicolon key). This inserts the current time in hh:mm format. These shortcuts “hard-code” the dates, so they won’t change the next day if you re-open the spreadsheet.</p> <h2 id="summary">Summary</h2> <p>The following table shows the various ways to insert the date, and the output which you’ll see in Excel.</p> <p><img src="/assets/excel-date-summary.png" alt="Summary of inserting date" /></p>Steve RumfordWorking with dates is a key task in Excel. This tutorial will show you a few ways to get the current date in Excel 2003, Excel 2007 and Excel 2010.Colour Cell Borders in Excel2012-09-20T00:52:19+01:002012-09-20T00:52:19+01:00/excel/2012/09/20/colour-cell-borders-in-excel<p>This is quite a simple tutorial, but it can help to make your Excel 2007 or Excel 2010 spreadsheets more attractive if used sparingly. We’ll show you the steps to colour the borders around a cell.</p> <p>First, select the cell you want to colour:</p> <p><img src="/assets/colour-borders-1.png" alt="Select the cell" /></p> <p>Right click then choose “Format cells” (or use shortcut CTRL+1):</p> <p><img src="/assets/colour-borders-2.png" alt="Choose format cells" /></p> <p>Select the border tab, then in the “colour” menu, choose the desired colour.</p> <p><img src="/assets/colour-borders-3.png" alt="Colour menu" /></p> <p>Now press the “outline” button to create a border around the outside of the selected cell (or on just some edges of it).</p> <p><img src="/assets/colour-borders-4.png" alt="Outline" /></p> <p>Press OK then marvel at the colours. Play around with setting the colours for the cell and those and the border until you get the combination you want.</p> <p><img src="/assets/colour-borders-5.png" alt="Cell with coloured borders" /></p> <p>You can vary the colour or style for individual edges of cells by following the steps above but just choose to colour a particular edge.</p>Steve RumfordThis is quite a simple tutorial, but it can help to make your Excel 2007 or Excel 2010 spreadsheets more attractive if used sparingly. We’ll show you the steps to colour the borders around a cell.Exporting from Movable Type to WordPress2012-08-29T00:52:19+01:002012-08-29T00:52:19+01:00/movable_type/wordpress/2012/08/29/export-movable-type-to-wordpress<p>Once upon a time this site was powered by Movable Type. Then I transferred everything to WordPress (now we use Jekyll). At the time, I wrote this post shaing my experiences of moving from <a href="http://www.movabletype.org/">Movable Type 4</a> to <a href="http://www.wordpress.org/">WordPress 3.4</a> blogging software.</p> <p>The reason I chose to switch was that, while MovableType produces static web pages which are quick to load on a slow webserver, the interface felt too cluttered - almost like it is trying to do too many things. To top it off, it was suggesting that I link to certain “promoted” sites which is an immediate turn off.</p> <p>I’ve exported from MovableType in the past, and encountered a few issues, but this time seemed to go much smoother. Perhaps it is partly due to having a smaller blog, or perhaps the process has improved. This article has a guide showing the steps I followed to move all my posts across to a fresh install of WordPress.</p> <h2 id="export-from-movabletype">Export from MovableType</h2> <p>The first step is to get your posts out of MovableType. To do this:</p> <p>Browse to your blog by selecting ‘Blogs’ from the System Overview menu:</p> <p><img src="/assets/mt-to-wp/1.png" alt="System overview menu" /></p> <p>Now you should be able to see a ‘Tools’ menu - from this, choose the ‘Export’ option</p> <p><img src="/assets/mt-to-wp/2.png" alt="Tools - Export" /></p> <p>Then on the Export page, click the ‘Export Blog’ button. This will start downloading a .txt file with the name of your blog so save that to your computer.</p> <p><img src="/assets/mt-to-wp/3.png" alt="Export Blog" /></p> <p>That’s you done exporting from MovableType!</p> <h2 id="import-to-wordpress">Import to WordPress</h2> <p>The next task is to import to the WordPress software. This is made easy by an Importer tool which is available to do this for you, but there are a few steps.</p> <p>From the WordPress dashboard, choose ‘Tools’ then ‘Import’</p> <p><img src="/assets/mt-to-wp/4.png" alt="Tools - Import" /></p> <p>This will then display a list of available Importers to install. You want to select the one called “Movable Type and TypePad”.</p> <p><img src="/assets/mt-to-wp/5.png" alt="Select Movable Type and TypePad" /></p> <p>It will then pop up a box giving you a little more detail on the Importer and the option to install - click “Install Now”</p> <p><img src="/assets/mt-to-wp/5.5.png" alt="Install Importer" /></p> <p>After the Importer has been installed, click “Activate Plugin &amp; Run Importer” to start it.</p> <p><img src="/assets/mt-to-wp/6.png" alt="Activate Plugin" /></p> <p>Choose the .txt file you saved when exporting from MovableType earlier, then press “Upload File and Import” to start importing.</p> <p><img src="/assets/mt-to-wp/8.png" alt="Choose text file" /></p> <p>You’ll get the option of setting up a new author to assigning posts to, or assigning to a user that’s already set up in WordPress. I chose to assign them to my WordPress admin account by deleting the “create user field” and choosing “admin”.</p> <p><img src="/assets/mt-to-wp/9.png" alt="Select author" /></p> <p>It gives you a quick status update and then tells you when it’s done. This was the end of the process for me - aside from checking the appearance of my posts was still ok.</p> <p><img src="/assets/mt-to-wp/10.png" alt="Importing complete" /></p> <p>I hope this guide helps you to export from MovableType to WordPress. As you should see from the guide, it is quite a straightforward process!</p>Steve RumfordOnce upon a time this site was powered by Movable Type. Then I transferred everything to WordPress (now we use Jekyll). At the time, I wrote this post shaing my experiences of moving from Movable Type 4 to WordPress 3.4 blogging software.Shortcut Key to Paste Values in Excel2012-08-28T00:52:19+01:002012-08-28T00:52:19+01:00/excel/2012/08/28/shortcut-key-to-paste-values-in-excel<p>If you came here looking for an easy short-cut key to paste values in Excel then it’s bad news I’m afraid - there just isn’t a simple keyboard shortcut in the same way there is to cut, copy or paste normally. However there is a way to paste values using the keyboard, which this tip shares with you.</p> <h2 id="alt-keys">Alt Keys</h2> <p><img src="/assets/altkeys.png" alt="alt keys" style="float:left; margin: 0 10px 10px 0" /> If you press Alt in Excel, you may have noticed that Excel puts letters on the ribbon, indicating the Alt key associated with that tab or function. If you press Alt, then press the letter for the tab you want (e.g. H is the home tab), you can then choose the function you want from that tab (e.g. C for copy, V for paste), and so on. Some items, including paste, have further sub-items which you can select by stringing these letters together.</p> <p>So to get to paste values this way, you can do the following0: <code class="highlighter-rouge">Alt - H - V - V</code>. Just press each one in time, no need to keep any key pressed.</p>Steve RumfordIf you came here looking for an easy short-cut key to paste values in Excel then it’s bad news I’m afraid - there just isn’t a simple keyboard shortcut in the same way there is to cut, copy or paste normally. However there is a way to paste values using the keyboard, which this tip shares with you.Password Protect Excel Spreadsheets2012-08-17T00:52:19+01:002012-08-17T00:52:19+01:00/excel/2012/08/17/password-protect-excel-spreadsheets<p>If you’re using Excel 2007 or later, the option to require a password to open an Excel spreadsheet may be in a different place than you are used to in earlier versions of Excel. Follow the steps below to require a password to protect your spreadsheet.</p> <p>Step 1: Choose File -&gt; Save As.</p> <p><img src="/assets/pw-protect/1.png" alt="File -&gt; Save As" /></p> <p>Step 2: Enter a filename, and click on ‘Tools’, just to the left of the Save button. Then select ‘General Options…’.</p> <p><img src="/assets/pw-protect/2.png" alt="Save As -&gt; Tools -&gt; General Options" /></p> <p>Step 3: In the box that opens, enter your chosen password in the top box. Note that there are two passwords here - one to open the spreadsheet and one to modify (but anyone can open). We’re setting the former - to require a password to be able to open the spreadsheet, but you can do either (or both).</p> <p><img src="/assets/pw-protect/3.png" alt="Type password in box" /></p> <p>Step 4: Excel will give you a warning about the consequences of forgetting your password and prompt you to re-enter the password to confirm it:</p> <p><img src="/assets/pw-protect/4.png" alt="Prompt to confirm password" /></p> <p>Step 5: Then choose a filename and save as normal. You will see the following when you next open it, requiring the password you entered to open:</p> <p><img src="/assets/pw-protect/5.png" alt="Password protected" /></p> <p>Note that password protecting a spreadsheet might make it difficult to open in other programs, web services (e.g. Google Documents / Gmail attachments) and mobile devices.</p>Steve RumfordIf you’re using Excel 2007 or later, the option to require a password to open an Excel spreadsheet may be in a different place than you are used to in earlier versions of Excel. Follow the steps below to require a password to protect your spreadsheet.