Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions docs/data-handling-database-R.html
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,16 @@ <h4 class="date"><em>February 3, 2017</em></h4>
<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>
<p>For Dutch-speaking Windows 7 users:</p>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<pre class="r"><code># WINDOWS USERS (uncomment when working in Windows)
#my_connection &lt;- odbcConnect(&quot;UserFlora&quot;) # uncomment for windows users

# LINUX/MAC USERS (comment out when working in Windows)
my_connection &lt;- odbcDriverConnect(&quot;Driver={ODBC Driver 13 for SQL Server};Server=inbosql04.inbo.be;Database=UserFlora;Trusted_Connection=yes;&quot;)</code></pre>
<p>Once this connection is sucessfully established, the database can be queried.</p>
<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>
<div id="get-a-complete-table-from-the-databse" class="section level2">
<h2>Get a complete table from the databse</h2>
<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>
Expand Down Expand Up @@ -321,19 +322,19 @@ <h2>Create and use query templates</h2>
<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>
<pre class="r"><code>subset_meting &lt;- sqlQuery(my_connection,
&quot;SELECT meet.Cor_X
, meet.COr_Y
, meet.Cor_Y
, meet.MetingStatusCode
, tax.NaamNederlands
, tax.NaamWetenschappelijk
, waar.IFBLHokID
FROM [dbo].[tblMeting] meet
LEFT JOIN [dbo].tblTaxon tax ON tax.ID = meet.TaxonID
LEFT JOIN [dbo].tblWaarneming waar ON waar.ID = meet.WaarnemingID
FROM [dbo].[tblMeting] AS meet
LEFT JOIN [dbo].tblTaxon AS tax ON tax.ID = meet.TaxonID
LEFT JOIN [dbo].tblWaarneming AS waar ON waar.ID = meet.WaarnemingID
WHERE meet.Cor_X IS NOT NULL
AND meet.Cor_X != 0
AND tax.NaamNederlands LIKE &#39;Wilde hyacint&#39;&quot;)
head(subset_meting)</code></pre>
<pre><code>## Cor_X COr_Y MetingStatusCode NaamNederlands
<pre><code>## Cor_X Cor_Y MetingStatusCode NaamNederlands
## 1 88720 208327 GDGA Wilde hyacint
## 2 24106 199925 GDGA Wilde hyacint
## 3 103111 190915 GDGA Wilde hyacint
Expand All @@ -357,8 +358,8 @@ <h2>Create and use query templates</h2>
, tax.NaamWetenschappelijk
, waar.IFBLHokID
FROM [dbo].[tblMeting] meet
LEFT JOIN [dbo].tblTaxon tax ON tax.ID = meet.TaxonID
LEFT JOIN [dbo].tblWaarneming waar ON waar.ID = meet.WaarnemingID
LEFT JOIN [dbo].tblTaxon AS tax ON tax.ID = meet.TaxonID
LEFT JOIN [dbo].tblWaarneming AS waar ON waar.ID = meet.WaarnemingID
WHERE meet.Cor_X IS NOT NULL
AND meet.Cor_X != 0
AND tax.NaamNederlands LIKE &#39;%s&#39;&quot;, dutch_name))
Expand Down Expand Up @@ -399,6 +400,20 @@ <h2>Create and use query templates</h2>
<p><strong>Remark:</strong> Do not forget to close your connection when done finished.</p>
<pre class="r"><code>close(my_connection)</code></pre>
</div>
<div id="sprintf" class="section level2">
<h2>The <code>sprintf</code> function</h2>
<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>
<pre class="r"><code>a_name &lt;- &#39;Jan&#39;
an_integer &lt;- 3
a_float &lt;- 2.8
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>
<pre><code>## [1] &quot;This prints a combination of a name: Jan, an integer: 3 and a float value: 2.800000&quot;</code></pre>
<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>
<pre class="r"><code>a_float &lt;- 0.0008262372
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>
<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>
<p>Similarly, this approach can be used to programmatically create template queries with adaptable input values.</p>
</div>



Expand Down
File renamed without changes
Loading