Description Usage Arguments Value Author(s) See Also Examples
Read all or part of a table in a MS Access database (referenced by 'edb').
1 2 3 4 5 6 7 8 9 10 11 |
edb |
An object of class 'edb', such as returned by |
tableName |
Single character string. Name of the table to read in 'edb'. |
sRow |
A list of named items. List of contrains/criterion to be applied on
apply. The (vector of) value(s) of each items are the possible values that can be retrieved. Values can be character or numeric. If NULL (the default), all values are returned. |
sCol |
Either (1) a vector of character strings with the name of the columns to retrieve or (2) a vector of logical of the same length as the number of columns or (3) a vector of indexes / integers giving the indexes of the column to retrieve. If negative, then it indicates the indexes of the column to leave out. |
sRowOp |
A single character string. Operator to be used to combine multiple constrains in sRow. Possible values are "OR" or "AND". Default value is "AND". |
formatCol |
If not NULL, a named list of functions to be applied to certain columns after the data has been extracted from the database. The name of each list item gives the column to process, and the value of each item gives the function that must be applied. For instance formatCol = list("DATE"=as.Date) will apply the function as.Date to the column "DATE". |
distinct |
Single logical. If TRUE, unique values in the result table will be returned, using the This is equivalent to applying data.frame returned by the function, except that the action is performed inside the database (not in R). |
orderBy |
Vector of character strings, or NULL (the default). If non NULL, vector of column names that must be used to sort the result table. Column names may be followed by a space and 'DESC' if the column must be sorted in a descending order ('ASC', ascending, is the default). This operation is performed in the database with SQL ORDER BY statement and is equivalent to ordering the data in R with column names between square brackets [] if they contain spaces. |
testFiles |
Single logical. Should the function test for the presence (file.exist()) of the needed files in the folder before trying to fetch information from the database? |
verbose |
Single logical. If TRUE, information on what is done are output on screen. |
... |
Additional parameters to be passed to |
The function returns the requested table.
Julien MOEYS <Julien.Moeys@mark.slu.se>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 | library( "easyrodbcaccess" )
# System check. Ingnore this or set 'testODBC' to TRUE
testODBC <- (class(try(odbcDataSources())) != "try-error") & # Check (R)ODBC
(Sys.info()[[ "sysname" ]] == "Windows") & # Only MS Windows
(.Machine$"sizeof.pointer" == 4) # Only 32-bits
### Windows only:
if( testODBC ){
### Make a copy of MS Access example database:
# (a database of soil profile description)
file.copy(
from = system.file( "soils.mdb", package = "easyrodbcaccess" ),
to = "soils.mdb"
)
### Describe the database (NB: this is not a connection)
myDb <- edb( dbType = "RODBC_Access", dbName = "soils.mdb" )
### Use the database:
## Read data in a table in the database
# Retrieve a table (data.frame style subsetting):
myDb[ "WRB_SOIL_GROUP" ]
# Same operation, but with edbRead()
edbRead( edb = myDb, tableName = "WRB_SOIL_GROUP" )
# Retrieve part of a table (with row constrains)
myDb[ "WRB_SOIL_GROUP", list("ABBREV" = c("AC","CR","PL")) ]
# Same operation, but with edbRead()
edbRead(
edb = myDb,
tableName = "WRB_SOIL_GROUP",
sRow = list("ABBREV" = c("AC","CR","PL"))
)
# Retrieve part of a table (row constrains + select only some columns)
myDb[
"WRB_SOIL_GROUP",
list("ABBREV" = c("AC","CR","PL")),
c("ID_WRB_SOIL_GROUP","NAME")
]
# Same operation, but with edbRead()
edbRead(
edb = myDb,
tableName = "WRB_SOIL_GROUP",
sRow = list("ABBREV" = c("AC","CR","PL")),
sCol = c("ID_WRB_SOIL_GROUP","NAME")
)
# Use some SQL constrains
myDb[
"WRB_SOIL_GROUP",
list(
"ABBREV" = c("AC","AB","AL","AN","AT"),
"SQL" = "NAME LIKE 'Al%'"
)
]
# NB: the different elements in the list are separated by
# "AND" statement
# Multiple row constrains, alternative (constrain 1 OR constrin 2)
myDb[
"WRB_SOIL_GROUP",
list(
"ABBREV" = c("AC","AB"),
"ID_WRB_SOIL_GROUP" = 25:30
),
sRowOp = "OR"
]
# Check the dimension of a table
# - Number of columns:
edbNCol( edb = myDb, tableName = "WRB_SOIL_GROUP" )
# - Number of rows:
edbNRow( edb = myDb, tableName = "WRB_SOIL_GROUP" )
# - Number of rows, with constrains:
edbNRow( edb = myDb, tableName = "WRB_SOIL_GROUP",
sRow = list("ABBREV" = c("AC","AB")) )
# - Dimensions:
edbDim( edb = myDb, tableName = "WRB_SOIL_GROUP" )
# More ways to select columns:
# - Indexes
myDb[ "WRB_SOIL_GROUP", sCol = 2:3 ]
# - Logicals
myDb[ "WRB_SOIL_GROUP", sCol = c(FALSE,TRUE,TRUE) ]
# It may be useful to transform some columns 'on-the-fly', after
# they have been read from the database. In the example below we
# have some dates and times values, as well as some boolean stored
# as integers (seconds or days since 1970-01-01 or 0/1 values,
# respectively). We want to transform them into dates or boolean.
myDb[ "MISCFORMAT" ]
# NB: although Yes/No format, the last column is read as integer too...
# So date variables stored as integers have to be converted.
# The code below show how to do that.
# Function to convert POSIX integer "seconds from 1970-01-01" into
# R POSIXct date format.
formatDT <- function( x, tz = "GMT" ){
res <- ISOdatetime( year = 1970, month = 1, day = 1,
hour = 0, min = 0, sec = 0, tz = tz )
res <- res + x
return( res ) }
# Function to convert integer "days from 1970-01-01" into
# R Date format.
formatD <- function( x, tz = "GMT" ){
res <- ISOdate( year = 1970, month = 1, day = 1, tz = tz )
res <- res + (x * 24 * 60 * 60 )
res <- as.Date( res )
return( res ) }
# Now we can convert the columns on-the-fly
myDb[ "MISCFORMAT", formatCol = list( "DAT_TIM_SEC" = formatDT,
"DAT_DAY" = formatD, "TEST_BOOL" = as.logical ) ]
# SQL SELECT DISTINCT, equivalent of R unique().
# To select unique / distinct values in a (group of) columns,
# set 'distinct' to TRUE:
myDb[ "PROFILE", sCol = "COMMENTS", distinct = TRUE ]
# SQL ORDER BY, equivalent of R order().
# To sort the table according to some columns (ascending or
# descending), use 'orderBy':
myDb[ "WRB_SOIL_GROUP", orderBy = "ABBREV DESC" ]
### Clean-up
file.remove( "soils.mdb" )
### Access 2007 ---------------------------------------------
file.copy(
from = system.file( "soils.accdb", package = "easyrodbcaccess" ),
to = "soils.accdb"
)
### Describe the database (NB: this is not a connection)
myDb2 <- edb( dbType = "RODBC_Access", dbName = "soils.accdb",
accessVersion = 2007 )
### Use the database:
## Read data in a table in the database
# Retrieve a table (data.frame style subsetting):
myDb2[ "WRB_SOIL_GROUP" ]
# Same operation, but with edbRead()
edbRead( edb = myDb2, tableName = "WRB_SOIL_GROUP" )
# Retrieve part of a table (with row constrains)
myDb2[ "WRB_SOIL_GROUP", list("ABBREV" = c("AC","CR","PL")) ]
# Same operation, but with edbRead()
edbRead(
edb = myDb2,
tableName = "WRB_SOIL_GROUP",
sRow = list("ABBREV" = c("AC","CR","PL"))
)
# Retrieve part of a table (row constrains + select only some columns)
myDb2[
"WRB_SOIL_GROUP",
list("ABBREV" = c("AC","CR","PL")),
c("ID_WRB_SOIL_GROUP","NAME")
]
# Same operation, but with edbRead()
edbRead(
edb = myDb2,
tableName = "WRB_SOIL_GROUP",
sRow = list("ABBREV" = c("AC","CR","PL")),
sCol = c("ID_WRB_SOIL_GROUP","NAME")
)
# Use some SQL constrains
myDb2[
"WRB_SOIL_GROUP",
list(
"ABBREV" = c("AC","AB","AL","AN","AT"),
"SQL" = "NAME LIKE 'Al%'"
)
]
# NB: the different elements in the list are separated by
# "AND" statement
# Multiple row constrains, alternative (constrain 1 OR constrin 2)
myDb2[
"WRB_SOIL_GROUP",
list(
"ABBREV" = c("AC","AB"),
"ID_WRB_SOIL_GROUP" = 25:30
),
sRowOp = "OR"
]
# Check the dimension of a table
# - Number of columns:
edbNCol( edb = myDb2, tableName = "WRB_SOIL_GROUP" )
# - Number of rows:
edbNCol( edb = myDb2, tableName = "WRB_SOIL_GROUP" )
# - Dimensions:
edbDim( edb = myDb2, tableName = "WRB_SOIL_GROUP" )
# More ways to select columns:
# - Indexes
myDb2[ "WRB_SOIL_GROUP", sCol = 2:3 ]
# - Logicals
myDb2[ "WRB_SOIL_GROUP", sCol = c(FALSE,TRUE,TRUE) ]
# It may be useful to transform some columns 'on-the-fly', after
# they have been read from the database. In the example below we
# have some dates and times values, as well as some boolean stored
# as integers (seconds or days since 1970-01-01 or 0/1 values,
# respectively). We want to transform them into dates or boolean.
myDb2[ "MISCFORMAT" ]
# NB: although Yes/No format, the last column is read as integer too...
# So date variables stored as integers have to be converted.
# The code below show how to do that.
# Function to convert POSIX integer "seconds from 1970-01-01" into
# R POSIXct date format.
formatDT <- function( x, tz = "GMT" ){
res <- ISOdatetime( year = 1970, month = 1, day = 1,
hour = 0, min = 0, sec = 0, tz = tz )
res <- res + x
return( res ) }
# Function to convert integer "days from 1970-01-01" into
# R Date format.
formatD <- function( x, tz = "GMT" ){
res <- ISOdate( year = 1970, month = 1, day = 1, tz = tz )
res <- res + (x * 24 * 60 * 60 )
res <- as.Date( res )
return( res ) }
# Now we can convert the columns on-the-fly
myDb2[ "MISCFORMAT", formatCol = list( "DAT_TIM_SEC" = formatDT,
"DAT_DAY" = formatD, "TEST_BOOL" = as.logical ) ]
# SQL SELECT DISTINCT, equivalent of R unique().
# To select unique / distinct values in a (group of) columns,
# set 'distinct' to TRUE:
myDb2[ "PROFILE", sCol = "COMMENTS", distinct = TRUE ]
# SQL ORDER BY, equivalent of R order().
# To sort the table according to some columns (ascending or
# descending), use 'orderBy':
myDb2[ "WRB_SOIL_GROUP", orderBy = "ABBREV DESC" ]
### Clean-up
file.remove( "soils.accdb" )
} #
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.