Advertisement
Guest User

Untitled

a guest
Jun 9th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. With this hook, one can write SQL statements in chunks
  2.  
  3. These statements will be transmitted to a Sybase server using the jTDS driver
  4.  
  5. Results are printed in html
  6.  
  7. To be used in an .Rmd file to generate a html_document
  8.  
  9. ```{r setup, include=FALSE}
  10. SybaseGetQuery <- function(requete.simple, db) {
  11. require(RJDBC)
  12. require(htmltools)
  13. statement <- paste(unlist(requete.simple), collapse="\n")
  14. conn_string <- sprintf("jdbc:jtds:sybase://%s:%s/%s;user=%s;password=%s",
  15. server_sybase, port, db, user, password)
  16. drv <- JDBC("net.sourceforge.jtds.jdbc.Driver", loc_jtds, "`")
  17. conn <- dbConnect(drv, conn_string)
  18. tryCatch(resultat <- dbGetQuery(conn, statement = statement),
  19. finally = dbDisconnect(conn))
  20. return(resultat)
  21. }
  22.  
  23. knitr::opts_hooks$set(sybase = function(options) {
  24. if (!is.null(options$sybase)) {
  25. options$eval <- FALSE
  26. options$echo <- TRUE
  27. options$results <- 'asis'
  28. options$`.class` <- "sql"
  29. }
  30. options
  31. })
  32.  
  33. knitr::knit_hooks$set(sybase = function(before, options, envir) {
  34. if (before) {
  35. }
  36. else {
  37. paste0(
  38. # Print the query result in an html format:
  39. knitr::kable(
  40. SybaseGetQuery(options["code"], options$sybase),
  41. format = "html",
  42. table.attr = 'class="responsive-table highlight"', # Class for materialize.css
  43. row.names = TRUE,
  44. format.args = list(big.mark = " ", decimal.mark = ",", big.interval = 3L)), # European format
  45. collapse = NULL
  46. )}
  47. })
  48.  
  49. loc_jtds <- "loc_jtds"
  50. server_sybase <- "server_name"
  51. port <- "a_port_number"
  52. user <- "username"
  53. password <- "password"
  54.  
  55. knitr::opts_chunk$set(sybase="db_name")
  56. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement