Skip to content

Commit 87b3cf4

Browse files
authored
Merge pull request #18 from inbo/imghandling
Imghandling
2 parents b2b3c48 + eceff12 commit 87b3cf4

9 files changed

Lines changed: 65 additions & 17578 deletions

File tree

docs/data-handling-database-R.html

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,16 @@ <h4 class="date"><em>February 3, 2017</em></h4>
270270
<p>For Windows users, the most important element is to know the so-called <code>DSN</code> (i.e. a registered Data Source Name). Actually, it is just the name of the database as it is known by your computer (and MS Access). The easiest way to check the <code>DSN</code> is to check the <a href="http://www.stata.com/support/faqs/data-management/configuring-odbc-win/"><em>registered ODBC connections</em></a> in the administrator tools menu.</p>
271271
<p>For Dutch-speaking Windows 7 users:</p>
272272
<pre><code>&gt; Kies in het Configuratiescherm van Windows de optie Systeembeheer &gt; Gegevensbronnen (ODBC). De optie Systeembeheer verschijnt in de categorie Systeem en onderhoud.</code></pre>
273-
<p>You should see a list similar to the list underneath, with the names of the available DSN names enlisted: <img src="/home/stijn_vanhoey/githubs/inbo_tutorials/img/odbc_gegevensbron.png" alt="odbc-connecties" /></p>
274-
<p>An alternative way to check the DSN name of a database already working on with Access, is to check the DSN inside MS Access (in dutch, check menu item <em>Koppelingsbeheer</em>): <img src="/home/stijn_vanhoey/githubs/inbo_tutorials/img/access_dsn.png" alt="access-dsn" /></p>
275-
<p>For example, the DSN name <code>UserFlora</code> or <code>Cydonia-prd</code> can be used to query these databases and extract data from it with similar queries to the one used in MSAccess. First of all, the connection with the database need to be established, by using the <code>odbcConnect</code> function, providing the DSN name as argument:</p>
273+
<p>You should see a list similar to the list underneath, with the names of the available DSN names enlisted: <img src="data-handling-database-R/odbc_gegevensbron.png" alt="odbc-connecties" /></p>
274+
<p>An alternative way to check the DSN name of a database already working on with Access, is to check the DSN inside MS Access (in dutch, check menu item <em>Koppelingsbeheer</em>): <img src="data-handling-database-R/access_dsn.png" alt="access-dsn" /></p>
275+
<p>For example, the DSN name <code>UserFlora</code> or <code>Cydonia-prd</code> can be used to query these databases and extract data from it with similar queries to the one used in MSAccess. First of all, the connection with the database need to be established, by using the <code>odbcConnect</code> function, providing the DSN name as argument (Notice, you have to change the comments when running the tutorial in windows):</p>
276276
<pre class="r"><code># WINDOWS USERS (uncomment when working in Windows)
277277
#my_connection &lt;- odbcConnect(&quot;UserFlora&quot;) # uncomment for windows users
278278

