Debian Codegraph @

R/nozzle.R

raw
1 # Nozzle: a Report Generation Toolkit for Data Analysis Pipelines
2 #
3 # Nils Gehlenborg ([email protected])
4 # Harvard Medical School, Center for Biomedical Informatics
5 # Broad Institute, Cancer Program
6 #
7 # Copyright (c) 2011-2013. All rights reserved.
8 #
9 # TODO: add default state (open/closed) for sections (per default all will be closed in custom reports)
10
11
12 #' Provides a high-level API to generate HTML reports with dynamic user interface elements.
13 #'
14 #' \tabular{ll}{
15 #' Package: \tab Nozzle.R1\cr
16 #' Type: \tab Package\cr
17 #' Version: \tab 1.1-1\cr
18 #' Date: \tab 2013-05-15\cr
19 #' License: \tab LGPL (>= 2)\cr
20 #' LazyLoad: \tab yes\cr
21 #' }
22 #'
23 #' Nozzle was designed to facilitate summarization and rapid browsing of complex results in data
24 #' analysis pipelines where multiple analyses are performed frequently on big data sets.
25 #'
26 #' @name Nozzle.R1-package
27 #' @aliases nozzle
28 #' @docType package
29 #' @title Nozzle: a Report Generation Toolkit for Data Analysis Pipelines
30 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
31 #' @references
32 #' \url{http://www.github.com/parklab/Nozzle},
33 #' \url{http://gdac.broadinstitute.org/nozzle}
34 #' @note The "R1" in the "Nozzle.R1" package name stands for "revision 1" of the Nozzle R API. All versions of the Nozzle.R1 package
35 #' will be backwards-compatible and able to render reports generated with earlier versions of the package.
36 #' When backwards-compatibility of the API can no longer maintained the package name will change to "Nozzle.R2".
37 #' @keywords package
38 NULL
39
40
41 # --- global variables ----------------------------------------------------------------------------
42
43 .nozzleEnv <- new.env();
44
45 .PACKAGE.VERSION <- "1.1-0";
46
47 .ELEMENT.REPORT <- "_report_";
48 .ELEMENT.SECTION <- "_section_";
49 .ELEMENT.SUBSECTION <- "_subsection_";
50 .ELEMENT.SUBSUBSECTION <- "_subsubsection_";
51 .ELEMENT.HTML <- "_html_";
52 .ELEMENT.PARAGRAPH <- "_paragraph_";
53 .ELEMENT.LIST <- "_list_";
54 .ELEMENT.PARAMETERLIST <- "_list_";
55 .ELEMENT.TABLE <- "_table_";
56 .ELEMENT.FIGURE <- "_figure_";
57 .ELEMENT.CITATION <- "_citation_";
58 .ELEMENT.RESULT <- "_result_";
59
60 .SEVERITY.LOG <- 0;
61 .SEVERITY.WARNING <- 1;
62 .SEVERITY.ERROR <- 2;
63
64 # names for popout windows
65 .POPOUT.FIGURE.WINDOW <- "nozzle_popout";
66 .POPOUT.TABLE.WINDOW <- "nozzle_popout";
67
68 #' Default filename for reports.
69 #' @export
70 DEFAULT.REPORT.FILENAME <- "nozzle";
71
72 #' Name of entities that are labeled as signficiant.
73 #' @export
74 DEFAULT.SIGNIFICANT.ENTITY <- "statistically significant findings";
75
76
77 # location relative to inst diretory of package
78 .NOZZLE.JAVASCRIPT.PATH <- "js";
79 .NOZZLE.JAVASCRIPT.FILE <- "nozzle.js";
80 .NOZZLE.JAVASCRIPT.MIN.FILE <- "nozzle.min.js";
81
82 .NOZZLE.JQUERY.MIN.FILE <- "jquery-1.5.1.min.js";
83 .NOZZLE.TABLESORTER.MIN.FILE <- "jquery.tablesorter.min.js";
84
85 .NOZZLE.GOOGLEANALYTICS.FILE <- "google-analytics.js";
86 .NOZZLE.GOOGLEANALYTICS.VARIABLE <- "GOOGLE_ANALYTICS_ID";
87
88 .NOZZLE.CSS.PATH <- "css";
89 .NOZZLE.CSS.FILE <- "nozzle.css";
90 .NOZZLE.CSS.MIN.FILE <- "nozzle.min.css";
91 .NOZZLE.CSS.PRINT.FILE <- "nozzle.print.css";
92 .NOZZLE.CSS.PRINT.MIN.FILE <- "nozzle.print.min.css";
93
94 .NOZZLE.CREDITS <- "Made with Nozzle";
95 .NOZZLE.URL <- "http://www.github.com/parklab/Nozzle";
96
97
98 #' Default number of significant digits to be used to trim numeric columns in tables.
99 #' @export
100 TABLE.SIGNIFICANT.DIGITS <- 2;
101
102
103 #' Public visibility.
104 #' @export
105 PROTECTION.PUBLIC <- 0;
106
107 #' Group visibility.
108 #' @export
109 PROTECTION.TCGA <- 5; # TCGA is an alias for GROUP
110
111 #' Group visibility.
112 #' @export
113 PROTECTION.GROUP <- 5;
114
115 #' Private visibility.
116 #' @export
117 PROTECTION.PRIVATE <- 10;
118
119
120 #' Logo position.
121 #' @export
122 LOGO.TOP.LEFT <- 1;
123
124 #' Logo position.
125 #' @export
126 LOGO.TOP.CENTER <- 2;
127
128 #' Logo position.
129 #' @export
130 LOGO.TOP.RIGHT <- 4;
131
132 #' Logo position.
133 #' @export
134 LOGO.BOTTOM.LEFT <- 8;
135
136 #' Logo position.
137 #' @export
138 LOGO.BOTTOM.CENTER <- 16;
139
140 #' Logo position.
141 #' @export
142 LOGO.BOTTOM.RIGHT <- 32;
143
144
145 #' Image type.
146 #' @export
147 IMAGE.TYPE.RASTER <- 0;
148
149 #' Image type.
150 #' @export
151 IMAGE.TYPE.SVG <- 1;
152
153 #' Image type.
154 #' @export
155 IMAGE.TYPE.PDF <- 2;
156
157
158 #' Section class.
159 #' @export
160 SECTION.CLASS.RESULTS <- "results";
161
162 #' Section class.
163 #' @export
164 SECTION.CLASS.META <- "meta";
165
166
167 #' Output type.
168 #' @export
169 HTML.REPORT <- "html_standalone";
170
171 #' Output type.
172 #' @export
173 HTML.FRAGMENT <- "html_fragment";
174
175 #' Output type.
176 #' @export
177 RDATA.REPORT <- "rdata";
178
179
180 #' Default DOI resolver URL.
181 #' @export
182 DEFAULT.DOI.RESOLVER <- "http://dx.doi.org"
183
184
185 .REFERENCE.STRING <- "#'REF#'";
186
187 .onLoad <- function( libname, pkgname )
188 {
189 .initEnvCounter( ".FIGURE.COUNTER" );
190 .initEnvCounter( ".TABLE.COUNTER" );
191 .initEnvCounter( ".FIGURE.SUPPLEMENTARY.COUNTER" );
192 .initEnvCounter( ".TABLE.SUPPLEMENTARY.COUNTER" );
193 .initEnvCounter( ".CITATION.COUNTER" );
194 .initEnvCounter( ".RESULT.COUNTER" ); # counter for non-empty results (assigned upon result writing)
195
196 .initEnvCounter( ".ELEMENT.ID" );
197 .initEnvCounter( ".RESULT.ID" ); # ids also for empty results (assigned upon result creation)
198
199 .setGlobalReportId( "" );
200
201 # TODO: check if files exist
202 assign( "cssCode", readLines( file.path( system.file( .NOZZLE.CSS.PATH, package=pkgname, lib.loc=libname ), .NOZZLE.CSS.FILE), warn=FALSE ), envir=.nozzleEnv );
203 assign( "cssPrintCode", readLines( file.path( system.file( .NOZZLE.CSS.PATH, package=pkgname, lib.loc=libname ), .NOZZLE.CSS.PRINT.FILE), warn=FALSE ), envir=.nozzleEnv );
204 assign( "javaScriptCode", readLines( file.path( system.file( .NOZZLE.JAVASCRIPT.PATH, package=pkgname, lib.loc=libname ), .NOZZLE.JAVASCRIPT.FILE ), warn=FALSE ), envir=.nozzleEnv );
205 assign( "cssMinCode", readLines( file.path( system.file( .NOZZLE.CSS.PATH, package=pkgname, lib.loc=libname ), .NOZZLE.CSS.MIN.FILE ), warn=FALSE ), envir=.nozzleEnv );
206 assign( "cssPrintMinCode", readLines( file.path( system.file( .NOZZLE.CSS.PATH, package=pkgname, lib.loc=libname ), .NOZZLE.CSS.PRINT.MIN.FILE ), warn=FALSE ), envir=.nozzleEnv );
207 assign( "javaScriptMinCode", readLines( file.path( system.file( .NOZZLE.JAVASCRIPT.PATH, package=pkgname, lib.loc=libname ), .NOZZLE.JAVASCRIPT.MIN.FILE ), warn=FALSE ), envir=.nozzleEnv );
208
209 assign( "jQueryMinCode", readLines( file.path( system.file( .NOZZLE.JAVASCRIPT.PATH, package=pkgname, lib.loc=libname ), .NOZZLE.JQUERY.MIN.FILE ), warn=FALSE ), envir=.nozzleEnv );
210 assign( "jQueryTableSorterMinCode", readLines( file.path( system.file( .NOZZLE.JAVASCRIPT.PATH, package=pkgname, lib.loc=libname ), .NOZZLE.TABLESORTER.MIN.FILE ), warn=FALSE ), envir=.nozzleEnv );
211
212 assign( "googleAnalyticsCode", readLines( file.path( system.file( .NOZZLE.JAVASCRIPT.PATH, package=pkgname, lib.loc=libname ), .NOZZLE.GOOGLEANALYTICS.FILE ), warn=FALSE ), envir=.nozzleEnv );
213
214 assign( ".PACKAGE", pkgname, envir=.nozzleEnv );
215 }
216
217
218
219 # --- utility functions ----------------------------------------------------------------------------
220
221 .updateEnvCounter <- function( counter, increment=1, environment=.nozzleEnv )
222 {
223 assign( counter, get( counter, envir=.nozzleEnv ) + increment, envir=environment );
224
225 return ( get( counter, envir=environment ) );
226 }
227
228 .initEnvCounter <- function( counter, environment=.nozzleEnv )
229 {
230 assign( counter, 0, envir=environment );
231 }
232
233 .getNextElementId <- function()
234 {
235 return ( .updateEnvCounter( ".ELEMENT.ID" ) );
236 }
237
238
239 .setGlobalReportId <- function( id, environment=.nozzleEnv )
240 {
241 assign( ".REPORT.ID", id, envir=environment );
242 }
243
244
245 .getGlobalReportId <- function( environment=.nozzleEnv )
246 {
247 return ( get( ".REPORT.ID", environment ) );
248 }
249
250
251 .getPackageVersion <- function( environment=.nozzleEnv )
252 {
253 return ( .concat( get( ".PACKAGE", environment ), "_", .PACKAGE.VERSION ) );
254
255 # set lib.loc!
256 #return ( .concat( get( ".PACKAGE", environment ), " ", packageVersion( get( ".PACKAGE", environment ), lib.loc=??? ) ) );
257 }
258
259
260 .initNumbers <- function()
261 {
262 .initEnvCounter( ".FIGURE.COUNTER" );
263 .initEnvCounter( ".TABLE.COUNTER" );
264 .initEnvCounter( ".FIGURE.SUPPLEMENT.COUNTER" );
265 .initEnvCounter( ".TABLE.SUPPLEMENT.COUNTER" );
266 .initEnvCounter( ".CITATION.COUNTER" );
267 .initEnvCounter( ".RESULT.COUNTER" );
268 }
269
270
271 .getNextFigureNumber <- function()
272 {
273 return ( .updateEnvCounter( ".FIGURE.COUNTER" ) );
274 }
275
276 .getNextTableNumber <- function()
277 {
278 return ( .updateEnvCounter( ".TABLE.COUNTER" ) );
279 }
280
281 # supplementary figure numbers are negative
282 .getNextSupplementFigureNumber <- function()
283 {
284 return ( .updateEnvCounter( ".FIGURE.SUPPLEMENT.COUNTER", increment=-1 ) );
285 }
286
287 # supplementary table numbers are negative
288 .getNextSupplementTableNumber <- function()
289 {
290 return ( .updateEnvCounter( ".TABLE.SUPPLEMENT.COUNTER", increment=-1 ) );
291 }
292
293
294 .getNextCitationNumber <- function()
295 {
296 return ( .updateEnvCounter( ".CITATION.COUNTER" ) );
297 }
298
299 .getNextResultId <- function()
300 {
301 return ( .updateEnvCounter( ".RESULT.ID" ) );
302 }
303
304 .getNextResultNumber <- function()
305 {
306 return ( .updateEnvCounter( ".RESULT.COUNTER" ) );
307 }
308
309
310 .newReportId <- function()
311 {
312 # inspired by http://www.broofa.com/Tools/Math.uuid.js
313 # creates an almost RFC 4122-compliant version 4 UUID (see Note 1 below)
314 # Note 1: since this UUID is going to be part of a JavaScript function name dashes are replaced with underscores
315 # Note 2: this is written for convenience, not performance
316 # Note 3: the "R" at position 1 is required to make the id a valid HTML id
317
318 template <- unlist( strsplit( "Rxxxxxxxx_xxxx_4xxx_yxxx_xxxxxxxxxxxx", "" ) );
319 hex <- unlist( strsplit( "0123456789ABCDEF", "" ) );
320
321 result <- unlist( lapply( template, function( c ) {
322 r <- floor( runif( n=1, min=1, max=16 ) );
323 v <- 0;
324
325 if ( c == "x" )
326 {
327 v <- r;
328 }
329 else if ( c == "y" )
330 {
331 v <- ( r %% 4 ) + 8;
332 }
333 else
334 {
335 return ( c );
336 }
337
338 return ( hex[v] );
339 }) );
340
341 return ( paste( result, sep="", collapse="" ) );
342 }
343
344
345 .removeProtectedElements <- function( element, level )
346 {
347 if ( element$protection > level )
348 {
349 return ( NULL );
350 }
351
352 if ( length( element$elements ) > 0 )
353 {
354 retainedElements <- list();
355
356 for ( i in 1:length( element$elements ) )
357 {
358 result <- .removeProtectedElements( element$elements[[i]], level );
359
360 if ( !is.null( result ) )
361 {
362 retainedElements[[length( retainedElements ) + 1]] <- result;
363 }
364 }
365
366 element$elements <- retainedElements;
367 }
368
369 return ( element );
370 }
371
372
373 .updateNumberedElements <- function( element, supplement=FALSE )
374 {
375 if ( element$type == .ELEMENT.FIGURE )
376 {
377 if ( !supplement )
378 {
379 element$number <- .getNextFigureNumber();
380 }
381 else
382 {
383 element$number <- .getNextSupplementFigureNumber();
384 }
385 }
386
387 if ( element$type == .ELEMENT.TABLE )
388 {
389 if ( !supplement )
390 {
391 element$number <- .getNextTableNumber();
392 }
393 else
394 {
395 element$number <- .getNextSupplementTableNumber();
396 }
397 }
398
399 if ( element$type == .ELEMENT.CITATION )
400 {
401 element$number <- .getNextCitationNumber();
402 }
403
404 if ( length( element$elements ) > 0 )
405 {
406 for ( i in 1:length( element$elements ) )
407 {
408 if ( !supplement && ( element$type == .ELEMENT.RESULT ) )
409 {
410 supplement <- TRUE;
411 }
412
413 element$elements[[i]] <- .updateNumberedElements( element$elements[[i]], supplement=supplement );
414 }
415 }
416
417 return ( element );
418 }
419
420
421 .countSignificantResults <- function( element )
422 {
423 counter <- 0;
424
425 if ( element$type == .ELEMENT.RESULT )
426 {
427 if ( element$isSignificant == TRUE )
428 {
429 counter <- counter + 1;
430 }
431 }
432
433 if ( length( element$elements ) > 0 )
434 {
435 for ( i in 1:length( element$elements ) )
436 {
437 counter <- counter + .countSignificantResults( element$elements[[i]] );
438 }
439 }
440
441 return ( counter );
442 }
443
444
445 .getReference <- function( referenceId, element )
446 {
447 if ( element$id == referenceId )
448 {
449 if ( element$type == .ELEMENT.CITATION )
450 {
451 return ( .concat( "[", element$number, "]" ) );
452 }
453
454 return ( .concat( element$niceType, " ", element$number ) );
455 }
456
457 if ( length( element$elements ) > 0 )
458 {
459 for ( i in 1:length( element$elements ) )
460 {
461 result <- .getReference( referenceId, element$elements[[i]] );
462
463 if ( !is.null( result ) )
464 {
465 return ( result );
466 }
467 }
468 }
469
470 return ( NULL );
471 }
472
473
474 .updateReferences <- function( element, report )
475 {
476 if ( !is.null( element$text ) )
477 {
478 if ( is.list( element$text ) )
479 {
480 updatedText = "";
481
482 if ( length( element$text ) > 0 )
483 {
484 for ( i in 1:length( element$text ) )
485 {
486 if ( is.list( element$text[[i]] ) || is.null( element$text[[i]] ) || is.na( element$text[[i]] ) )
487 {
488 next();
489 }
490
491 if ( substr( element$text[[i]], 0, nchar( .REFERENCE.STRING ) ) == .REFERENCE.STRING )
492 {
493 referenceId <- substr( element$text[[i]], nchar( .REFERENCE.STRING ) + 1, nchar( element$text[[i]] ) );
494 referenceText <- .getReference( referenceId, report );
495
496 if ( is.null( referenceText ) )
497 {
498 stop( paste( "Element", referenceId, "is referenced but not included in the report." ) );
499 }
500
501 updatedText <- paste( updatedText, referenceText, sep="" );
502 }
503 else
504 {
505 updatedText <-paste( updatedText, element$text[[i]], sep="" );
506 }
507 }
508 }
509
510 element$text <- updatedText;
511 }
512 }
513
514 if ( length( element$elements ) > 0 )
515 {
516 for ( i in 1:length( element$elements ) )
517 {
518 element$elements[[i]] <- .updateReferences( element$elements[[i]], report );
519 }
520 }
521
522 return ( element );
523 }
524
525
526 .updateResults <- function( element, report )
527 {
528 # keeps track of all results that are encountered in this element
529 resultList <- list();
530
531 if ( !is.null( element$text ) )
532 {
533 if ( is.list( element$text ) )
534 {
535 updatedList <- list();
536 j <- 1;
537
538 if ( length( element$text ) > 0 )
539 {
540 for ( i in 1:length( element$text ) )
541 {
542 if ( is.list( element$text[[i]] ) )
543 {
544 # TODO: check if this is really of type result!!!!
545 result <- element$text[[i]];
546
547 if ( result$isSignificant == TRUE )
548 {
549 if ( length( result$elements ) == 0 )
550 {
551 updatedList[[j]] <- .tag( "span", class=c( "result", "significant", "noevidence" ) );
552 }
553 else
554 {
555 updatedList[[j]] <- .tag( "span", id=.concat( "resultid_", result$resultId ), class=c( "result", "significant" ) );
556 }
557 }
558 else
559 {
560 if ( length( result$elements ) == 0 )
561 {
562 updatedList[[j]] <- .tag( "span", class=c( "result", "noevidence" ) );
563 }
564 else
565 {
566 updatedList[[j]] <- .tag( "span", id=.concat( "resultid_", result$resultId ), class=c( "result" ) );
567 }
568 }
569 j <- j + 1;
570
571 result <- .updateReferences( result, report );
572
573 updatedList[[j]] <- result$title;
574 j <- j + 1;
575
576 updatedList[[j]] <- .tag( "/span" );
577 j <- j + 1;
578
579 # the result should be added to the parent
580 # add the result linked from the result summary to this element:
581 resultList[[length( resultList ) + 1]] <- result;
582 }
583 else
584 {
585 updatedList[[j]] <-element$text[[i]];
586 j <- j + 1;
587 }
588 }
589 }
590
591 element$text <- updatedList;
592 }
593 }
594
595 if ( length( element$elements ) > 0 )
596 {
597 updatedElements <- list();
598
599 for ( i in 1:length( element$elements ) )
600 {
601 update <- .updateResults( element$elements[[i]], report );
602
603 updatedElements[[length( updatedElements ) + 1]] <- update$element;
604
605 # if results were found in the element they will be inserted after the element into the report tree
606 if ( length( update$results ) > 0 )
607 {
608 for ( r in 1:length( update$results ) )
609 {
610 updatedElements[[length( updatedElements ) + 1]] <- update$results[[r]];
611 }
612 }
613 }
614
615 # replace old elements with updated list
616 element$elements <- updatedElements;
617 }
618
619 return ( list( element=element, results=resultList ) );
620 }
621
622
623 .log <- function( ..., severity=.SEVERITY.LOG )
624 {
625 message <- .concat( ... );
626
627 if ( severity == .SEVERITY.LOG )
628 {
629 message( "Log: ", message, "\n" );
630 }
631
632 if ( severity == .SEVERITY.WARNING )
633 {
634 warning( message );
635 }
636
637 if ( severity == .SEVERITY.ERROR )
638 {
639 stop( message );
640 }
641 }
642
643
644 # --- element constructors -------------------------------------------------------------------------
645
646 .newElement <- function( elementType, title=NULL, text=NULL, niceType=NULL, domId=NULL, class=NULL, exportId=NULL, protection=PROTECTION.PUBLIC )
647 {
648 element <- list( type = elementType );
649 element$elements <- list();
650 element$protection <- protection;
651
652 element$id <- .getNextElementId();
653
654 if ( !missing( title ) )
655 {
656 element$title <- title;
657 }
658
659 if ( !missing( text ) )
660 {
661 element$text <- text;
662 }
663
664 if ( !missing( niceType ) )
665 {
666 element$niceType <- niceType;
667 }
668
669 if ( !missing( domId ) )
670 {
671 element$domId <- domId;
672 }
673
674 if ( !missing( class ) )
675 {
676 element$class <- class;
677 }
678
679 if ( !missing( exportId ) )
680 {
681 element$exportId <- exportId;
682 }
683 else
684 {
685 element$exportId = NULL;
686 }
687
688
689 return ( element );
690 }
691
692
693 #' Create a new report with pre-defined sections Overview/Introduction, Overview/Summary, Results, Methods & Data/Input, Methods & Data/References and Meta Data.
694 #' @param ... One or more strings that will be concatenated into the report title.
695 #' @param version Version number. Not in use.
696 #' @export
697 #' @return A new report element.
698 #'
699 #' @author Nils Gehlenborg (nils@@hms.harvard.edu)
700 newReport <- function( ..., version=0 )
701 {
702 if ( !missing( ... ) )
703 {
704 element <- .newGeneralReport( ..., version=version );
705 }
706 else
707 {
708 element <- .newGeneralReport( "Nozzle Report", version=version );
709 }
710
711 overview <- newSection( "Overview" );
712 overview$domId <- "overview";
713
714 introduction <- newSubSection( "Introduction" );
715 introduction$domId <- "introduction";
716
717 summary <- newSubSection( "Summary" );
718 summary$domId <- "summary";
719
720 results <- newSection( "Results", class="results" );
721 results$domId <- "results";
722
723 methods <- newSection( "Methods & Data" );
724 methods$domId <- "methods";
725
726 input <- newSubSection( "Input" );
727 input$domId <- "input";
728
729 references <- newSubSection( "References" )
730 references$domId <- "references";
731
732 meta <- newSection( "Meta Information", class="meta" )
733 meta$domId <- "meta";
734
735 # If a new predefined section is added here a ".hasPredefinedXXXSection" function needs to be added below as well as an an "addToXXX" function.
736
737 element$predefined <- list();
738 element$predefined$overviewId <- overview$id;
739 element$predefined$introductionId <- introduction$id;
740 element$predefined$summaryId <- summary$id;
741 element$predefined$resultsId <- results$id;
742 element$predefined$methodsId <- methods$id;
743 element$predefined$inputId <- input$id;
744 element$predefined$referencesId <- references$id;
745 element$predefined$metaId <- meta$id;
746
747 element <- addTo( parent=element, addTo( parent=overview, introduction, summary ) );
748 element <- addTo( parent=element, results );
749 element <- addTo( parent=element, addTo( parent=methods, input, references ) );
750 element <- addTo( parent=element, meta );
751 }
752
753
754 .hasPredefinedOverviewSection <- function( report )
755 {
756 return ( ( is.list( report$predefined ) && ( "overviewId" %in% names( report$predefined ) ) ) );
757 }
758
759
760 .hasPredefinedSummarySection <- function( report )
761 {
762 return ( ( is.list( report$predefined ) && ( "summaryId" %in% names( report$predefined ) ) ) );
763 }
764
765
766 .hasPredefinedIntroductionSection <- function( report )
767 {
768 return ( ( is.list( report$predefined ) && ( "introductionId" %in% names( report$predefined ) ) ) );
769 }
770
771
772 .hasPredefinedResultsSection <- function( report )
773 {
774 return ( ( is.list( report$predefined ) && ( "resultsId" %in% names( report$predefined ) ) ) );
775 }
776
777
778 .hasPredefinedInputSection <- function( report )
779 {
780 return ( ( is.list( report$predefined ) && ( "inputId" %in% names( report$predefined ) ) ) );
781 }
782
783
784 .hasPredefinedReferencesSection <- function( report )
785 {
786 return ( ( is.list( report$predefined ) && ( "referencesId" %in% names( report$predefined ) ) ) );
787 }
788
789
790 .hasPredefinedMethodsSection <- function( report )
791 {
792 return ( ( is.list( report$predefined ) && ( "methodsId" %in% names( report$predefined ) ) ) );
793 }
794
795
796 .hasPredefinedMetaSection <- function( report )
797 {
798 return ( ( is.list( report$predefined ) && ( "metaId" %in% names( report$predefined ) ) ) );
799 }
800
801
802 #' Create a new custom report without pre-defined sections.
803 #' @param ... One or more strings that will be concatenated into the report title.
804 #' @param version Version number. Not in use.
805 #' @export
806 #' @return A new report element.
807 #'
808 #' @author Nils Gehlenborg (nils@@hms.harvard.edu)
809 newCustomReport <- function( ..., version=0 )
810 {
811 if ( !missing( ... ) )
812 {
813 element <- .newGeneralReport( ..., version=version );
814 }
815 else
816 {
817 element <- .newGeneralReport( "Nozzle Custom Report", version=version );
818 }
819 }
820
821
822 .newGeneralReport <- function( ..., version=0 )
823 {
824 element <- .newElement( .ELEMENT.REPORT, .concat( ... ) );
825
826 element$subTitle <- NA;
827
828 element$meta <- list();
829
830 # report version
831 element$meta$version <- version;
832
833 # report creator software
834 element$meta$creator$name <- .getPackageVersion();
835 element$meta$renderer$name <- element$meta$creator$name;
836
837 # report creation date
838 element$meta$creator$date <- date();
839 element$meta$renderer$date <- element$meta$creator$date;
840
841 # maintainer details
842 element$meta$maintainer$name <- NA;
843 element$meta$maintainer$email <- NA;
844 element$meta$maintainer$affiliation <- NA;
845
846 # DOI resolver URL
847 setDoiResolver( element, DEFAULT.DOI.RESOLVER );
848
849 # report navigation: parent, prev, next
850 element$navigation <- list();
851
852 element$navigation$parentUrl <- NA;
853 element$navigation$parentName <- NA;
854
855 element$navigation$nextUrl <- NA;
856 element$navigation$nextName <- NA;
857
858 element$navigation$previousUrl <- NA;
859 element$navigation$previousName <- NA;
860
861 return ( element );
862 }
863
864
865 #' Set the URL and title of the "next" report after \code{report}. This will be accessible through the utility menu.
866 #' @param report Report object.
867 #' @param url URL of the next report.
868 #' @param ... One or more strings that will be concatenated to form the title of the next report.
869 #' @export
870 #' @return Updated report element.
871 #'
872 #' @author nils
873 setNextReport <- function( report, url, ... )
874 {
875 if ( is.null( report$navigation ) )
876 {
877 report$navigation <- list();
878 }
879
880 report$navigation$nextUrl <- url;
881 report$navigation$nextName <- .concat( ... );
882
883 return ( report );
884 }
885
886
887 #' Set the URL and title of the "previous" report before \code{report}. This will be accessible through the utility menu.
888 #' @param report Report object.
889 #' @param url URL of the next report.
890 #' @param ... One or more strings that will be concatenated to form the title of the previous report.
891 #' @export
892 #' @return Updated report element.
893 #'
894 #' @author nils
895 setPreviousReport <- function( report, url, ... )
896 {
897 if ( is.null( report$navigation ) )
898 {
899 report$navigation <- list();
900 }
901
902 report$navigation$previousUrl <- url;
903 report$navigation$previousName <- .concat( ... );
904
905 return ( report );
906 }
907
908
909 #' Set the URL and title of the "parent" report above \code{report}. This will be accessible through the utility menu.
910 #' @param report Report object.
911 #' @param url URL of the next report.
912 #' @param ... One or more strings that will be concatenated to form the title of the parent report.
913 #' @export
914 #' @return Updated report element.
915 #'
916 #' @author nils
917 setParentReport <- function( report, url, ... )
918 {
919 if ( is.null( report$navigation ) )
920 {
921 report$navigation <- list();
922 }
923
924 report$navigation$parentUrl <- url;
925 report$navigation$parentName <- .concat( ... );
926
927 return ( report );
928 }
929
930
931 #' Get the title of \code{report}.
932 #' @param report Report element.
933 #' @export
934 #' @return Title of \code{report}
935 #'
936 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
937 getReportTitle <- function( report )
938 {
939 if ( is.null( report$title ) )
940 {
941 return ( NA );
942 }
943
944 return ( report$title );
945 }
946
947
948 #' Set the title of \code{report}.
949 #' @param report Report element.
950 #' @param ... One or more strings that will be concatenated to form the title of the report.
951 #' @export
952 #' @return Updated report element or NA if \code{report} has no title element.
953 #'
954 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
955 setReportTitle <- function( report, ... )
956 {
957 if ( !is.null( report$title ) )
958 {
959 report$title <- .concat( ... );
960
961 return ( report );
962 }
963
964 return ( NA );
965 }
966
967
968 #' Get the subtitle of \code{report}.
969 #' @param report Report element.
970 #' @export
971 #' @return SubTitle of \code{report}
972 #'
973 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
974 getReportSubTitle <- function( report )
975 {
976 if ( is.null( report$subTitle ) )
977 {
978 return ( NA );
979 }
980
981 return ( report$subTitle );
982 }
983
984
985 #' Set the subtitle of \code{report}.
986 #' @param report Report element.
987 #' @param ... One or more strings that will be concatenated to form the subtitle of the report.
988 #' @export
989 #' @return Updated report element or NA if \code{report} has no subtitle element.
990 #'
991 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
992 setReportSubTitle <- function( report, ... )
993 {
994 if ( !is.null( report$subTitle ) )
995 {
996 report$subTitle <- .concat( ... );
997
998 return ( report );
999 }
1000
1001 return ( NA );
1002 }
1003
1004
1005
1006 #' Get the ID (a UUID) of \code{report}.
1007 #' @param report Report element.
1008 #' @export
1009 #' @return ID of \code{report} or NA.
1010 #'
1011 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1012 getReportId <- function( report )
1013 {
1014 if ( is.null( report$reportId ) )
1015 {
1016 return ( NA );
1017 }
1018
1019 return ( report$reportId );
1020 }
1021
1022
1023 #' Get name and version of the Nozzle package that was used to render \code{report}.
1024 #' @param report Report element.
1025 #' @export
1026 #' @return Name and version of the Nozzle package that rendered \code{report} or NA.
1027 #'
1028 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1029 getRendererName <- function( report )
1030 {
1031 if ( is.null( report$meta$renderer ) || is.atomic( report$meta$renderer ) )
1032 {
1033 return ( NA );
1034 }
1035
1036 if ( is.null( report$meta$renderer$name ) )
1037 {
1038 return ( NA );
1039 }
1040
1041 return ( report$meta$renderer$name );
1042 }
1043
1044
1045 #' Get date when \code{report} was rendered.
1046 #' @param report Report element.
1047 #' @export
1048 #' @return Date when \code{report} was rendered or NA.
1049 #'
1050 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1051 getRendererDate <- function( report )
1052 {
1053 if ( is.null( report$meta$renderer ) || is.atomic( report$meta$renderer ) )
1054 {
1055 return ( NA );
1056 }
1057
1058 if ( is.null( report$meta$renderer$date ) )
1059 {
1060 return ( NA );
1061 }
1062
1063 return ( report$meta$renderer$date );
1064 }
1065
1066
1067 #' Get name and version of the Nozzle package that was used to create \code{report}.
1068 #' @param report Report element.
1069 #' @export
1070 #' @return Name and version of the Nozzle package that created \code{report} or NA.
1071 #'
1072 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1073 getCreatorName <- function( report )
1074 {
1075 if ( is.null( report$meta$creator ) || is.atomic( report$meta$creator ) )
1076 {
1077 return ( NA );
1078 }
1079
1080 if ( is.null( report$meta$creator$name ) )
1081 {
1082 return ( NA );
1083 }
1084
1085 return ( report$meta$creator$name );
1086 }
1087
1088
1089 #' Get date when \code{report} was created.
1090 #' @param report Report element.
1091 #' @export
1092 #' @return Date when \code{report} was created or NA.
1093 #'
1094 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1095 getCreatorDate <- function( report )
1096 {
1097 if ( is.null( report$meta$creator ) || is.atomic( report$meta$creator ) )
1098 {
1099 return ( NA );
1100 }
1101
1102 if ( is.null( report$meta$creator$date ) )
1103 {
1104 return ( NA );
1105 }
1106
1107 return ( report$meta$creator$date );
1108 }
1109
1110
1111 #' Set the name of the software that used Nozzle to generate \code{report}, e.g. "My Report Generator Script".
1112 #' @param report Report element.
1113 #' @param ... One or more strings that will be concatenated to form the name of the software.
1114 #' @export
1115 #' @return Updated report element.
1116 #'
1117 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1118 setSoftwareName <- function( report, ... )
1119 {
1120 if ( is.null( report$meta$software ) )
1121 {
1122 report$meta$software <- list();
1123 }
1124
1125 report$meta$software$name <- .concat( ... );
1126
1127 return ( report );
1128 }
1129
1130
1131 #' Get the name of the software that used Nozzle to generate \code{report}.
1132 #' @param report Report element.
1133 #' @export
1134 #' @return Name of the software that used Nozzle to generate \code{report}.
1135 #'
1136 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1137 getSoftwareName <- function( report )
1138 {
1139 if ( is.null( report$meta$software ) )
1140 {
1141 return ( NA );
1142 }
1143
1144 if ( is.null( report$meta$software$name ) )
1145 {
1146 return ( NA );
1147 }
1148
1149 return ( report$meta$software$name );
1150 }
1151
1152
1153 #' Set the name of the software that used Nozzle to generate \code{report}, e.g. "Version 1.2".
1154 #' @param report Report element.
1155 #' @param ... One or more strings that will be concatenated to form the version of the software.
1156 #' @export
1157 #' @return Updated report element.
1158 #'
1159 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1160 setSoftwareVersion <- function( report, ... )
1161 {
1162 if ( is.null( report$meta$software ) )
1163 {
1164 report$meta$software <- list();
1165 }
1166
1167 report$meta$software$version <- .concat( ... );
1168
1169 return ( report );
1170 }
1171
1172
1173 #' Get the version of the software that used Nozzle to generate \code{report}.
1174 #' @param report Report element.
1175 #' @export
1176 #' @return Version of the software that used Nozzle to generate \code{report}.
1177 #'
1178 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1179 getSoftwareVersion <- function( report )
1180 {
1181 if ( is.null( report$meta$software ) )
1182 {
1183 return ( NA );
1184 }
1185
1186 if ( is.null( report$meta$software$version ) )
1187 {
1188 return ( NA );
1189 }
1190
1191 return ( report$meta$software$version );
1192 }
1193
1194
1195 #' Set a logo file for one of six positions (three at the top, three at the bottom) in \code{report}, e.g. an institute logo.
1196 #' @param report Report element.
1197 #' @param filename Path or URL to the logo file (relative) to the final location of the report HTML file.
1198 #' @param position One of LOGO.TOP.LEFT, LOGO.TOP.CENTER, LOGO.TOP.RIGHT, LOGO.BOTTOM.LEFT, LOGO.BOTTOM.CENTER, LOGO.BOTTOM.RIGHT.
1199 #' @export
1200 #' @return Updated report element.
1201 #'
1202 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1203 setLogo <- function( report, filename, position )
1204 {
1205 if ( is.null( report$meta$logo ) )
1206 {
1207 report$meta$logo <- list();
1208 }
1209
1210 # top logos
1211 if ( position == LOGO.TOP.LEFT )
1212 {
1213 report$meta$logo$topleft <- filename;
1214 }
1215
1216 if ( position == LOGO.TOP.CENTER )
1217 {
1218 report$meta$logo$topcenter <- filename;
1219 }
1220
1221 if ( position == LOGO.TOP.RIGHT )
1222 {
1223 report$meta$logo$topright <- filename;
1224 }
1225
1226 # bottom logos
1227 if ( position == LOGO.BOTTOM.LEFT )
1228 {
1229 report$meta$logo$bottomleft <- filename;
1230 }
1231
1232 if ( position == LOGO.BOTTOM.CENTER )
1233 {
1234 report$meta$logo$bottomcenter <- filename;
1235 }
1236
1237 if ( position == LOGO.BOTTOM.RIGHT )
1238 {
1239 report$meta$logo$bottomright <- filename;
1240 }
1241
1242 return ( report );
1243 }
1244
1245
1246 #' Get logo file for one of six positions (three at the top, three at the bottom) in \code{report}.
1247 #' @param report Report element.
1248 #' @param position One of LOGO.TOP.LEFT, LOGO.TOP.CENTER, LOGO.TOP.RIGHT, LOGO.BOTTOM.LEFT, LOGO.BOTTOM.CENTER, LOGO.BOTTOM.RIGHT.
1249 #' @export
1250 #' @return Path or URL to the logo file at \code{position}.
1251 #'
1252 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1253 getLogo <- function( report, position )
1254 {
1255 if ( is.null( report$meta$logo ) )
1256 {
1257 return ( NA );
1258 }
1259
1260 # top logos
1261 if ( position == LOGO.TOP.LEFT )
1262 {
1263 if ( is.null( report$meta$logo$topleft ) )
1264 {
1265 return ( NA );
1266 }
1267
1268 return ( report$meta$logo$topleft )
1269 }
1270
1271 if ( position == LOGO.TOP.CENTER )
1272 {
1273 if ( is.null( report$meta$logo$topcenter ) )
1274 {
1275 return ( NA );
1276 }
1277
1278 return ( report$meta$logo$topcenter )
1279 }
1280
1281 if ( position == LOGO.TOP.RIGHT )
1282 {
1283 if ( is.null( report$meta$logo$topright ) )
1284 {
1285 return ( NA );
1286 }
1287
1288 return ( report$meta$logo$topright )
1289 }
1290
1291 # bottom logos
1292 if ( position == LOGO.BOTTOM.LEFT )
1293 {
1294 if ( is.null( report$meta$logo$bottomleft ) )
1295 {
1296 return ( NA );
1297 }
1298
1299 return ( report$meta$logo$bottomleft )
1300 }
1301
1302 if ( position == LOGO.BOTTOM.CENTER )
1303 {
1304 if ( is.null( report$meta$logo$bottomcenter ) )
1305 {
1306 return ( NA );
1307 }
1308
1309 return ( report$meta$logo$bottomcenter )
1310 }
1311
1312 if ( position == LOGO.BOTTOM.RIGHT )
1313 {
1314 if ( is.null( report$meta$logo$bottomright ) )
1315 {
1316 return ( NA );
1317 }
1318
1319 return ( report$meta$logo$bottomright )
1320 }
1321
1322 return ( NA );
1323 }
1324
1325
1326 #' Set name of maintainer of \code{report}.
1327 #' @param report Report element.
1328 #' @param ... One or more strings that will be concatenated to form the name of the maintainer.
1329 #' @export
1330 #' @return Updated report element.
1331 #'
1332 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1333 setMaintainerName <- function( report, ... )
1334 {
1335 if ( is.null( report$meta$maintainer ) )
1336 {
1337 report$meta$maintainer <- list();
1338 }
1339
1340 report$meta$maintainer$name <- .concat( ... );
1341
1342 return ( report );
1343 }
1344
1345
1346 #' Get name of maintainer of \code{report}.
1347 #' @param report Report element.
1348 #' @export
1349 #' @return Name of the maintainer.
1350 #'
1351 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1352 getMaintainerName <- function( report )
1353 {
1354 if ( is.null( report$meta$maintainer$name ) )
1355 {
1356 return ( NA );
1357 }
1358
1359 return ( report$meta$maintainer$name );
1360 }
1361
1362
1363 #' Set email address of maintainer of \code{report}.
1364 #' @param report Report element.
1365 #' @param ... One or more strings that will be concatenated to form the email address of the maintainer.
1366 #' @export
1367 #' @return Updated report element.
1368 #'
1369 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1370 setMaintainerEmail <- function( report, ... )
1371 {
1372 if ( is.null( report$meta$maintainer ) )
1373 {
1374 report$meta$maintainer <- list();
1375 }
1376
1377 report$meta$maintainer$email <- .concat( ... );
1378
1379 return ( report );
1380 }
1381
1382
1383 #' Get email address of maintainer of \code{report}.
1384 #' @param report Report element.
1385 #' @export
1386 #' @return Email address of the maintainer.
1387 #'
1388 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1389 getMaintainerEmail <- function( report )
1390 {
1391 if ( is.null( report$meta$maintainer$email ) )
1392 {
1393 return ( NA );
1394 }
1395
1396 return ( report$meta$maintainer$email );
1397 }
1398
1399
1400 #' Set affiliation of maintainer of \code{report}.
1401 #' @param report Report element.
1402 #' @param ... One or more strings that will be concatenated to form the affiliation of the maintainer.
1403 #' @export
1404 #' @return Updated report element.
1405 #'
1406 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1407 setMaintainerAffiliation <- function( report, ... )
1408 {
1409 if ( is.null( report$meta$maintainer ) )
1410 {
1411 report$meta$maintainer <- list();
1412 }
1413
1414 report$meta$maintainer$affiliation <- .concat( ... );
1415
1416 return ( report );
1417 }
1418
1419
1420 #' Get affiliation of maintainer of \code{report}.
1421 #' @param report Report element.
1422 #' @export
1423 #' @return Affiliation of the maintainer.
1424 #'
1425 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1426 getMaintainerAffiliation <- function( report )
1427 {
1428 if ( is.null( report$meta$maintainer$affiliation ) )
1429 {
1430 return ( NA );
1431 }
1432
1433 return ( report$meta$maintainer$affiliation );
1434 }
1435
1436
1437 #' Set copyright messsage for \code{report}.
1438 #' @param report Report element.
1439 #' @param owner Copyright owner, e.g. "The President and Fellows of Harvard College".
1440 #' @param year Copyright year, e.g. "2012" or "2011-2013".
1441 #' @param statement Copyright statement, e.g. "All rights reserved.".
1442 #' @param url A URL that will be linked to the copyright owner.
1443 #' @export
1444 #' @return Updated report element.
1445 #'
1446 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1447 setCopyright <- function( report, owner, year, statement=NA, url=NA )
1448 {
1449 if ( is.null( report$meta$copyright ) )
1450 {
1451 report$meta$copyright <- list();
1452 }
1453
1454 report$meta$copyright$owner <- owner;
1455 report$meta$copyright$year <- year;
1456 report$meta$copyright$statement <- statement;
1457 report$meta$copyright$url <- url;
1458
1459 return ( report );
1460 }
1461
1462
1463 #' Set contact information for \code{report}. This is used to create a "contact" button in the top right corner of the report, e.g. to collect feedback about the report.
1464 #' @param report Report element.
1465 #' @param email Email address of the recipient of contact emails.
1466 #' @param subject Subject of the email to be sent.
1467 #' @param message Message used to pre-populate the email body.
1468 #' @param label Label for the button, e.g. "Contact Us".
1469 #' @export
1470 #' @return Updated report element.
1471 #'
1472 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1473 setContactInformation <- function( report, email, subject, message, label=NA )
1474 {
1475 if ( is.null( report$meta$contact ) )
1476 {
1477 report$meta$contact <- list();
1478 }
1479
1480 report$meta$contact$label <- label;
1481 report$meta$contact$email <- email;
1482 report$meta$contact$subject <- subject;
1483 report$meta$contact$message <- gsub( "\n", "%0A", message ); # line breaks will break the Javascript code
1484
1485 return ( report );
1486 }
1487
1488
1489 #' Get contact email address for \code{report}.
1490 #' @param report Report element.
1491 #' @export
1492 #' @return Contact email address.
1493 #'
1494 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1495 getContactInformationEmail <- function( report )
1496 {
1497 if ( is.null( report$meta$contact$email ) )
1498 {
1499 return ( NA );
1500 }
1501
1502 return ( report$meta$contact$email );
1503 }
1504
1505
1506 #' Get contact email subject line for \code{report}.
1507 #' @param report Report element.
1508 #' @export
1509 #' @return Contact email subject line.
1510 #'
1511 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1512 getContactInformationSubject <- function( report )
1513 {
1514 if ( is.null( report$meta$contact$subject ) )
1515 {
1516 return ( NA );
1517 }
1518
1519 return ( report$meta$contact$subject );
1520 }
1521
1522
1523 #' Get contact email default message for \code{report}.
1524 #' @param report Report element.
1525 #' @export
1526 #' @return Contact email default message.
1527 #'
1528 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1529 getContactInformationMessage <- function( report )
1530 {
1531 if ( is.null( report$meta$contact$message ) )
1532 {
1533 return ( NA );
1534 }
1535
1536 return ( report$meta$contact$message );
1537 }
1538
1539
1540 #' Get label for contact button for \code{report}.
1541 #' @param report Report element.
1542 #' @export
1543 #' @return Contact email button label.
1544 #'
1545 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1546 getContactInformationLabel <- function( report )
1547 {
1548 if ( is.null( report$meta$contact$label ) )
1549 {
1550 return ( NA );
1551 }
1552
1553 return ( report$meta$contact$label );
1554 }
1555
1556
1557 #' Get name of the copyright owner for \code{report}.
1558 #' @param report Report element.
1559 #' @export
1560 #' @return Name of the copyright owner.
1561 #'
1562 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1563 getCopyrightOwner <- function( report )
1564 {
1565 if ( is.null( report$meta$copyright$owner ) )
1566 {
1567 return ( NA );
1568 }
1569
1570 return ( report$meta$copyright$owner );
1571 }
1572
1573
1574 #' Get copyright year \code{report}.
1575 #' @param report Report element.
1576 #' @export
1577 #' @return Copyright year.
1578 #'
1579 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1580 getCopyrightYear <- function( report )
1581 {
1582 if ( is.null( report$meta$copyright$year ) )
1583 {
1584 return ( NA );
1585 }
1586
1587 return ( report$meta$copyright$year );
1588 }
1589
1590
1591 #' Get copyright URL for \code{report}, which is linked to the copyright statement.
1592 #' @param report Report element.
1593 #' @export
1594 #' @return Copyright URL.
1595 #'
1596 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1597 getCopyrightUrl <- function( report )
1598 {
1599 if ( is.null( report$meta$copyright$url) )
1600 {
1601 return ( NA );
1602 }
1603
1604 return ( report$meta$copyright$url );
1605 }
1606
1607
1608 #' Get copyright statement for \code{report}. This text is linked to the copyright URL.
1609 #' @param report Report element.
1610 #' @export
1611 #' @return Text of the c opyright statement.
1612 #'
1613 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1614 getCopyrightStatement <- function( report )
1615 {
1616 if ( is.null( report$meta$copyright$statement ) )
1617 {
1618 return ( NA );
1619 }
1620
1621 return ( report$meta$copyright$statement );
1622 }
1623
1624
1625 #' Set name of entities that are called out as significant, e.g. "gene". This is currently not being used and might become obsolete in future versions of Nozzle.
1626 #' @param report Report element.
1627 #' @param ... List of strings that will be concatenated to form the name of the entities.
1628 #' @export
1629 #' @return Updated report element.
1630 #'
1631 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1632 setSignificantEntity <- function( report, ... )
1633 {
1634 report$meta$significantEntity <- .concat( ... );
1635
1636 return ( report );
1637 }
1638
1639
1640 #' Get name of entities that are called out as significant, e.g. "gene". This is currently not being used and might become obsolete in future versions of Nozzle.
1641 #' @param report Report element.
1642 #' @export
1643 #' @return Name of entities called out as significant.
1644 #'
1645 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1646 getSignificantEntity <- function( report )
1647 {
1648 if ( is.null( report$meta$significantEntity ) )
1649 {
1650 return ( DEFAULT.SIGNIFICANT.ENTITY );
1651 }
1652
1653 return ( report$meta$significantEntity );
1654 }
1655
1656
1657 #' Set the DOI (document object identifier, http://www.doi.org) for \code{report}. A warning will be emitted if the report has been assigned a DOI before.
1658 #' @param report Report element.
1659 #' @param doi The document object identifier.
1660 #' @export
1661 #' @return Updated report element.
1662 #' @note A document object identifer must have been created before this is called.
1663 #' @references \url{http://www.doi.org}
1664 #'
1665 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1666 setDoi <- function( report, doi )
1667 {
1668 if ( is.null( report$meta$doi ) )
1669 {
1670 report$meta$doi <- list();
1671 }
1672
1673 if ( !is.null( report$meta$doi$identifier ) )
1674 {
1675 warning( "This report has already been assigned a DOI (", report$meta$doi$identifier ,"), which will be overwritten. Each report should only be assigned a single DOI." );
1676 }
1677
1678 report$meta$doi$identifier <- doi;
1679
1680 return ( report );
1681 }
1682
1683
1684 #' Get the DOI (document object identifier, http://www.doi.org) for \code{report}.
1685 #' @param report Report element.
1686 #' @export
1687 #' @return Document object identifier (DOI) for \code{report}.
1688 #'
1689 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1690 getDoi <- function( report )
1691 {
1692 if ( is.null( report$meta$doi ) )
1693 {
1694 return ( NA );
1695 }
1696
1697 if ( is.null( report$meta$doi$identifier ) )
1698 {
1699 return ( NA );
1700 }
1701
1702 return ( report$meta$doi$identifier );
1703 }
1704
1705
1706 #' Set the DOI meta data creator for \code{report}.
1707 #' @param report Report element.
1708 #' @param creator Name of the report creator/author.
1709 #' @export
1710 #' @return Updated report element.
1711 #' @note This should match the meta data stored for the DOI.
1712 #'
1713 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1714 setDoiCreator <- function( report, creator )
1715 {
1716 if ( is.null( report$meta$doi ) )
1717 {
1718 report$meta$doi <- list();
1719 }
1720
1721 report$meta$doi$creator <- creator;
1722
1723 return ( report );
1724 }
1725
1726
1727 #' Get the DOI creator for \code{report}.
1728 #' @param report Report element.
1729 #' @export
1730 #' @return Creator associated with the DOI of the report \code{report}.
1731 #'
1732 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1733 getDoiCreator <- function( report )
1734 {
1735 if ( is.null( report$meta$doi ) )
1736 {
1737 return ( NA );
1738 }
1739
1740 if ( is.null( report$meta$doi$creator ) )
1741 {
1742 return ( NA );
1743 }
1744
1745 return ( report$meta$doi$creator );
1746 }
1747
1748
1749 #' Set the DOI meta data title for \code{report}.
1750 #' @param report Report element.
1751 #' @param title Title of the report.
1752 #' @export
1753 #' @return Updated report element.
1754 #' @note This should match the meta data stored for the DOI.
1755 #'
1756 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1757 setDoiTitle <- function( report, title )
1758 {
1759 if ( is.null( report$meta$doi ) )
1760 {
1761 report$meta$doi <- list();
1762 }
1763
1764 report$meta$doi$title <- title;
1765
1766 return ( report );
1767 }
1768
1769
1770 #' Get the DOI title for \code{report}.
1771 #' @param report Report element.
1772 #' @export
1773 #' @return Title associated with the DOI of the report \code{report}.
1774 #'
1775 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1776 getDoiTitle <- function( report )
1777 {
1778 if ( is.null( report$meta$doi ) )
1779 {
1780 return ( NA );
1781 }
1782
1783 if ( is.null( report$meta$doi$title ) )
1784 {
1785 return ( NA );
1786 }
1787
1788 return ( report$meta$doi$title );
1789 }
1790
1791
1792 #' Set the DOI meta data publisher for \code{report}.
1793 #' @param report Report element.
1794 #' @param publisher Publisher of the report.
1795 #' @export
1796 #' @return Updated report element.
1797 #' @note This should match the meta data stored for the DOI.
1798 #'
1799 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1800 setDoiPublisher <- function( report, publisher )
1801 {
1802 if ( is.null( report$meta$doi ) )
1803 {
1804 report$meta$doi <- list();
1805 }
1806
1807 report$meta$doi$publisher <- publisher;
1808
1809 return ( report );
1810 }
1811
1812
1813 #' Get the DOI publisher for \code{report}.
1814 #' @param report Report element.
1815 #' @export
1816 #' @return Publisher associated with the DOI of the report \code{report}.
1817 #'
1818 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1819 getDoiPublisher <- function( report )
1820 {
1821 if ( is.null( report$meta$doi ) )
1822 {
1823 return ( NA );
1824 }
1825
1826 if ( is.null( report$meta$doi$publisher ) )
1827 {
1828 return ( NA );
1829 }
1830
1831 return ( report$meta$doi$publisher );
1832 }
1833
1834
1835 #' Set the DOI meta data year for \code{report}.
1836 #' @param report Report element.
1837 #' @param year Publication year of the report.
1838 #' @export
1839 #' @return Updated report element.
1840 #' @note This should match the meta data stored for the DOI.
1841 #'
1842 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1843 setDoiYear <- function( report, year )
1844 {
1845 if ( is.null( report$meta$doi ) )
1846 {
1847 report$meta$doi <- list();
1848 }
1849
1850 report$meta$doi$year <- year;
1851
1852 return ( report );
1853 }
1854
1855
1856 #' Get the DOI year for \code{report}.
1857 #' @param report Report element.
1858 #' @export
1859 #' @return Publication year associated with the DOI of the report \code{report}.
1860 #'
1861 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1862 getDoiYear <- function( report )
1863 {
1864 if ( is.null( report$meta$doi ) )
1865 {
1866 return ( NA );
1867 }
1868
1869 if ( is.null( report$meta$doi$year ) )
1870 {
1871 return ( NA );
1872 }
1873
1874 return ( report$meta$doi$year );
1875 }
1876
1877
1878
1879 #' Set the DOI meta data version for \code{report}.
1880 #' @param report Report element.
1881 #' @param version Version of the report.
1882 #' @export
1883 #' @return Updated report element.
1884 #' @note This should match the meta data stored for the DOI.
1885 #'
1886 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1887 setDoiVersion <- function( report, version )
1888 {
1889 if ( is.null( report$meta$doi ) )
1890 {
1891 report$meta$doi <- list();
1892 }
1893
1894 report$meta$doi$version <- version;
1895
1896 return ( report );
1897 }
1898
1899
1900 #' Get the DOI version for \code{report}.
1901 #' @param report Report element.
1902 #' @export
1903 #' @return Version associated with the DOI of the report \code{report}.
1904 #'
1905 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1906 getDoiVersion <- function( report )
1907 {
1908 if ( is.null( report$meta$doi ) )
1909 {
1910 return ( NA );
1911 }
1912
1913 if ( is.null( report$meta$doi$version ) )
1914 {
1915 return ( NA );
1916 }
1917
1918 return ( report$meta$doi$version );
1919 }
1920
1921
1922
1923 #' Set the DOI resolver URL (e.g. http://dx.doi.org) for \code{report}. The URL must not end with a slash!
1924 #' @param report Report element.
1925 #' @param url The resolver URL (without a trailing slash). The default is "http://dx.doi.org".
1926 #' @export
1927 #' @return Updated report element.
1928 #'
1929 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1930 setDoiResolver <- function( report, url )
1931 {
1932 if ( is.null( report$meta$doi ) )
1933 {
1934 report$meta$doi <- list();
1935 }
1936
1937 report$meta$doi$resolver <- url;
1938
1939 return ( report );
1940 }
1941
1942
1943 #' Get the DOI resolver URL (e.g. http://dx.doi.org) for \code{report}.
1944 #' @param report Report element.
1945 #' @export
1946 #' @return Document object identifier (DOI) resolver URL for \code{report}.
1947 #'
1948 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1949 getDoiResolver <- function( report )
1950 {
1951 if ( is.null( report$meta$doi ) )
1952 {
1953 return ( NA );
1954 }
1955
1956 if ( is.null( report$meta$doi$resolver ) )
1957 {
1958 return ( NA );
1959 }
1960
1961 return ( report$meta$doi$resolver );
1962 }
1963
1964
1965 #' Set the Google Analytics tracking ID to be embedded in this report ("web property id", usually starts with "UA-").
1966 #' @param report Report element.
1967 #' @param id Web property ID for the Google Analytics tracking account.
1968 #' @export
1969 #' @return Updated report element.
1970 #' @note A (free) Google Analytics account is required.
1971 #' @references \url{http://www.google.com/analytics}
1972 #'
1973 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1974 setGoogleAnalyticsId <- function( report, id )
1975 {
1976 report$meta$googleAnalyticsId <- id;
1977
1978 return ( report );
1979 }
1980
1981
1982 #' Get Google Analytics tracking ID for \code{report}.
1983 #' @param report Report element.
1984 #' @export
1985 #' @return Google Analytics Tracking ID or NA if not set.
1986 #'
1987 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
1988 getGoogleAnalyticsId <- function( report )
1989 {
1990 if ( is.null( report$meta$googleAnalyticsId ) )
1991 {
1992 return ( NA );
1993 }
1994
1995 return ( report$meta$googleAnalyticsId );
1996 }
1997
1998
1999 #' Set the path or URL of the CSS file to be used to overwrite the default screen (not: print) style sheet. Can be relative or absolute.
2000 #' @param report Report element.
2001 #' @param cssFile URL or a relative or absolute path to a CSS file.
2002 #' @export
2003 #' @return Updated report element.
2004 #'
2005 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2006 setCustomScreenCss <- function( report, cssFile )
2007 {
2008 report$customScreenCss <- cssFile
2009
2010 return ( report );
2011 }
2012
2013
2014 #' Get the path or URL of the CSS file to be used to overwrite the default screen (not: print) style sheet.
2015 #' @param report Report element.
2016 #' @export
2017 #' @return Path or URL of CSS file. Can be relative or absolute.
2018 #'
2019 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2020 getCustomScreenCss <- function( report )
2021 {
2022 if ( is.null( report$customScreenCss ) )
2023 {
2024 return ( NA );
2025 }
2026
2027 return ( report$customScreenCss );
2028 }
2029
2030
2031 #' Set the path or URL of the CSS file to be used to overwrite the default print (not: screen) style sheet. Can be relative or absolute.
2032 #' @param report Report element.
2033 #' @param cssFile URL or a relative or absolute path to a CSS file.
2034 #' @export
2035 #' @return Updated report element.
2036 #'
2037 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2038 setCustomPrintCss <- function( report, cssFile )
2039 {
2040 report$customPrintCss <- cssFile
2041
2042 return ( report );
2043 }
2044
2045
2046 #' Get the path or URL of the CSS file to be used to overwrite the default print (not: screen) style sheet.
2047 #' @param report Report element.
2048 #' @export
2049 #' @return Path or URL of CSS file. Can be relative or absolute.
2050 #'
2051 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2052 getCustomPrintCss <- function( report )
2053 {
2054 if ( is.null( report$customPrintCss ) )
2055 {
2056 return ( NA );
2057 }
2058
2059 return ( report$customPrintCss );
2060 }
2061
2062
2063 .addToElementById <- function( element, id, ... )
2064 {
2065 # this function will always return the element if successful or not
2066 # test first with .getElementById (see below) if the element exists
2067
2068 if ( element$id == id )
2069 {
2070 element <- addTo( parent=element, ... );
2071 }
2072
2073 if ( length( element$elements ) > 0 )
2074 {
2075 for ( i in 1:length( element$elements ) )
2076 {
2077 element$elements[[i]] <- .addToElementById( element$elements[[i]], id, ... );
2078 }
2079 }
2080
2081 return ( element );
2082 }
2083
2084 .getElementById <- function( element, id )
2085 {
2086 if ( element$id == id )
2087 {
2088 return ( element );
2089 }
2090
2091 if ( length( element$elements ) > 0 )
2092 {
2093 for ( i in 1:length( element$elements ) )
2094 {
2095 result <- .getElementById( element$elements[[i]], id );
2096
2097 if ( !is.null( result ) )
2098 {
2099 return ( result );
2100 }
2101 }
2102 }
2103
2104 return ( NULL );
2105 }
2106
2107 # removes element the given id from the tree using breadth-first search
2108 .removeElementById <- function( element, id )
2109 {
2110 if ( length( element$elements ) > 0 )
2111 {
2112 newElements <- list();
2113
2114 for ( i in 1:length( element$elements ) )
2115 {
2116 if ( element$elements[[i]]$id != id )
2117 {
2118 newElements[[length( newElements ) + 1]] <- element$elements[[i]];
2119 }
2120 }
2121
2122 element$elements <- newElements;
2123
2124 if ( length( element$elements ) > 0 )
2125 {
2126 for ( i in 1:length( element$elements ) )
2127 {
2128 element$elements[[i]] <- .removeElementById( element$elements[[i]], id );
2129 }
2130 }
2131 }
2132
2133 return ( element );
2134 }
2135
2136
2137 #' Get an exported element from a report. This can be used to generate aggregate reports. This is an experimental feature of Nozzle and may not lead to the expected results.
2138 #' @param report The source report.
2139 #' @param exportId The ID of the exported element. \code{getExportedElementIds} returns a list of exported element IDs.
2140 #' @export
2141 #' @return The exported report element or NULL if the ID does not exist in \code{report}.
2142 #' @note Elements containing references should not be exported since references cannot be resolved in the target report. Relative paths in exported elements may have to be adjusted manually if the target report will be located in a different directory.
2143 #'
2144 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2145 getExportedElement <- function( report, exportId )
2146 {
2147 if ( !is.null( report$exportId ) && ( report$exportId == exportId ) )
2148 {
2149 return ( report );
2150 }
2151
2152 if ( length( report$elements ) > 0 )
2153 {
2154 for ( i in 1:length( report$elements ) )
2155 {
2156 result <- getExportedElement( report$elements[[i]], exportId );
2157
2158 if ( !is.null( result ) )
2159 {
2160 return ( result );
2161 }
2162 }
2163 }
2164
2165 return ( NULL );
2166 }
2167
2168
2169 #' Get the IDs of exported elements from \code{report}. This is an experimental feature of Nozzle and may not lead to the expected results.
2170 #' @param report The source report.
2171 #' @export
2172 #' @return The IDs of exported report elements or NULL.
2173 #'
2174 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2175 getExportedElementIds <- function( report )
2176 {
2177 if ( !is.null( report$exportId ) )
2178 {
2179 return( report$exportId );
2180 }
2181
2182 elementIds <- NULL;
2183
2184 if ( length( report$elements ) > 0 )
2185 {
2186 for ( i in 1:length( report$elements ) )
2187 {
2188 elementIds <- c( elementIds, getExportedElementIds( report$elements[[i]] ) );
2189 }
2190 }
2191
2192 return ( unique( elementIds ) );
2193 }
2194
2195
2196 #' Add elements to the "Results" section of a standard report.
2197 #' @param report Report element.
2198 #' @param ... Elements that will be added to the results section. Often these are subsection elements.
2199 #' @export
2200 #' @return Updated report element.
2201 #'
2202 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2203 addToResults <- function( report, ... )
2204 {
2205 if ( .hasPredefinedResultsSection( report ) )
2206 {
2207 return ( .addToElementById( report, report$predefined$resultsId, ... ) );
2208 }
2209
2210 stop( "This report does not contain a predefined results section. Is this a custom report?" );
2211 }
2212
2213
2214 #' Add elements to the "Methds & Data" section of a standard report.
2215 #' @param report Report element.
2216 #' @param ... Elements that will be added to the methods section. Often these are subsection elements.
2217 #' @export
2218 #' @return Updated report element.
2219 #'
2220 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2221 addToMethods <- function( report, ... )
2222 {
2223 if ( .hasPredefinedMethodsSection( report ) )
2224 {
2225 return ( .addToElementById( report, report$predefined$methodsId, ... ) );
2226 }
2227
2228 stop( "This report does not contain a predefined methods section. Is this a custom report?" );
2229 }
2230
2231
2232
2233 #' Add elements to the "Overview" section of a standard report.
2234 #' @param report Report element.
2235 #' @param ... Elements that will be added to the overview section. Often these are subsection elements.
2236 #' @export
2237 #' @return Updated report element.
2238 #'
2239 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2240 addToOverview <- function( report, ... )
2241 {
2242 if ( .hasPredefinedOverviewSection( report ) )
2243 {
2244 return ( .addToElementById( report, report$predefined$overviewId, ... ) );
2245 }
2246
2247 stop( "This report does not contain a predefined overview section. Is this a custom report?" );
2248 }
2249
2250
2251 #' Add elements to the "Introduction" subsection in the "Overview" section of a standard report.
2252 #' @param report Report element.
2253 #' @param ... Elements that will be added to the introduction section. Often these are paragraph elements.
2254 #' @export
2255 #' @return Updated report element.
2256 #'
2257 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2258 addToIntroduction <- function( report, ... )
2259 {
2260 if ( .hasPredefinedIntroductionSection( report ) )
2261 {
2262 return ( .addToElementById( report, report$predefined$introductionId, ... ) );
2263 }
2264
2265 stop( "This report does not contain a predefined introduction section. Is this a custom report?" );
2266 }
2267
2268
2269 #' Add elements to the "Summary" subsection in the "Overview" section of a standard report.
2270 #' @param report Report element.
2271 #' @param ... Elements that will be added to the overview section. Often these are paragraph elements.
2272 #' @export
2273 #' @return Updated report element.
2274 #'
2275 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2276 addToSummary <- function( report, ... )
2277 {
2278 if ( .hasPredefinedSummarySection( report ) )
2279 {
2280 return ( .addToElementById( report, report$predefined$summaryId, ... ) );
2281 }
2282
2283 stop( "This report does not contain a predefined summary section. Is this a custom report?" );
2284 }
2285
2286
2287 #' Add elements to the "Input" subsection in the "Methods & Data" section of a standard report.
2288 #' @param report Report element.
2289 #' @param ... Elements that will be added to the input section. Often these are paragraph elements.
2290 #' @export
2291 #' @return Updated report element.
2292 #'
2293 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2294 addToInput <- function( report, ... )
2295 {
2296 if ( .hasPredefinedInputSection( report ) )
2297 {
2298 return ( .addToElementById( report, report$predefined$inputId, ... ) );
2299 }
2300
2301 stop( "This report does not contain a predefined input section. Is this a custom report?" );
2302 }
2303
2304
2305 #' Add elements to the "References" subsection in the "Methods & Data" section of a standard report.
2306 #' @param report Report element.
2307 #' @param ... Elements that will be added to the references section. These should be citation elements.
2308 #' @export
2309 #' @return Updated report element.
2310 #'
2311 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2312 addToReferences <- function( report, ... )
2313 {
2314 if ( .hasPredefinedReferencesSection( report ) )
2315 {
2316 return ( .addToElementById( report, report$predefined$referencesId, ... ) );
2317 }
2318
2319 stop( "This report does not contain a predefined references section. Is this a custom report?" );
2320 }
2321
2322
2323 #' Add elements to the "Meta" section of a standard report.
2324 #' @param report Report element.
2325 #' @param ... Elements that will be added to the references section. These may be any elements.
2326 #' @export
2327 #' @return Updated report element.
2328 #'
2329 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2330 addToMeta <- function( report, ... )
2331 {
2332 if ( .hasPredefinedMetaSection( report ) )
2333 {
2334 return ( .addToElementById( report, report$predefined$metaId, ... ) );
2335 }
2336
2337 stop( "This report does not contain a predefined meta information section. Is this a custom report?" );
2338 }
2339
2340
2341 #' Create a new section element.
2342 #' @param ... Strings that will be concatenated to form the section title.
2343 #' @param class If set to SECTION.CLASS.RESULTS, results can be reported in this section. If set to SECTION.CLASS.META the section will be a meta data section.
2344 #' @param exportId Unique string to identify this element. Used to retrieve the element using \code{getExportedElement}.
2345 #' @param protection Procection level. One of PROTECTION.PUBLIC, PROTECTION.GROUP, PROTECTION.PRIVATE.
2346 #' @export
2347 #' @return New element.
2348 #'
2349 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2350 newSection <- function( ..., class="", exportId=NULL, protection=PROTECTION.PUBLIC )
2351 {
2352 return ( .newElement( .ELEMENT.SECTION, .concat( ... ), class=class, exportId=exportId, protection=protection ) );
2353 }
2354
2355
2356 #' Create a new subsection element.
2357 #' @param ... Strings that will be concatenated to form the subsection title.
2358 #' @param exportId Unique string to identify this element. Used to retrieve the element using \code{getExportedElement}.
2359 #' @param protection Procection level. One of PROTECTION.PUBLIC, PROTECTION.GROUP, PROTECTION.PRIVATE.
2360 #' @export
2361 #' @return New element.
2362 #'
2363 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2364 newSubSection <- function( ..., exportId=NULL, protection=PROTECTION.PUBLIC )
2365 {
2366 return ( .newElement( .ELEMENT.SUBSECTION, .concat( ... ), exportId=exportId, protection=protection ) );
2367 }
2368
2369
2370 #' Create a new subsubsection element.
2371 #' @param ... Strings that will be concatenated to form the subsubsection title.
2372 #' @param exportId Unique string to identify this element. Used to retrieve the element using \code{getExportedElement}.
2373 #' @param protection Procection level. One of PROTECTION.PUBLIC, PROTECTION.GROUP, PROTECTION.PRIVATE.
2374 #' @export
2375 #' @return New element.
2376 #'
2377 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2378 newSubSubSection <- function( ..., exportId=NULL, protection=PROTECTION.PUBLIC )
2379 {
2380 return ( .newElement( .ELEMENT.SUBSUBSECTION, .concat( ... ), exportId=exportId, protection=protection ) );
2381 }
2382
2383
2384 .writeSection <- function ( element, file, level="" )
2385 {
2386 if ( is.null( element$domId ) )
2387 {
2388 .write( .tag( "div", class=.concat( level, "section", " ", element$class ) ), file );
2389 }
2390 else
2391 {
2392 .write( .tag( "div", class=.concat( level, "section", " ", element$class ), id=element$domId ), file );
2393 }
2394
2395 .write( .tag( "div", class="sectionheader" ), file );
2396 .write( element$title, file );
2397 .write( .tag( "/div" ), file );
2398
2399 .write( .tag( "div", class="sectionbody" ), file );
2400 .writeElements( element, file );
2401 .write( .tag( "/div" ), file );
2402
2403 .write( .tag( "/div" ), file );
2404 }
2405
2406
2407 #' Create a new list element.
2408 #' @param ... Elements of type paragraph, list or result that will form the items in the list.
2409 #' @param isNumbered If set to FALSE, the list will be unordered with bullet points. If set to TRUE, the list will be numbered with arabic numerals.
2410 #' @param exportId Unique string to identify this element. Used to retrieve the element using \code{getExportedElement}.
2411 #' @param protection Procection level. One of PROTECTION.PUBLIC, PROTECTION.GROUP or PROTECTION.PRIVATE.
2412 #' @export
2413 #' @return New element.
2414 #'
2415 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2416 newList <- function( ..., isNumbered=FALSE, exportId=NULL, protection=PROTECTION.PUBLIC )
2417 {
2418 element <- .newElement( .ELEMENT.LIST, exportId=exportId, protection=protection );
2419
2420 element$isNumbered <- isNumbered;
2421
2422 if ( missing( ... ) )
2423 {
2424 return ( element );
2425 }
2426
2427 args <- list( ... );
2428
2429 # TODO: run the same checks in "addTo"
2430 for ( i in 1:length( args ) )
2431 {
2432 if ( args[[i]]$type == .ELEMENT.PARAGRAPH ||
2433 args[[i]]$type == .ELEMENT.LIST ||
2434 args[[i]]$type == .ELEMENT.RESULT )
2435 {
2436 element$elements[[length( element$elements ) + 1]] <- args[[i]];
2437 }
2438 else
2439 {
2440 stop( "Report elements of type ", args[[i]]$element$type, " can not be added to a list." );
2441 }
2442 }
2443
2444 return ( element );
2445 }
2446
2447 .writeList <- function( element, file )
2448 {
2449 if ( element$isNumbered == TRUE )
2450 {
2451 .write( .tag( "ol" ), file );
2452 }
2453 else
2454 {
2455 .write( .tag( "ul" ), file );
2456 }
2457
2458 for ( i in 1:length( element$elements ) )
2459 {
2460 if ( ( element$elements[[i]]$type != .ELEMENT.LIST ) && ( element$elements[[i]]$type != .ELEMENT.RESULT ) )
2461 {
2462 .write( .tag( "li" ), file );
2463 .writeElement( element$elements[[i]], file );
2464 .write( .tag( "/li" ), file );
2465 }
2466 else
2467 {
2468 .writeElement( element$elements[[i]], file );
2469 }
2470 }
2471
2472 if ( element$isNumbered == TRUE )
2473 {
2474 .write( .tag( "/ol" ), file );
2475 }
2476 else
2477 {
2478 .write( .tag( "/ul" ), file );
2479 }
2480 }
2481
2482
2483 #' Create a new list element.
2484 #' @param file Path or URL to the image file. Paths can be absolute or relative.
2485 #' @param ... Strings that will be concatenated to form the figure caption.
2486 #' @param fileHighRes Path or URL to a high-resolution or vector-based version of the image file. Paths can be absolute or relative.
2487 #' @param type Currenlty only IMAGE.TYPE.RASTER is allowed.
2488 #' @param exportId Unique string to identify this element. Used to retrieve the element using \code{getExportedElement}.
2489 #' @param protection Procection level. One of PROTECTION.PUBLIC, PROTECTION.GROUP, PROTECTION.PRIVATE.
2490 #' @export
2491 #' @return New element.
2492 #'
2493 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2494 newFigure <- function( file, ..., fileHighRes=NA, type=IMAGE.TYPE.RASTER, exportId=NULL, protection=PROTECTION.PUBLIC )
2495 {
2496 element <- .newElement( .ELEMENT.FIGURE, niceType="Figure", exportId=exportId, protection=protection );
2497
2498 element$imageFilename <- file;
2499 element$imageFilenameHighRes <- fileHighRes;
2500 element$imageType <- type;
2501
2502 element$text <- list( ... );
2503
2504 return ( element );
2505 }
2506
2507
2508 #' Test if \code{element} is a figure element.
2509 #' @param element Element to test.
2510 #' @export
2511 #' @return TRUE if the element is a figure, FALSE otherwise.
2512 #'
2513 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2514 isFigure <- function( element )
2515 {
2516 return ( element$type == .ELEMENT.FIGURE );
2517 }
2518
2519
2520 #' Get path or URL of image file associated with a figure element.
2521 #' @param element Figure element.
2522 #' @export
2523 #' @return Path or URL of the image file or NA if \code{element} is not a figure.
2524 #'
2525 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2526 getFigureFile <- function( element )
2527 {
2528 if ( isFigure( element ) )
2529 {
2530 return ( element$imageFilename );
2531 }
2532
2533 return ( NA );
2534 }
2535
2536
2537 #' Set path or URL of image file associated with a figure element. Paths can relative or absolute.
2538 #' @param element A figure element.
2539 #' @param file Path or URL of the image file.
2540 #' @export
2541 #' @return Updated figure element or NA is \code{element} is not a figure.
2542 #'
2543 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2544 setFigureFile <- function( element, file )
2545 {
2546 if ( isFigure( element ) )
2547 {
2548 element$imageFilename <- file;
2549
2550 return ( element );
2551 }
2552
2553 return ( NA );
2554 }
2555
2556
2557 #' Get path or URL of high-resolution of vector-based image file associated with a figure element.
2558 #' @param element Figure element.
2559 #' @export
2560 #' @return Path or URL of the image file or NA if \code{element} is not a figure.
2561 #'
2562 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2563 getFigureFileHighRes <- function( element )
2564 {
2565 if ( isFigure( element ) )
2566 {
2567 return ( element$imageFilenameHighRes );
2568 }
2569
2570 return ( NA );
2571 }
2572
2573
2574 #' Set path or URL of high-resolution or vector-based image file associated with a figure element. Paths can be relative or absolute.
2575 #' @param element A figure element.
2576 #' @param file Path or URL of the image file.
2577 #' @export
2578 #' @return Updated figure element or NA is \code{element} is not a figure.
2579 #'
2580 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2581 setFigureFileHighRes <- function( element, file )
2582 {
2583 if ( isFigure( element ) )
2584 {
2585 element$imageFilenameHighRes <- file;
2586
2587 return ( element );
2588 }
2589
2590 return ( NA );
2591 }
2592
2593
2594 .writeFigure <- function ( element, file )
2595 {
2596 figureNumber <- element$number;
2597
2598 # supplementary figure?
2599 if ( figureNumber < 0 )
2600 {
2601 figureNumber <- .concat( "S", abs( figureNumber ) );
2602 }
2603
2604 .write( .tag( "div", class="figure", id=.concat( "figure_", figureNumber ) ), file );
2605
2606 .write( .tag( "div", class="caption" ), file );
2607 .write( .tag( "p" ), file );
2608
2609 # "Figure 1. " ...
2610 .write( asStrong( element$niceType, "&nbsp;", figureNumber, ".&nbsp;" ), file );
2611
2612 if ( !is.na( element$imageFilenameHighRes ) )
2613 {
2614 .write( "\n", .tag( "a", class="download", other=.concat( "href=\"", element$imageFilenameHighRes , "\"" ) ), "Get High-res Image", .tag( "/a" ), "\n", file );
2615 }
2616 .write( element$text, file );
2617 .write( .tag( "/p" ), file );
2618 .write( .tag( "/div" ), file ); # caption
2619
2620 .write( .tag( "div", class="image" ), file );
2621
2622
2623 if ( ( element$imageType == IMAGE.TYPE.RASTER ) || ( is.null( element$imageType ) ) )
2624 {
2625 .write( .tag( "img", other=.concat( "src=\"", element$imageFilename, "\"/" ) ), file );
2626 }
2627 else if ( element$imageType == IMAGE.TYPE.SVG )
2628 {
2629 .write( .tag( "object", other=.concat( "data=\"", element$imageFilename, "\" type=\"image/svg+xml\"" ) ), file );
2630 .write( .tag( "/object" ), file );
2631 }
2632 else
2633 {
2634 stop( "Unknown image type selected in newFigure()." );
2635 }
2636
2637 .write( .tag( "/div" ), file ); # image
2638
2639 .write( .tag( "/div" ), file ); # figure
2640 }
2641
2642
2643 #' Create new table element.
2644 #' @param table A matrix or data frame containing the table data. Column names will be extracted and used as column headers.
2645 #' @param ... Strings that will be concatenated to form the table caption.
2646 #' @param file Path or URL to a file containing the full table. It is recommend to only show a relevant subset of all results in the report itself to increase readability.
2647 #' @param significantDigits Number of significant digits used to trim all numeric columns. The default is \code{TABLE.SIGNIFICANT.DIGITS}.
2648 #' @param exportId Unique string to identify this element. Used to retrieve the element using \code{getExportedElement}.
2649 #' @param protection Procection level. One of PROTECTION.PUBLIC, PROTECTION.GROUP, PROTECTION.PRIVATE.
2650 #' @export
2651 #' @return New element.
2652 #'
2653 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2654 newTable <- function( table, ..., file=NA, significantDigits=TABLE.SIGNIFICANT.DIGITS, exportId=NULL, protection=PROTECTION.PUBLIC )
2655 {
2656 element <- .newElement( .ELEMENT.TABLE, niceType="Table", exportId=exportId, protection=protection );
2657
2658 element$table <- table;
2659 element$tableSignificantDigits <- significantDigits;
2660 element$tableFilename <- file;
2661
2662 element$resultList <- list();
2663
2664 if ( !is.null( dim( table ) ) )
2665 {
2666 element$resultIndices <- array( dim=dim( table ) );
2667 }
2668
2669 element$text <- list( ... );
2670
2671 return ( element );
2672 }
2673
2674
2675 #' Test if \code{element} is a table element.
2676 #' @param element Element to test.
2677 #' @export
2678 #' @return TRUE if the element is a table, FALSE otherwise.
2679 #'
2680 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2681 isTable <- function( element )
2682 {
2683 return ( element$type == .ELEMENT.TABLE );
2684 }
2685
2686
2687 #' Get path or URL of file associatd with table element.
2688 #' @param element Table element.
2689 #' @export
2690 #' @return Path or URL.
2691 #'
2692 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2693 getTableFile <- function( element )
2694 {
2695 if ( isTable( element ) )
2696 {
2697 return ( element$tableFilename );
2698 }
2699
2700 return ( NA );
2701 }
2702
2703
2704 #' Set path or URL of file associatd with table element.
2705 #' @param element Table element.
2706 #' @param file Path or URL to file.
2707 #' @export
2708 #' @return Updated element.
2709 #'
2710 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2711
2712 setTableFile <- function( element, file )
2713 {
2714 if ( isTable( element ) )
2715 {
2716 element$tableFilename <- file;
2717
2718 return ( element );
2719 }
2720
2721 return ( NA );
2722 }
2723
2724
2725 .trimTable <- function( table, significantDigits=3 )
2726 {
2727 if ( significantDigits <= 0 )
2728 {
2729 return ( table );
2730 }
2731
2732 for( c in 1:dim( table )[2] )
2733 {
2734 # if this column is numeric ...
2735 if ( is.numeric( table[,c] ) == TRUE )
2736 {
2737 # if there are entries with decimal digits ...
2738 if ( any( trunc( table[,c] ) != table[,c], na.rm=TRUE ) )
2739 {
2740 # trim to significant digits
2741 table[,c] <- signif( table[,c], digits=significantDigits );
2742 }
2743 else
2744 {
2745 # add separators between every block of 3 digits
2746 # PROBLEM: TableSorter doesn't sort these numbers correctly
2747 #table[,c] <- format( table[,c], big.mark=",", scientific=FALSE );
2748 }
2749 }
2750 }
2751
2752 return ( table );
2753 }
2754
2755
2756 .writeTable <- function ( element, file )
2757 {
2758 tableNumber <- element$number;
2759
2760 # supplementary table?
2761 if ( tableNumber < 0 )
2762 {
2763 tableNumber <- .concat( "S", abs( tableNumber ) );
2764 }
2765
2766 .write( .tag( "div", class="table", id=.concat( "table_", tableNumber ) ), file );
2767
2768 .write( .tag( "div", class="caption" ), file );
2769 .write( .tag( "p" ), file );
2770
2771 # "Table 1. " ...
2772 .write( asStrong( element$niceType, "&nbsp;", tableNumber, ".&nbsp;" ), file );
2773
2774 if ( !is.na( element$tableFilename ) )
2775 {
2776 .write( "\n", .tag( "a", class="download", other=.concat( "href=\"", element$tableFilename, "\"" ) ), "Get Full Table", .tag( "/a" ), "\n", file );
2777 }
2778
2779 .write( element$text, file );
2780 .write( .tag( "/p" ), file );
2781 .write( .tag( "/div" ), file ); # caption
2782
2783 # write table only if a matrix/data frame has been supplied
2784 if ( is.null( dim( element$table ) ) )
2785 {
2786 .write( .tag( "table", class="resulttable tablesorter sortabletable" ), file );
2787
2788 # --- table header ---
2789
2790 .write( .tag( "thead" ), file );
2791 .write( .tag( "tr" ), file );
2792
2793 # write column names
2794 for( c in 1:length( element$table ) )
2795 {
2796 .write( .tag( "th" ), file );
2797 .write( element$table[c], file );
2798 .write( .tag( "/th" ), file );
2799 }
2800
2801 .write( .tag( "/tr" ), file );
2802 .write( .tag( "/thead" ), file );
2803
2804 .write( .tag( "tbody" ), file );
2805 .write( .tag( "tr" ), file );
2806 .write( .tag( "td", other=.concat( "colspan=\"", length( element$table ), "\" align=\"center\"" ) ), file );
2807 .write( .tag( "a", other=.concat( "href=\"", element$tableFilename, "\"" ) ), "Get Full Table", .tag( "/a" ), file );
2808 .write( .tag( "/td" ), file );
2809 .write( .tag( "/tr" ), file );
2810 .write( .tag( "/tbody" ), file );
2811
2812 .write( .tag( "/table" ), file ); # resultstable
2813 }
2814 else
2815 {
2816 # TODO: make sure that the resulttable class is attached to these tables
2817 #.write( .tag( "table", id=.concat( "sortable_table_", element$number ), class="tablesorter" ), file );
2818 #.write( .tag( "table", id=.concat( "sortable_table_", element$number ), class="resulttable tablesorter" ), file );
2819 .write( .tag( "table", class="resulttable tablesorter sortabletable" ), file );
2820
2821 # --- table header ---
2822
2823 .write( .tag( "thead" ), file );
2824 .write( .tag( "tr" ), file );
2825
2826 # write column names
2827 for( c in 1:dim( element$table )[2] )
2828 {
2829 .write( .tag( "th" ), file );
2830 .write( colnames( element$table )[c], file );
2831 .write( .tag( "/th" ), file );
2832 }
2833
2834 .write( .tag( "/tr" ), file );
2835 .write( .tag( "/thead" ), file );
2836
2837
2838 # --- table body ---
2839
2840 # copy original table and perform trimming to significant digits
2841 trimmedTable <- 0;
2842
2843 if ( is.null( ( element$tableSignificantDigits ) ) )
2844 {
2845 trimmedTable <- .trimTable( element$table, TABLE.SIGNIFICANT.DIGITS );
2846 }
2847 else
2848 {
2849 if ( is.na( element$tableSignificantDigits ) )
2850 {
2851 trimmedTable <- .trimTable( element$table, TABLE.SIGNIFICANT.DIGITS );
2852 }
2853 else
2854 {
2855 trimmedTable <- .trimTable( element$table, element$tableSignificantDigits );
2856 }
2857 }
2858
2859 .write( .tag( "tbody" ), file );
2860
2861 for( r in 1:dim( element$table )[1] )
2862 {
2863 .write( .tag( "tr" ), file );
2864
2865 for( c in 1:dim( element$table )[2] )
2866 {
2867 .write( .tag( "td" ), file );
2868
2869 resultIndex <- element$resultIndices[r,c];
2870
2871 if ( !is.na( resultIndex ) )
2872 {
2873 result <- element$resultList[[resultIndex]];
2874
2875 if ( result$isSignificant )
2876 {
2877 if ( length( result$elements ) == 0 )
2878 {
2879 #.write( .tag( "span", class=c( "result", "significant", "noevidence" ), id=.concat( "resultid_", result$resultId, "_", result$resultNumber ) ), file );
2880 .write( .tag( "span", class=c( "result", "significant", "noevidence" ) ), file );
2881 }
2882 else
2883 {
2884 .write( .tag( "span", class=c( "result", "significant" ), id=.concat( "resultid_", result$resultId ) ), file );
2885 }
2886 }
2887 else
2888 {
2889 if ( length( result$elements ) == 0 )
2890 {
2891 #.write( .tag( "span", class=c( "result", "noevidence" ), id=.concat( "resultid_", result$resultId, "_", result$resultNumber ) ), file );
2892 .write( .tag( "span", class=c( "result", "noevidence" ) ), file );
2893 }
2894 else
2895 {
2896 .write( .tag( "span", class=c( "result" ), id=.concat( "resultid_", result$resultId ) ), file );
2897 }
2898 }
2899 }
2900
2901 #.write( as.character( element$table[r,c] ), file );
2902 #.write( as.character( trimmedTable[r,c] ), file );
2903 .write( format( trimmedTable[r,c], scientific=1 ), file );
2904
2905 if ( !is.na( resultIndex ) )
2906 {
2907 .write( .tag( "/span" ), file );
2908 }
2909
2910 .write( .tag( "/td" ), file );
2911 }
2912
2913 .write( .tag( "/tr" ), file );
2914 }
2915
2916 .write( .tag( "/tbody" ), file );
2917
2918 .write( .tag( "/table" ), file ); # resultstable
2919 }
2920
2921 .write( .tag( "/div" ), file ); # table
2922
2923 # write all elements, i.e. results only in this case
2924 .writeElements( element, file );
2925 }
2926
2927
2928
2929 #' Create a citation element.
2930 #' @param authors Names of authors.
2931 #' @param title Title of the document.
2932 #' @param publication Name of the publication where the document appeared.
2933 #' @param issue Issue of the publication.
2934 #' @param number Number of the publication.
2935 #' @param pages Pages of the document in the publication.
2936 #' @param year Year when the document was published.
2937 #' @param url URL of the document.
2938 #' @export
2939 #' @return New element.
2940 #'
2941 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2942 newCitation <- function( authors="", title, publication="", issue="", number="", pages="", year="", url="" )
2943 {
2944 element <- .newElement( .ELEMENT.CITATION, niceType="Citation" );
2945
2946 element$authors <- authors;
2947 element$year <- year;
2948 element$title <- title;
2949 element$publication <- publication;
2950 element$publicationIssue <- issue;
2951 element$publicationNumber <- number;
2952 element$pages <- pages;
2953 element$url <- url;
2954
2955 return ( element );
2956 }
2957
2958
2959 #' Create a citation element that represents a document published in a journal. This is a convenience wrapper for \code{newCitation}.
2960 #' @param authors Names of authors.
2961 #' @param title Title of the document.
2962 #' @param publication Name of the publication where the document appeared.
2963 #' @param issue Issue of the publication.
2964 #' @param number Number of the publication.
2965 #' @param pages Pages of the document in the publication.
2966 #' @param year Year when the document was published.
2967 #' @param url URL of the document.
2968 #' @export
2969 #' @return New element.
2970 #'
2971 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2972 newJournalCitation <- function( authors, title, publication, issue, number, pages, year, url="" )
2973 {
2974 return ( newCitation( authors=authors, title=title, publication=publication, issue=issue, number=number, pages=pages, year=year, url=url ) );
2975 }
2976
2977
2978 #' Create a citation element that represents a document published online. This is a convenience wrapper for \code{newCitation}.
2979 #' @param authors Names of authors.
2980 #' @param title Title of the document.
2981 #' @param url URL of the document.
2982 #' @export
2983 #' @return New element.
2984 #'
2985 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
2986 newWebCitation <- function( authors, title, url )
2987 {
2988 return ( newCitation( authors=authors, title=title, url=url ) );
2989 }
2990
2991
2992 .writeCitation <- function( element, file )
2993 {
2994 .write( .tag( "div", class="reference", id=.concat( "reference_", element$number ) ), file );
2995
2996 .write( "[", element$number, "] ", nobreak=TRUE, file );
2997
2998 if ( element$authors != "" )
2999 {
3000 .write( element$authors, ", ", nobreak=TRUE, file );
3001 }
3002
3003 if ( element$title != "" ) # only required field
3004 {
3005 .write( asLink( url=element$url, element$title ), nobreak=TRUE, file );
3006 }
3007
3008 if ( element$publication != "" )
3009 {
3010 .write( ", ", asEmph( element$publication ), nobreak=TRUE, file );
3011 }
3012
3013 if ( element$publication != "" && element$publicationIssue != "" )
3014 {
3015 .write( " ", asStrong( element$publicationIssue ), nobreak=TRUE, file );
3016 }
3017
3018 if ( element$publication != "" && element$publicationIssue != "" && element$publicationNumber != "" )
3019 {
3020 .write( "(", element$publicationNumber, ")", nobreak=TRUE, file );
3021 }
3022
3023 if ( element$pages != "" )
3024 {
3025 .write( ":", element$pages, nobreak=TRUE, file );
3026 }
3027
3028 if ( element$year != "" )
3029 {
3030 .write( " (",element$year, ")", nobreak=TRUE, file );
3031 }
3032
3033 .write( .tag( "/div" ), file ); # reference
3034 }
3035
3036
3037 #' Create a new paragraph element.
3038 #' @param ... Strings that will be concatenated to form the text of the paragraph.
3039 #' @param exportId Unique string to identify this element. Used to retrieve the element using \code{getExportedElement}.
3040 #' @param protection Procection level. One of PROTECTION.PUBLIC, PROTECTION.GROUP, PROTECTION.PRIVATE.
3041 #' @export
3042 #' @return New element.
3043 #'
3044 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
3045 newParagraph <- function( ..., exportId=NULL, protection=PROTECTION.PUBLIC )
3046 {
3047 element <- .newElement( .ELEMENT.PARAGRAPH, exportId=exportId, protection=protection );
3048
3049 element$text <- list( ... );
3050
3051 return ( element );
3052 }
3053
3054
3055 #' Create a new freeform HTML element. THIS MUST BE USED WITH EXTRAORDINARTY CARE!
3056 #' @param ... Strings that will be concatenated to form the HTML content that will be wrapped into a \code{div} element.
3057 #' @param style CSS to be applied to the \code{div} element.
3058 #' @param exportId Unique string to identify this element. Used to retrieve the element using \code{getExportedElement}.
3059 #' @param protection Procection level. One of PROTECTION.PUBLIC, PROTECTION.GROUP, PROTECTION.PRIVATE.
3060 #' @export
3061 #' @return New element.
3062 #'
3063 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
3064 newHtml <- function( ..., style=NULL, exportId=NULL, protection=PROTECTION.PUBLIC )
3065 {
3066 element <- .newElement( .ELEMENT.HTML, exportId=exportId, protection=protection );
3067
3068 element$text <- list( ... );
3069 element$style <- style;
3070
3071 return ( element );
3072 }
3073
3074
3075
3076 #' Create a new parameter list element. A parameter list is an unnumbered list of the form param_1 = value_1, ..., param_n = value_n where param_i is formated as a parameter and value_i is formatted as a value.
3077 #' @param ... 2n strings that will be concatenated to form the parameter-value pairs. Strings at positions 1, ..., 2n - 1 will be treated as parameters 1 through n and strings at positions 2, ..., 2n will be treated as values 1 through n.
3078 #' @param separator String to be used to separate parameters and values. Whitespace characters need to be supplied if required.
3079 #' @param exportId Unique string to identify this element. Used to retrieve the element using \code{getExportedElement}.
3080 #' @param protection Procection level. One of PROTECTION.PUBLIC, PROTECTION.GROUP, PROTECTION.PRIVATE.
3081 #' @export
3082 #' @return New element.
3083 #'
3084 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
3085 newParameterList <- function( ..., separator=" = ", exportId=NULL, protection=PROTECTION.PUBLIC )
3086 {
3087 element <- newList( isNumbered=FALSE, exportId=exportId, protection=protection );
3088
3089 args <- list( ... );
3090
3091 # if the only argument passed is a list then use it as the list of arguments
3092 if ( ( length( args ) == 1 ) && ( is.list( args[[1]] ) ) )
3093 {
3094 args <- args[[1]];
3095 }
3096
3097 if ( length( args ) < 2 )
3098 {
3099 stop( "At least one parameter-value pair has to be provided to parameter list." );
3100 }
3101
3102 if ( length( args ) %% 2 != 0 )
3103 {
3104 stop( "Number of elements in parameter list is not multiple of 2." );
3105 }
3106
3107 j <- 1;
3108
3109 for ( i in 1:(length( args )/2) )
3110 {
3111 element <- addTo( parent=element, newParagraph( args[[j]], separator, args[[j+1]] ) );
3112
3113 j <- j + 2;
3114 }
3115
3116 return ( element );
3117 }
3118
3119
3120 .writeParagraph <- function ( element, file )
3121 {
3122 .write( .tag( "p" ), file );
3123 .write( element$text, file );
3124
3125 .writeElements( element, file );
3126
3127 .write( .tag( "/p" ), file );
3128 }
3129
3130
3131 .writeHtml <- function ( element, file )
3132 {
3133 .write( .tag( .concat( "div class=\"freeform\" style=\"", element$style,"\"" ) ), file );
3134 .write( element$text, file );
3135
3136 .writeElements( element, file );
3137
3138 .write( .tag( "/div" ), file );
3139 }
3140
3141
3142
3143 #' Create a new result element.
3144 #' @param ... One or more strings that will be concatenated to form the text associated with the result (often just a scalar or single string).
3145 #' @param isSignificant If TRUE, the result will be declared signficant.
3146 #' @param protection Procection level. One of PROTECTION.PUBLIC, PROTECTION.GROUP, PROTECTION.PRIVATE.
3147 #' @export
3148 #' @return New element.
3149 #'
3150 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
3151 newResult <- function( ..., isSignificant=FALSE, protection=PROTECTION.PUBLIC )
3152 {
3153 element <- .newElement( .ELEMENT.RESULT, .concat( ... ), protection=protection );
3154
3155 element$resultId <- .getNextResultId();
3156 element$isSignificant <- isSignificant;
3157
3158 return ( element );
3159 }
3160
3161
3162 .writeResult <- function ( element, file )
3163 {
3164 # only write non-empty results
3165 if ( length( element$elements ) > 0 )
3166 {
3167 .write( .tag( "div", class="evidence", id=.concat( "evidenceid_", element$resultId ) ), file );
3168
3169 .writeElements( element, file );
3170
3171 .write( .tag( "/div" ), file );
3172 }
3173 }
3174
3175
3176 .writeTopLogos <- function ( report, file )
3177 {
3178 # quit if there are no logos to be added
3179 if ( is.na( getLogo( report, LOGO.TOP.LEFT ) ) &&
3180 is.na( getLogo( report, LOGO.TOP.CENTER ) ) &&
3181 is.na( getLogo( report, LOGO.TOP.RIGHT ) ) )
3182 {
3183 return ();
3184 }
3185
3186 # write top logos
3187 .write( .tag( "div", class="logo" ), file );
3188 .write( .tag( "div", class="top" ), file );
3189
3190 .write( .tag( "div", class="left" ), file );
3191 if ( !is.na( getLogo( report, LOGO.TOP.LEFT ) ) )
3192 {
3193 .write( .tag( "img", other=.concat( "src=\"", getLogo( report, LOGO.TOP.LEFT ), "\"/" ) ), file );
3194 }
3195 .write( .tag( "/div" ), file );
3196
3197 .write( .tag( "div", class="center" ), file );
3198 if ( !is.na( getLogo( report, LOGO.TOP.CENTER ) ) )
3199 {
3200 .write( .tag( "img", other=.concat( "src=\"", getLogo( report, LOGO.TOP.CENTER ), "\"/" ) ), file );
3201 }
3202 .write( .tag( "/div" ), file );
3203
3204 .write( .tag( "div", class="right" ), file );
3205 if ( !is.na( getLogo( report, LOGO.TOP.RIGHT ) ) )
3206 {
3207 .write( .tag( "img", other=.concat( "src=\"", getLogo( report, LOGO.TOP.RIGHT ), "\"/" ) ), file );
3208 }
3209 .write( .tag( "/div" ), file );
3210
3211 .write( .tag( "/div" ), file );
3212 .write( .tag( "/div" ), file );
3213 }
3214
3215
3216 .writeBottomLogos <- function ( report, file )
3217 {
3218 # quit if there are no logos to be added
3219 if ( is.na( getLogo( report, LOGO.BOTTOM.LEFT ) ) &&
3220 is.na( getLogo( report, LOGO.BOTTOM.CENTER ) ) &&
3221 is.na( getLogo( report, LOGO.BOTTOM.RIGHT ) ) )
3222 {
3223 return ();
3224 }
3225
3226
3227 # write bottom logos
3228 .write( .tag( "div", class="logo" ), file );
3229 .write( .tag( "div", class="bottom" ), file );
3230
3231 .write( .tag( "div", class="left" ), file );
3232 if ( !is.na( getLogo( report, LOGO.BOTTOM.LEFT ) ) )
3233 {
3234 .write( .tag( "img", other=.concat( "src=\"", getLogo( report, LOGO.BOTTOM.LEFT ), "\"/" ) ), file );
3235 }
3236 .write( .tag( "/div" ), file );
3237
3238 .write( .tag( "div", class="center" ), file );
3239 if ( !is.na( getLogo( report, LOGO.BOTTOM.CENTER ) ) )
3240 {
3241 .write( .tag( "img", other=.concat( "src=\"", getLogo( report, LOGO.BOTTOM.CENTER ), "\"/" ) ), file );
3242 }
3243 .write( .tag( "/div" ), file );
3244
3245 .write( .tag( "div", class="right" ), file );
3246 if ( !is.na( getLogo( report, LOGO.BOTTOM.RIGHT ) ) )
3247 {
3248 .write( .tag( "img", other=.concat( "src=\"", getLogo( report, LOGO.BOTTOM.RIGHT ), "\"/" ) ), file );
3249 }
3250 .write( .tag( "/div" ), file );
3251
3252 .write( .tag( "/div" ), file );
3253 .write( .tag( "/div" ), file );
3254 }
3255
3256
3257 #' Add child elements to a parent element.
3258 #' @param parent Parent element.
3259 #' @param ... One or more child elements.
3260 #' @param row If parent element is a table row and column indices must be provided to add supplementary results to cell in the table.
3261 #' @param column If parent element is a table row and column indices must be provided to add supplementary results to cell in the table.
3262 #' @export
3263 #' @return Updated parent element.
3264 #'
3265 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
3266 addTo <- function( parent, ..., row=NA, column=NA )
3267 {
3268 args <- list( ... );
3269
3270 if ( parent$type == .ELEMENT.TABLE )
3271 {
3272 if ( !missing( row ) && !missing( column ) )
3273 {
3274 if ( length( args ) == 1 )
3275 {
3276 if ( args[[1]]$type == .ELEMENT.RESULT )
3277 {
3278 parent <- .addResult( table=parent, row=row, column=column, result=args[[1]] );
3279
3280 return ( parent );
3281 }
3282 }
3283 }
3284 }
3285
3286
3287 if ( length( args ) > 0 )
3288 {
3289 for ( i in 1:length( args ) )
3290 {
3291 parent$elements[[length( parent$elements ) + 1]] <- args[[i]];
3292 }
3293 }
3294
3295 return ( parent );
3296 }
3297
3298
3299 .addResult <- function( table, row, column, result )
3300 {
3301 # !!! do not change the order of the following two lines!!!
3302 table$resultList[[length( table$resultList ) + 1]] <- result;
3303 table$resultIndices[row,column] = length( table$resultList );
3304
3305 # add result right after table
3306 table$elements[[length( table$elements ) + 1]] <- result;
3307
3308 return ( table );
3309 }
3310
3311
3312 #' Reference a citation, figure or table element.
3313 #' @param element Citation, figure or table element.
3314 #' @export
3315 #' @return A reference string for the referenced element that will be resolved when the report is written to file and rendered to HTML.
3316 #'
3317 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
3318 asReference <- function( element )
3319 {
3320 # TODO: create link if requested
3321 # TODO: check for type!
3322 return ( paste( .REFERENCE.STRING, element$id, sep="" ) );
3323 }
3324
3325
3326 #' Include a result in text. This is a legacy method and provided only for backwards compatibility.
3327 #' @param result The result element.
3328 #' @export
3329 #' @return The result element.
3330 #'
3331 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
3332 asSummary <- function( result )
3333 {
3334 return ( result );
3335 }
3336
3337
3338 #' Format text as a hyperlink.
3339 #' @param url URL to be for the link.
3340 #' @param ... One or more strings that will be concatenated to form the text of the link.
3341 #' @export
3342 #' @return A hyperlink.
3343 #'
3344 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
3345 asLink <- function( url, ... )
3346 {
3347 # TODO: open link in new window if requested
3348
3349 args <- list( ... );
3350
3351 if ( missing( url ) && length( args ) == 0 )
3352 {
3353 stop( "Unable to create link without URL or link text." );
3354 }
3355
3356 if ( url == "" || missing( url ) )
3357 {
3358 return ( .concat( args ) ); # just print text but do not create a link
3359 }
3360
3361 if ( length( args ) == 0 )
3362 {
3363 args <- url; # print URL as link text
3364 }
3365
3366 return ( .concat( .tag( "a", other=.concat( "href=\"", url, "\"" ) ), args, .tag( "/a" ) ) );
3367 }
3368
3369
3370 #asIdentifier <- function( label, class=NA, column=NA, row=NA )
3371 #{
3372 # if ( missing( class ) && missing( label ) )
3373 # {
3374 # stop( "Unable to create identifier without class or label." );
3375 # }
3376 #
3377 # if ( missing( class ) )
3378 # {
3379 # return ( label ); # just print text
3380 # }
3381 #
3382 # return ( .concat( "<span class=\"identifier ", class, "\">", label, "</span>" ) );
3383 #}
3384
3385
3386 #' Format text with strong emphasis (usually resulting in text set in bold).
3387 #' @param ... One or more strings that will be concatenated.
3388 #' @export
3389 #' @return Text with strong emphasis.
3390 #'
3391 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
3392 asStrong <- function( ... )
3393 {
3394 return ( .concat( .tag( "strong" ), ..., .tag( "/strong" ) ) );
3395 }
3396
3397
3398 #' Format text with emphasis (usually resulting in text set in italics).
3399 #' @param ... One or more strings that will be concatenated.
3400 #' @export
3401 #' @return Text with emphasis.
3402 #'
3403 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
3404 asEmph <- function( ... )
3405 {
3406 return ( .concat( .tag( "em" ), ..., .tag( "/em" ) ) );
3407 }
3408
3409
3410 #' Format text as parameter.
3411 #' @param ... One or more strings that will be concatenated.
3412 #' @export
3413 #' @return Text formatted as a parameter.
3414 #'
3415 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
3416 asParameter <- function( ... )
3417 {
3418 return ( .concat( .tag( "span", class="parameter" ), ..., .tag( "/span" ) ) );
3419 }
3420
3421
3422 #' Format text as value.
3423 #' @param ... One or more strings that will be concatenated.
3424 #' @export
3425 #' @return Text formatted as a value.
3426 #'
3427 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
3428 asValue <- function( ... )
3429 {
3430 return ( .concat( .tag( "span", class="value" ), ..., .tag( "/span" ) ) );
3431 }
3432
3433
3434 #' Format text as filename.
3435 #' @param ... One or more strings that will be concatenated.
3436 #' @export
3437 #' @return Text formatted as a filename.
3438 #'
3439 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
3440 asFilename <- function( ... )
3441 {
3442 return ( .concat( .tag( "span", class="filename" ), ..., .tag( "/span" ) ) );
3443 }
3444
3445
3446 #' Format text as code.
3447 #' @param ... One or more strings that will be concatenated.
3448 #' @export
3449 #' @return Text formatted as a code.
3450 #'
3451 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
3452 asCode <- function( ... )
3453 {
3454 return ( .concat( .tag( "span", class="code" ), ..., .tag( "/span" ) ) );
3455 }
3456
3457
3458 .concat <- function( ... )
3459 {
3460 if ( length( list( ... ) ) == 0 )
3461 {
3462 return ( "" );
3463 }
3464
3465 return ( paste( ..., sep="" ) );
3466 }
3467
3468
3469 # helper function: writes a line of html code
3470 .write <- function( ..., nobreak=FALSE, file )
3471 {
3472 if ( missing( file ) )
3473 {
3474 args <- list( ... );
3475 file <- args[[length( args )]];
3476 args[[length( args )]] <- "";
3477 }
3478
3479 # string to be written to file
3480 string <- paste( args, sep="" );
3481
3482 # if the report is supposed to be used as a fragment, all ids have to be made unique
3483 # scan text for this pattern "< ...id="xyz" ...>"
3484 # replace with "< ...id="xyz_REPORTID" ...>"
3485 # sed s/"\(<.*\)id=\"\([A-Za-z][A-Za-z0-9_\.-\:]*\)\"\(.*>\)"/\\1id=\\2_TEXT\\3/g demo.html
3486 if ( .getGlobalReportId() != "" )
3487 {
3488 string <- gsub( "(<.*)id=\"([A-Za-z][A-Za-z0-9_\\.-\\:]*)\"(.*>)", .concat( "\\1id=\"\\2_", .getGlobalReportId(), "\"\\3" ), string );
3489 }
3490
3491 if ( nobreak )
3492 {
3493 cat( string, file=file, sep="" );
3494 }
3495 else
3496 {
3497 cat( string, "\n", file=file, sep="" );
3498 }
3499 }
3500
3501
3502 .tag <- function( tag, id=NA, class=NA, other=NA )
3503 {
3504 idString <- "";
3505 classString <- "";
3506 otherString <- "";
3507
3508 if ( !missing( id ) )
3509 {
3510 idString <- .concat( " id=\"", id, "\"" );
3511 }
3512
3513 if ( !missing( class ) )
3514 {
3515 classString <- .concat( " class=\"", paste( class, sep=" ", collapse=" " ), "\"" );
3516 }
3517
3518 if ( !missing( other ) )
3519 {
3520 otherString <- .concat( " ", other );
3521 }
3522
3523 return ( .concat( "<", tag, classString, idString, otherString, ">" ) );
3524 }
3525
3526 .comment <- function( ... )
3527 {
3528 return ( .concat( "<!-- ", ..., " -->" ) );
3529 }
3530
3531
3532
3533 .writeElement <- function( element, file )
3534 {
3535 if ( element$type == .ELEMENT.PARAGRAPH )
3536 {
3537 .writeParagraph( element, file );
3538 }
3539
3540 if ( element$type == .ELEMENT.HTML )
3541 {
3542 .writeHtml( element, file );
3543 }
3544
3545 if ( element$type == .ELEMENT.SECTION )
3546 {
3547 .writeSection( element, file, "" );
3548 }
3549
3550 if ( element$type == .ELEMENT.SUBSECTION )
3551 {
3552 .writeSection( element, file, "sub" );
3553 }
3554
3555 if ( element$type == .ELEMENT.SUBSUBSECTION )
3556 {
3557 .writeSection( element, file, "subsub" );
3558 }
3559
3560 if ( element$type == .ELEMENT.LIST )
3561 {
3562 .writeList( element, file );
3563 }
3564
3565 if ( element$type == .ELEMENT.FIGURE )
3566 {
3567 .writeFigure( element, file );
3568 }
3569
3570 if ( element$type == .ELEMENT.TABLE )
3571 {
3572 .writeTable( element, file );
3573 }
3574
3575 if ( element$type == .ELEMENT.CITATION )
3576 {
3577 .writeCitation( element, file );
3578 }
3579
3580 if ( element$type == .ELEMENT.RESULT )
3581 {
3582 .writeResult( element, file );
3583 }
3584 }
3585
3586
3587 .writeElements <- function( element, file )
3588 {
3589 if ( length( element$elements ) > 0 )
3590 {
3591 for ( i in 1:length( element$elements ) )
3592 {
3593 .writeElement( element$elements[[i]], file );
3594 }
3595 }
3596 }
3597
3598
3599
3600
3601
3602 #' Get the first element of the "Summary" subsection in the "Overview" section in a standard report.
3603 #' @param report The report.
3604 #' @export
3605 #' @return A report element.
3606 #'
3607 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
3608 getSummary <- function( report )
3609 {
3610 .initNumbers();
3611
3612 return ( .updateReferences( .updateNumberedElements( .getElementById( report, report$predefined$summaryId ) ), report=report )$elements );
3613 }
3614
3615
3616 #' Get the total number of significant results in \code{report}.
3617 #' @param report The report.
3618 #' @export
3619 #' @return Number of signficiant results.
3620 #'
3621 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
3622 getSignificantResultsCount <- function( report )
3623 {
3624 return ( .countSignificantResults( report ) );
3625 }
3626
3627
3628 #' Write \code{report} to file.
3629 #' @param report The report to be written.
3630 #' @param filename Name of the output file without file extension.
3631 #' @param debug If TRUE, external CSS (\code{debugCss}) and JavaScript (\code{debugJavaScript}) can be supplied.
3632 #' @param output A list of output formats. Any combination of HTML.REPORT and RDATA.REPORT is allowed.
3633 #' @param credits If TRUE, a note and a link will be included at the bottom of the report to indicate that it was created with Nozzle.
3634 #' @param level The protection level of the report. If set to PROTECTION.PUBLIC only elements with protection level PROTECTION.PUBLIC (default) will be included in the report. If set to PROTECTION.GROUP, then all elements with protection level PROTECTION.PUBLIC and PROTECTION.GROUP will be included. If set to PROTECTION.PRIVATE all elements will be included.
3635 #' @param debugCss A path or URL to a CSS file that should be used instead of the built-in CSS. Only used if \code{debug} is TRUE.
3636 #' @param debugJavaScript A path or URL to a JavaScript file that should be used instead of the built-in JavaScript. Only used if \code{debug} is TRUE.
3637 #' @export
3638 #'
3639 #' @author Nils Gehlenborg \email{nils@@hms.harvard.edu}
3640 writeReport <- function( report, filename=DEFAULT.REPORT.FILENAME, debug=FALSE, output=c( HTML.REPORT, RDATA.REPORT ), credits=TRUE, level=PROTECTION.PUBLIC, debugCss=NA, debugJavaScript=NA )
3641 {
3642 # check if input and references contain any reports, if not, remove those sections
3643
3644 if ( is.list( report$predefined ) && ( "referencesId" %in% names( report$predefined ) ) )
3645 {
3646 references <- .getElementById( report, report$predefined$referencesId );
3647
3648 if ( !is.null( references ) )
3649 {
3650 # remove reference section
3651 report <- .removeElementById( report, references$id );
3652
3653 if ( length( references$elements ) > 0 )
3654 {
3655 # there are some references, insert them at the end of the methods section
3656 report <- addToMethods( report, references );
3657 }
3658 }
3659 }
3660
3661 if ( is.list( report$predefined ) && ( "inputId" %in% names( report$predefined ) ) ) {
3662 input <- .getElementById( report, report$predefined$inputId );
3663
3664 if ( !is.null( input ) )
3665 {
3666 if ( length( input$elements ) == 0 )
3667 {
3668 # no input, remove the empty section
3669 report <- .removeElementById( report, input$id );
3670 }
3671 }
3672 }
3673
3674 if ( is.list( report$predefined ) && ( "metaId" %in% names( report$predefined ) ) )
3675 {
3676 meta <- .getElementById( report, report$predefined$metaId );
3677
3678 if ( !is.null( meta ) )
3679 {
3680 if ( length( meta$elements ) == 0 )
3681 {
3682 # no meta information, remove the empty section
3683 report <- .removeElementById( report, meta$id );
3684 }
3685 }
3686 }
3687
3688 # --- additional meta information --- #
3689
3690 # assign report id
3691 report$reportId <- .newReportId();
3692
3693 # assign date of report writing (if doesn't exist yet)
3694 if ( is.null( report$meta$createDate ) )
3695 {
3696 report$meta$createDate <- date();
3697 }
3698
3699 # write as requested output types
3700 if ( RDATA.REPORT %in% output )
3701 {
3702 save( report, file=.concat( filename, ".RData" ) );
3703 }
3704
3705 if ( HTML.REPORT %in% output )
3706 {
3707 .writeGeneralReport( report, .concat( filename, ".html" ), debug, HTML.REPORT, FALSE, credits=credits, level=level, debugCss=debugCss, debugJavaScript=debugJavaScript );
3708 }
3709
3710 if ( HTML.FRAGMENT %in% output )
3711 {
3712 warning( "HTML fragments are no longer supported in Nozzle version 0.3.0 or later." );
3713 #.writeGeneralReport( report, .concat( filename, ".html" ), debug, HTML.FRAGMENT, FALSE, level=level, debugCss=debugCss, debugJavaScript=debugJavaScript );
3714 }
3715 }
3716
3717
3718 .writeGeneralReport <- function( report, filename, debug=FALSE, output=HTML.REPORT, append=FALSE, credits=TRUE, level=PROTECTION.PUBLIC, debugCss=NA, debugJavaScript=NA )
3719 {
3720 # initialize counters for all numbered objects (figures, tables, references, etc.)
3721 .initNumbers();
3722
3723 report <- .removeProtectedElements( report, level );
3724
3725 # extract result elements (e.g. from paragraphs) and insert into the report tree
3726 report <- .updateResults( report, report=report )$element; # if there are results directly in the report they will be ignored
3727 report <- .updateNumberedElements( report );
3728 report <- .updateReferences( report, report=report );
3729
3730 file <- NA;
3731
3732 if ( append )
3733 {
3734 file <- file( filename, "a" );
3735 }
3736 else
3737 {
3738 file <- file( filename, "w" );
3739 }
3740
3741 # write file header if in debug mode or if writing standalone report
3742 if ( output == HTML.REPORT )
3743 {
3744 .write( "<!DOCTYPE html>", file );
3745
3746 .write( .tag( "html" ), file );
3747
3748 .write( .tag( "head" ), file );
3749
3750 # write report title
3751 .write( .tag( "title" ), file );
3752 .write( report$title, file );
3753 if ( "subTitle" %in% names( report ) && !is.null( report$subTitle ) && !is.na( report$subTitle ) )
3754 {
3755 .write( " - ", report$subTitle, file );
3756 }
3757 .write( .tag( "/title" ), file );
3758
3759 # embedded Google Analytics JavaScript if a Google Analytics id has been provided (should be last before </head>)(
3760 if ( !is.na( getGoogleAnalyticsId( report ) ) )
3761 {
3762 .write( "<script type=\"text/javascript\">", file );
3763 .write( paste( gsub( .NOZZLE.GOOGLEANALYTICS.VARIABLE, getGoogleAnalyticsId( report ), get( "googleAnalyticsCode", envir=.nozzleEnv ) ), collapse="\n" ), file );
3764 .write( "</script>", file );
3765 }
3766
3767 .write( .tag( "/head" ), file );
3768
3769 .write( .tag( "body" ), file );
3770
3771 # insert mask to make report loading appear more gracefully
3772 # the mask covers the view and hides possible ugly artefacts that might appear during loading before
3773 # all divs have been resized to their correct size
3774 .write( "<div style=\"display: none\" id=\"mask\">&nbsp;</div>", file );
3775 .write( "<script type=\"text/javascript\">", file );
3776 .write( "document.getElementById(\"mask\").style.display = \"block\";", file );
3777 .write( "</script>", file );
3778 }
3779
3780 .write( .tag( "div", class="report", id=report$reportId ), file );
3781
3782 .write( .comment( "Nozzle Report Id = ", report$reportId ), file );
3783 .write( .comment( "Nozzle Report Version = ", report$meta$version ), file );
3784
3785 .write( .comment( "Nozzle Report Create Package = ", getCreatorName( report ) ), file );
3786 .write( .comment( "Nozzle Report Create Date = ", getCreatorDate( report ) ), file );
3787
3788 report$meta$renderer$name <- .getPackageVersion();
3789 report$meta$renderer$date <- date();
3790
3791 .write( .comment( "Nozzle Report Render Package = ", getRendererName( report ) ), file );
3792 .write( .comment( "Nozzle Report Render Date = ", getRendererDate( report ) ), file );
3793
3794 # add further meta information
3795 report$meta$reportTitle <- report$title;
3796 report$meta$reportId <- report$reportId;
3797
3798
3799 # insert Nozzle CSS
3800 if ( debug )
3801 {
3802 if ( is.na( debugCss ) )
3803 {
3804 .write( .tag( "style media=\"screen\"" ), file );
3805 .write( paste( get( "cssCode", envir=.nozzleEnv ), collapse="\n" ), file );
3806 .write( .tag( "/style" ), file );
3807
3808 .write( .tag( "style media=\"print\"" ), file );
3809 .write( paste( get( "cssPrintCode", envir=.nozzleEnv ), collapse="\n" ), file );
3810 .write( .tag( "/style" ), file );
3811 }
3812 else
3813 {
3814 .write( "<link type=\"text/css\" rel=\"stylesheet\" href=\"", debugCss, "\">", file );
3815 }
3816 }
3817 else
3818 {
3819 .write( .tag( "style media=\"screen\"" ), file );
3820 .write( paste( get( "cssMinCode", envir=.nozzleEnv ), collapse="\n" ), file );
3821 .write( .tag( "/style" ), file );
3822
3823 .write( .tag( "style media=\"print\"" ), file );
3824 .write( paste( get( "cssPrintMinCode", envir=.nozzleEnv ), collapse="\n" ), file );
3825 .write( .tag( "/style" ), file );
3826 }
3827
3828 if ( !is.na( getCustomScreenCss( report ) ) )
3829 {
3830 .write( "<link type=\"text/css\" rel=\"stylesheet\" media=\"screen\" href=\"", getCustomScreenCss( report ), "\">", file );
3831 }
3832
3833 if ( !is.na( getCustomPrintCss( report ) ) )
3834 {
3835 .write( "<link type=\"text/css\" rel=\"stylesheet\" media=\"print\" href=\"", getCustomPrintCss( report ), "\">", file );
3836 }
3837
3838 # embedded jQuery JavaScript (only in standalone)
3839 if ( output == HTML.REPORT )
3840 {
3841 #.write( "<script type=\"text/javascript\" src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js\"></script>", file );
3842 .write( "<script type=\"text/javascript\">", file );
3843 .write( paste( get( "jQueryMinCode", envir=.nozzleEnv ), collapse="\n" ), file );
3844 .write( "</script>", file );
3845 }
3846
3847 # embedded TableSorter JavaScript
3848 .write( "<script type=\"text/javascript\">", file );
3849 if ( debug )
3850 {
3851 .write( paste( get( "jQueryTableSorterMinCode", envir=.nozzleEnv ), collapse="\n" ), file );
3852 }
3853 else
3854 {
3855 .write( paste( get( "jQueryTableSorterMinCode", envir=.nozzleEnv ), collapse="\n" ), file );
3856 }
3857 .write( "</script>", file );
3858
3859
3860 # embedded Nozzle JavaScript
3861 if ( debug )
3862 {
3863 if ( is.na( debugJavaScript ) )
3864 {
3865 .write( "<script type=\"text/javascript\">", file );
3866 .write( paste( get( "javaScriptCode", envir=.nozzleEnv ), collapse="\n" ), file );
3867 .write( "</script>", file );
3868 }
3869 else
3870 {
3871 .write( "<script type=\"text/javascript\" src=\"", debugJavaScript ,"\"></script>", file );
3872 }
3873 }
3874 else
3875 {
3876 .write( "<script type=\"text/javascript\">", file );
3877 .write( paste( get( "javaScriptMinCode", envir=.nozzleEnv ), collapse="\n" ), file );
3878 .write( "</script>", file );
3879 }
3880
3881
3882 if ( ( output == HTML.REPORT ) || ( append == TRUE ) )
3883 {
3884 .write( "<script type=\"text/javascript\">$(document).ready( function() { initNozzle( \"", report$reportId, "\", $ ); });</script>", file );
3885 }
3886 else
3887 {
3888 # warning!
3889 }
3890
3891
3892 # === REPORT META BEGIN ===
3893
3894 .write( "<script type=\"text/javascript\">", file );
3895 .writeJsonElement( "nozzleMeta", report$meta, file );
3896 .writeJsonElement( "nozzleNavigation", report$navigation, file );
3897 .write( "</script>", file );
3898
3899 # === REPORT META END ===
3900
3901
3902 # === REPORT CONTENT BEGIN ====
3903
3904 .write( .tag( "div", class="frame" ), file );
3905 .write( .tag( "div", class="main" ), file );
3906
3907 # set global report id if this is being written as a fragment
3908 # if set to anything else but "", .write will attach this to all ids in HTML tags
3909 .setGlobalReportId( report$reportId );
3910
3911 # write top logos
3912 .writeTopLogos( report, file );
3913
3914 # write title
3915 .write( .tag( "div", class="title" ), report$title, .tag( "/div" ), file );
3916
3917 if ( !is.null( report$subTitle ) && !is.na( report$subTitle ) )
3918 {
3919 .write( .tag( "div", class="subtitle" ), report$subTitle, .tag( "/div" ), file );
3920 }
3921
3922 # write maintainer info
3923 .writeMaintainerInformation( report, file );
3924
3925 # write DOI info
3926 .writeDoiCitationInformation( report, file );
3927
3928
3929 # write content
3930 if ( length( report$elements ) > 0 )
3931 {
3932 for ( i in 1:length( report$elements ) )
3933 {
3934 .writeElement( report$elements[[i]], file );
3935 }
3936 }
3937
3938 # write copyright info
3939 if ( !is.na( getCopyrightOwner( report ) ) )
3940 {
3941 .write( .tag( "div", class="copyright" ), file );
3942 .write( .tag( "span", class="notice" ), file );
3943
3944 .write( "Copyright &copy; ", getCopyrightYear( report ), file );
3945
3946 .write( .tag( "span", class="owner" ), file );
3947 if ( !is.na( getCopyrightUrl( report ) ) )
3948 {
3949 .write( asLink( getCopyrightOwner( report ), url=getCopyrightUrl( report ) ), ".", file );
3950 }
3951 else
3952 {
3953 .write( getCopyrightOwner( report ), ".", file );
3954 }
3955 .write( .tag( "/span" ), file );
3956
3957 if ( !is.na( getCopyrightStatement( report ) ) )
3958 {
3959 .write( getCopyrightStatement( report ), file );
3960 }
3961
3962 .write( .tag( "/span" ), file );
3963 .write( .tag( "/div" ), file );
3964 }
3965
3966 # write bottom logos
3967 .writeBottomLogos( report, file );
3968
3969
3970 # write credits (something like "MADE WITH NOZZLE")
3971 if ( credits )
3972 {
3973 .write( .tag( "div", class="credits" ), file );
3974 .write( .tag( "span", class="notice" ), file );
3975
3976 .write( asLink( .NOZZLE.CREDITS, url=.NOZZLE.URL ), file );
3977
3978 .write( .tag( "/span" ), file );
3979 .write( .tag( "/div" ), file );
3980 }
3981
3982
3983 .write( .tag( "/div" ), file ); # main
3984 .write( .tag( "/div" ), file ); # frame
3985 .write( .tag( "/div" ), file ); # report
3986
3987 # === REPORT CONTENT END ====
3988
3989
3990 # write file footer if in debug mode or standalone report
3991 if ( output == HTML.REPORT )
3992 {
3993 .write( .tag( "/body" ), file );
3994
3995 .write( .tag( "/html" ), file );
3996 }
3997
3998
3999 # reset global report id if this is being written as a fragment to avoid conflicts with other reports
4000 .setGlobalReportId( "" );
4001
4002 close( file );
4003 }
4004
4005
4006 # writes a meta entry into the header into file unless the value is NA with write.na == FALSE
4007 .writeMetaInformation <- function( attribute, value, file, write.na=FALSE )
4008 {
4009 if ( !is.na( value ) || write.na )
4010 {
4011 .write( .tag( "meta", other=.concat( "attr=\"", attribute, "\" value=\"", value, "\"" ) ), file );
4012 .write( .tag( "/meta" ), file );
4013 }
4014 }
4015
4016
4017 .writeMaintainerInformation <- function( report, file, cssClass="maintainer" )
4018 {
4019 # write maintainer information
4020 if ( !is.na( getMaintainerName( report ) ) )
4021 {
4022 .write( .tag( "div", class=cssClass ), file );
4023
4024 if ( !is.na( getMaintainerEmail( report ) ) )
4025 {
4026 .write( "<i>Maintained by</i> ", .tag( "span", class="name" ), asLink( url=.concat( "mailto:", getMaintainerEmail( report ) ), getMaintainerName( report ) ), .tag( "/span" ), file );
4027 }
4028 else
4029 {
4030 .write( "<i>Maintained by</i> ", .tag( "span", class="name" ), getMaintainerName( report ), .tag( "/span" ), file );
4031 }
4032
4033 if ( !is.na( getMaintainerAffiliation( report ) ) )
4034 {
4035 .write( " (", .tag( "span", class="affiliation" ), getMaintainerAffiliation( report ), .tag( "/span" ), ")", file );
4036 }
4037
4038 .write( .tag( "/div" ), file );
4039 }
4040 }
4041
4042
4043 .writeDoiCitationInformation <- function( report, file, cssClass="citation" )
4044 {
4045 # write DOI and citation info
4046 if ( !is.na( getDoi( report ) ) )
4047 {
4048 .write( .tag( "div", class=cssClass ), file );
4049
4050 doiResolver <- getDoiResolver( report );
4051 if ( is.na( doiResolver ) )
4052 {
4053 doiResolver <- DEFAULT.DOI.RESOLVER;
4054 }
4055
4056 .write( "<i>Cite as</i> ", file );
4057
4058 if ( !is.na( getDoiCreator( report ) ) )
4059 {
4060 .write( .concat( getDoiCreator( report ), "" ), file );
4061 }
4062
4063 if ( !is.na( getDoiYear( report ) ) )
4064 {
4065 .write( .concat( " (", getDoiYear( report ), "): " ), file );
4066 }
4067
4068 if ( !is.na( getDoiTitle( report ) ) )
4069 {
4070 .write( .concat( "", getDoiTitle( report ), ". " ), file );
4071 }
4072
4073 if ( !is.na( getDoiVersion( report ) ) )
4074 {
4075 .write( .concat( getDoiVersion( report ), ". " ), file );
4076 }
4077
4078 if ( !is.na( getDoiPublisher( report ) ) )
4079 {
4080 .write( .concat( getDoiPublisher( report ), ". " ), file );
4081 }
4082
4083 .write( asLink( url=.concat( doiResolver, "/", getDoi( report ) ), .concat( "doi:", getDoi( report ) ) ), file )
4084
4085 .write( .tag( "/div" ), file );
4086 }
4087 }
4088
4089
4090 #===================================================================================================
4091 # functions to write R lists a JSON objects
4092 #===================================================================================================
4093
4094 .writeJsonElement <- function( jsVariable, element, file )
4095 {
4096 .write( "var ", jsVariable, " = {", file );
4097
4098 .writeJsonList( NA, element, file, indent=3 );
4099
4100 .write( "};", file );
4101 }
4102
4103
4104 .writeJsonNumeric <- function( name, value, file, indent=0 )
4105 {
4106 if ( length( value ) == 1 )
4107 {
4108 .write( .createIndent( indent ), "\"", name, "\": ", value, file, nobreak=T );
4109 }
4110 else
4111 {
4112 .write( .createIndent( indent ), "\"", name, "\": [", paste( value, collapse=", " ), "]", file, nobreak=T );
4113 }
4114 }
4115
4116
4117 .writeJsonCharacter <- function( name, value, file, indent=0 )
4118 {
4119 if ( length( value ) == 1 )
4120 {
4121 .write( .createIndent( indent ), "\"", name, "\": ", "\"", value, "\"", file, nobreak=T );
4122 }
4123 else
4124 {
4125 .write( .createIndent( indent ), "\"", name, "\": [", paste( paste( "\"", value, "\"", sep="" ), collapse=", " ), "]", file, nobreak=T );
4126 }
4127 }
4128
4129
4130 .writeJsonNull <- function( name, file, indent=0 )
4131 {
4132 .write( .createIndent( indent ), "\"", name, "\": ", "null", file, nobreak=T );
4133 }
4134
4135
4136 .writeJsonList <- function( name, element, file, indent=0 )
4137 {
4138 if ( !is.na( name ) )
4139 {
4140 .write( .createIndent( indent ), "\"", name, "\": {", file );
4141 }
4142
4143 # get names
4144 listNames <- names( element );
4145
4146 # iterate over names and write out values
4147 if ( length( listNames ) > 0 )
4148 {
4149 for ( i in 1:length( listNames ) )
4150 {
4151 value <- element[[listNames[i]]];
4152
4153 if ( is.list( value ) )
4154 {
4155 .writeJsonList( listNames[i], value, file, indent=indent+3 );
4156 }
4157 else if ( is.na( value ) )
4158 {
4159 .writeJsonNull( listNames[i], file, indent=indent+3 );
4160 }
4161 else if ( is.numeric( value ) )
4162 {
4163 .writeJsonNumeric( listNames[i], value, file, indent=indent+3 );
4164 }
4165 else
4166 {
4167 .writeJsonCharacter( listNames[i], value, file, indent=indent+3 );
4168 }
4169
4170 if ( i < length( listNames ) )
4171 {
4172 .write( ",", file );
4173 }
4174 else
4175 {
4176 .write( "\n", file, nobreak=T );
4177 }
4178 }
4179 }
4180
4181 # closing braces
4182 if ( !is.na( name ) )
4183 {
4184 .write( .createIndent( indent ), "}", file, nobreak=T );
4185 }
4186 }
4187
4188 .createIndent <- function( indent, character=" " )
4189 {
4190 return( paste( rep( character, indent ), collapse="" ) );
4191 }