Skip to content

Commit

Permalink
appendex A running
Browse files Browse the repository at this point in the history
TODO: close DB connection in appendix A example
  • Loading branch information
JohnMount committed Dec 11, 2015
1 parent ed143c2 commit 9aa4de5
Show file tree
Hide file tree
Showing 10 changed files with 873 additions and 11 deletions.
Binary file modified CodeExamples.zip
Binary file not shown.
@@ -1,7 +1,7 @@
# informalexample A.1 of section A.1.5
# (informalexample A.1 of section A.1.5) : Working with R and other tools : Installing the tools : R resources

install.packages('ctv')
install.packages('ctv',repos='https://cran.r-project.org')
library('ctv')
install.views('TimeSeries')
# install.views('TimeSeries') # can take a LONG time

Expand Up @@ -5,5 +5,5 @@ x <- 2
x < - 3
## [1] FALSE
print(x)
[1] 2
## [1] 2

Expand Up @@ -4,11 +4,11 @@

require(RCurl) # Note: 1
urlBase <-
'https://raw.github.com/WinVector/zmPDSwR/master/' # Note: 2
'https://raw.githubusercontent.com/WinVector/zmPDSwR/master/' # Note: 2
mkCon <- function(nm) { # Note: 3
textConnection(getURL(paste(urlBase,nm,sep='')))
textConnection(getURL(paste(urlBase,nm,sep='/')))
}
cars <- read.table(mkCon('UCICar/car.data.csv'), # Note: 4
cars <- read.table(mkCon('car.data.csv'), # Note: 4
sep=',',header=T,comment.char='')

# Note 1:
Expand Down
Expand Up @@ -2,11 +2,10 @@
# (example A.10 of section A.3.2) : Working with R and other tools : Using databases with R : Starting with SQuirreL SQL
# Title: Reading database data into R

install.packages('RJDBC') # Note: 1
install.packages('RJDBC',repos='https://cran.r-project.org') # Note: 1
library('RJDBC') # Note: 2
drv <- JDBC("org.h2.Driver","h2-1.3.170.jar",identifier.quote="'") # Note: 3
setwd('/Users/johnmount/Downloads')
conn <- dbConnect(drv,"jdbc:h2://h2demodb_h2","u","u") # Note: 4
conn <- dbConnect(drv,"jdbc:h2:h2demodb_h2","u","u") # Note: 4
d <- dbGetQuery(conn,"SELECT * FROM example_table") # Note: 5
print(d) # Note: 6
## STATUSID NAME
Expand Down
2 changes: 1 addition & 1 deletion RunExamples/rXA.Rmd
Expand Up @@ -11,5 +11,5 @@ source('runDir.R')

```{r xA1, tidy=FALSE,comment='',prompt=FALSE}
runDir('../CodeExamples/x0A_Working_with_R_and_other_tools',
'.')
'../SQLExample')
```
827 changes: 827 additions & 0 deletions RunExamples/rXA.html

Large diffs are not rendered by default.

@@ -0,0 +1,7 @@
# informalexample A.4 of section A.2.1
# (informalexample A.4 of section A.2.1) : Working with R and other tools : Starting with R : Primary features of R

tryCatch(add(1,'fred'),
error=function(x) print(x))
## Error in a + b : non-numeric argument to binary operator

29 changes: 29 additions & 0 deletions RunExamples/replacements/00244_example_A.14_of_section_A.3.4.R
@@ -0,0 +1,29 @@
# example A.14 of section A.3.4
# (example A.14 of section A.3.4) : Working with R and other tools : Using databases with R : An example SQL data transformation task
# Title: Assembling many rows using SQL

options(gsubfn.engine = "R")
library('sqldf')
joined <- sqldf('
select
bCurrent.date as StayDate,
bCurrent.daysBefore as daysBefore,
bCurrent.nDaysBefore as nDaysBefore,
p.price as price,
bCurrent.bookings as bookingsCurrent,
bPrevious.bookings as bookingsPrevious,
bCurrent.bookings - bPrevious.bookings as pickup
from
bthin bCurrent
join
bthin bPrevious
on
bCurrent.date=bPrevious.date
and bCurrent.nDaysBefore+1=bPrevious.nDaysBefore
join
pthin p
on
bCurrent.date=p.date
and bCurrent.nDaysBefore=p.nDaysBefore
')
print(joined)
2 changes: 1 addition & 1 deletion RunExamples/runDir.R
Expand Up @@ -3,7 +3,7 @@
#'
#' runDir changes directory to datadir, runs the R examples number between
#' first and last (inclusive) found in sourcedir.
#' @parm sourcedir character path to take R examples from
#' @parm sourcedir character path to take R examples from PATH RELATIVE to datadir
#' @param datadir character path to run in (and take data from)
#' @param first integer optional first numbered example eligible to run
#' @param last integer optional last numbered example eligible to run
Expand Down

0 comments on commit 9aa4de5

Please sign in to comment.