279279
# LINUX/MAC USERS (comment out when working in Windows)
280280
my_connection &lt;- odbcDriverConnect(&quot;Driver={ODBC Driver 13 for SQL Server};Server=inbosql04.inbo.be;Database=UserFlora;Trusted_Connection=yes;&quot;)</code></pre>
281281
<p>Once this connection is sucessfully established, the database can be queried.</p>
282+
<p><strong>Remark for linux users:</strong> When working in Linux, this setup requires an active <em>kerberos</em> session. More information about the setup and functionality will be provided in an upcoming tutorial.</p>
282283
<div id="get-a-complete-table-from-the-databse" class="section level2">
283284
<h2>Get a complete table from the databse</h2>
284285
<p>The function <code>sqlFetch</code> can be used to load an entire table from a database. For example, to extract the <code>tblTaxon</code> table from the flora database:</p>
@@ -321,19 +322,19 @@ <h2>Create and use query templates</h2>
321322
<p>Consider the execution of the following query. We are interested in those records with valid X and Y coordinates for the measurement, based on a given dutch name:</p>
322323
<pre class="r"><code>subset_meting &lt;- sqlQuery(my_connection,
323324
&quot;SELECT meet.Cor_X
324-
, meet.COr_Y
325+
, meet.Cor_Y
325326
, meet.MetingStatusCode
326327
, tax.NaamNederlands
327328
, tax.NaamWetenschappelijk
328329
, waar.IFBLHokID
329-
FROM [dbo].[tblMeting] meet
330-
LEFT JOIN [dbo].tblTaxon tax ON tax.ID = meet.TaxonID
331-
LEFT JOIN [dbo].tblWaarneming waar ON waar.ID = meet.WaarnemingID
330+
FROM [dbo].[tblMeting] AS meet
331+
LEFT JOIN [dbo].tblTaxon AS tax ON tax.ID = meet.TaxonID
332+
LEFT JOIN [dbo].tblWaarneming AS waar ON waar.ID = meet.WaarnemingID
332333
WHERE meet.Cor_X IS NOT NULL
333334
AND meet.Cor_X != 0
334335
AND tax.NaamNederlands LIKE &#39;Wilde hyacint&#39;&quot;)
335336
head(subset_meting)</code></pre>
336-
<pre><code>## Cor_X COr_Y MetingStatusCode NaamNederlands
337+
<pre><code>## Cor_X Cor_Y MetingStatusCode NaamNederlands
337338
## 1 88720 208327 GDGA Wilde hyacint
338339
## 2 24106 199925 GDGA Wilde hyacint
339340
## 3 103111 190915 GDGA Wilde hyacint
@@ -357,8 +358,8 @@ <h2>Create and use query templates</h2>
357358
, tax.NaamWetenschappelijk
358359
, waar.IFBLHokID
359360
FROM [dbo].[tblMeting] meet
360-
LEFT JOIN [dbo].tblTaxon tax ON tax.ID = meet.TaxonID
361-
LEFT JOIN [dbo].tblWaarneming waar ON waar.ID = meet.WaarnemingID
361+
LEFT JOIN [dbo].tblTaxon AS tax ON tax.ID = meet.TaxonID
362+
LEFT JOIN [dbo].tblWaarneming AS waar ON waar.ID = meet.WaarnemingID
362363
WHERE meet.Cor_X IS NOT NULL
363364
AND meet.Cor_X != 0
364365
AND tax.NaamNederlands LIKE &#39;%s&#39;&quot;, dutch_name))
@@ -399,6 +400,20 @@ <h2>Create and use query templates</h2>
399400
<p><strong>Remark:</strong> Do not forget to close your connection when done finished.</p>
400401
<pre class="r"><code>close(my_connection)</code></pre>
401402
</div>
403+
<div id="sprintf" class="section level2">
404+
<h2>The <code>sprintf</code> function</h2>
405+
<p>In order to accomplish the re-usage of a query for different input names (<code>dutch_name</code>), the <code>sprintf</code> function is used. The <code>sprintf</code> function provides the ability to combine text and variable values in a single charactor string (i.e. the query to execute). For each variable name required in the query (any part of your query you want to have interchangeable), a representation in the query is given by a <code>%</code> in combination with the data type you want to represent. The latter is an agreed format conversion, for example <code>%s</code> is a character string (e.g. the example of <code>dutch_name</code>) and <code>%i</code> is an integer value (e.g. <code>2</code>). More information about the different symbols is given in the documentation of <code>sprintf</code> (type <code>?sprintf</code> in the console).</p>
406+
<pre class="r"><code>a_name &lt;- &#39;Jan&#39;
407+
an_integer &lt;- 3
408+
a_float &lt;- 2.8
409+
sprintf(&#39;This prints a combination of a name: %s, an integer: %i and a float value: %f&#39;, a_name, an_integer, a_float)</code></pre>
410+
<pre><code>## [1] &quot;This prints a combination of a name: Jan, an integer: 3 and a float value: 2.800000&quot;</code></pre>
411+
<p>For float values, you can further define the fixed number of decimals (<code>%f</code>) or a fixed number of significant digits with <code>%g</code></p>
412+
<pre class="r"><code>a_float &lt;- 0.0008262372
413+
sprintf(&#39;This prints the same value with five: %.5f or two: %.2f decimal numbers, or a define a number of significant digits: %.5g or %.2g which is sometimes more useful.&#39;, a_float, a_float, a_float, a_float)</code></pre>
414+
<pre><code>## [1] &quot;This prints the same value with five: 0.00083 or two: 0.00 decimal numbers, or a define a number of significant digits: 0.00082624 or 0.00083 which is sometimes more useful.&quot;</code></pre>
415+
<p>Similarly, this approach can be used to programmatically create template queries with adaptable input values.</p>
416+
</div>
402417

403418

404419

img/access_dsn.png renamed to docs/data-handling-database-R/access_dsn.png

File renamed without changes.

img/odbc_gegevensbron.png renamed to docs/data-handling-database-R/odbc_gegevensbron.png

File renamed without changes.

0 commit comments

Comments
 (0)