You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.0KB

  1. .HTML.file <- NULL
  2. .tabs <- 0
  3. .ligne <- F
  4. #' Initialiser un fichier HTML
  5. #'
  6. #' Cree et remplit les headers pour un fichier hTML
  7. #'
  8. #' Si aucun nom de fichier n'est fourni, cree un fichier temporaire dans le repertoire temporaire
  9. #' Le nom du fichier actuel est stocke dans .HTML.file
  10. #' @encoding UTF-8
  11. #' @param file Nom du fichier HTML a creer, par defaut un fichier temporaire
  12. #' @param title Titre de la page
  13. #' @param CSSfile Fichier CSS a utiliser
  14. #' @examples
  15. #' \dontrun{
  16. #' HTMLInit(file="sortie.html", title="Titre de la page", CSSfile="desc.css")
  17. #' }
  18. #' @export
  19. HTMLInit <- function(file=tempfile(pattern="report", fileext=".html"), title="", CSSfile="")
  20. {
  21. file.copy(from=file.path(path.package("cosmosR"),"desc.css"), to=file.path(dirname(file),"desc.css"),overwrite=T)
  22. file.copy(from=file.path(path.package("cosmosR"),"diag.css"), to=file.path(dirname(file),"diag.css"),overwrite=T)
  23. file.copy(from=file.path(path.package("cosmosR"),"cosmosR.js"), to=file.path(dirname(file),"cosmosR.js"),overwrite=T)
  24. unlockBinding(".HTML.file", env=asNamespace("cosmosR"))
  25. unlockBinding(".tabs", env=asNamespace("cosmosR"))
  26. unlockBinding(".ligne", env=asNamespace("cosmosR"))
  27. .HTML.file <<- file
  28. .tabs <<- 0
  29. .ligne <<- F
  30. HTML("<!DOCTYPE html>", append=F)
  31. HTML("<HTML>")
  32. inc()
  33. HTML("<HEAD>")
  34. inc()
  35. HTML("<meta charset='", localeToCharset()[1], "' />")
  36. HTML("<title>", title, "</title>")
  37. HTML("<link rel='stylesheet' href='", CSSfile, "' />")
  38. HTML("<script src='cosmosR.js'></script>")
  39. dec()
  40. HTML("</HEAD>")
  41. HTML("<BODY>")
  42. inc()
  43. }
  44. #' Termine et clos le fichier HTML
  45. #'
  46. #' Ecrit le footer du fichier hTML
  47. #'
  48. #' Ecrit le footer du fichier initialise par HTMLInit, ouvre le fichier dans le navigateur et supprime l'acces.
  49. #' @encoding UTF-8
  50. #' @export
  51. HTMLEnd <- function()
  52. {
  53. if (is.null(.HTML.file)) return
  54. unlockBinding(".HTML.file", env=asNamespace("cosmosR"))
  55. dec()
  56. HTML("</BODY>")
  57. dec()
  58. HTML("</HTML>")
  59. browseURL(.HTML.file)
  60. .HTML.file <<- NULL
  61. }
  62. #' Ecrit dans le fichier HTML
  63. #'
  64. #' Ecrit dans le fichier HTML cree par HTMLInit
  65. #'
  66. #' Ecrit dans le fichier initialise par HTMLInit dont le nom est contenu dans .HTML.file
  67. #' @encoding UTF-8
  68. #' @param x Contenu a ecrire
  69. #' @param ... Contenu concatene sans espace a x
  70. #' @param append Decide si x... doit etre ajoute a un fichier existant
  71. #' @param sep Separateur de fin de ligne, modifier pour ecrire sur la meme ligne du fichier
  72. #' @export
  73. HTML <- function(x, ..., append=T,sep="\n")
  74. {
  75. if (is.null(.HTML.file)) return
  76. unlockBinding(".ligne", env=asNamespace("cosmosR"))
  77. x <- paste0(x,...,collapse="")
  78. if (.ligne)
  79. tabs <- ""
  80. else
  81. tabs <- paste0(rep("\t", .tabs),collapse="")
  82. if (sep == "\n")
  83. .ligne <<- F
  84. else
  85. .ligne <<-T
  86. x <- paste0(tabs, x)
  87. cat(x,file=.HTML.file,sep=sep, append=append)
  88. }
  89. inc <- function()
  90. {
  91. unlockBinding(".tabs", env=asNamespace("cosmosR"))
  92. .tabs <<- .tabs+1
  93. }
  94. dec <- function()
  95. {
  96. unlockBinding(".tabs", env=asNamespace("cosmosR"))
  97. if (.tabs>0) .tabs <<- .tabs-1
  98. }