Description Usage Arguments Value Author(s) See Also Examples
Read all or part of a table in a MySQL 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 “ delimiters 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 | library( "easyrodbcmysql" )
testMySQL <- FALSE # Set to TRUE to run some tests
# after modifying the edb() part below
# with your own database setting
### Windows only:
if( (Sys.info()[[ "sysname" ]] == "Windows") & testMySQL ){
### Describe the database (NB: this is not a connection)
myDb <- edb(
dbType = "RODBC_MySQL",
dbSourceName = "nameOfODBCSource", # or any name you like
dbName = "nameOfDatabase",
dbLogin = "yourUserName",
dbPwd = "yourPassword",
dbHost = "127.0.0.1",
dbPort = 3306
) #
### Register the data source in ODBC
edbDataSource( myDb, verbose = TRUE )
### 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" ]
### Un-register the data source in ODBC (windows only)
edbDataSource( myDb, trash = TRUE )
} #
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.