You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<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 <ahref="http://www.stata.com/support/faqs/data-management/configuring-odbc-win/"><em>registered ODBC connections</em></a> in the administrator tools menu.</p>
271
271
<p>For Dutch-speaking Windows 7 users:</p>
272
272
<pre><code>> Kies in het Configuratiescherm van Windows de optie Systeembeheer > 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: <imgsrc="/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>): <imgsrc="/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: <imgsrc="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>): <imgsrc="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>
276
276
<preclass="r"><code># WINDOWS USERS (uncomment when working in Windows)
277
277
#my_connection <- odbcConnect("UserFlora") # uncomment for windows users
278
278
279
279
# LINUX/MAC USERS (comment out when working in Windows)
280
280
my_connection <- odbcDriverConnect("Driver={ODBC Driver 13 for SQL Server};Server=inbosql04.inbo.be;Database=UserFlora;Trusted_Connection=yes;")</code></pre>
281
281
<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>
<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>
321
322
<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>
<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
+
<preclass="r"><code>a_name <- 'Jan'
407
+
an_integer <- 3
408
+
a_float <- 2.8
409
+
sprintf('This prints a combination of a name: %s, an integer: %i and a float value: %f', a_name, an_integer, a_float)</code></pre>
410
+
<pre><code>## [1] "This prints a combination of a name: Jan, an integer: 3 and a float value: 2.800000"</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
+
<preclass="r"><code>a_float <- 0.0008262372
413
+
sprintf('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.', a_float, a_float, a_float, a_float)</code></pre>
414
+
<pre><code>## [1] "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."</code></pre>
415
+
<p>Similarly, this approach can be used to programmatically create template queries with adaptable input values.</p>
0 commit comments