diff --git a/NAMESPACE b/NAMESPACE index c77cc4188e7108eb63fec131c181016a1122914a..2678e118fb789afb33a456d724cdfa8009767e1b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -31,6 +31,7 @@ export(plot_struct_nowrap) export(prescreen) export(read_rt) export(report) +export(report_old) export(rev2list) export(rt_input) export(run) diff --git a/R/api.R b/R/api.R index f049139c3df1afbfed171c69aa3baadd5d55c2c1..9c62f734c0df5f7f5dca4fbb23df147a08ad50ea 100644 --- a/R/api.R +++ b/R/api.R @@ -606,7 +606,7 @@ create_plots <- function(m) { } #' @export -report <- function(m) { +report_old <- function(m) { figtopdir <- FIG_TOPDIR #file.path(m$conf$project,FIG_TOPDIR) pander::evalsOptions("graph.output","pdf") author <- if (!is.null(m$conf$report$author)) m$conf$report$author else REPORT_AUTHOR @@ -685,3 +685,87 @@ app <- function(shiny_args=list(launch.browser=F),render_args=NULL,indir=getwd() file.copy(system.file(file.path("rmd","app.Rmd"),package = "shinyscreen"),"app_run.Rmd") rmarkdown::run(file = "app_run.Rmd", shiny_args = shiny_args, render_args = render_args) } + + +#' @export +#' @title report +report <- function(m) { + report_author <- if (!is.null(m$conf$report$author)) m$conf$report$author else REPORT_AUTHOR + report_title <- if (!is.null(m$conf$report$title)) m$conf$report$title else REPORT_TITLE + fn_header <- system.file(file.path('rmd','report_header.rmd'),package = "shinyscreen") + fn_chunk <- system.file(file.path('rmd','report_chunk.rmd'), package = "shinyscreen") + dir.create(REP_TOPDIR,recursive = T,showWarnings = F) + header <- knitr::knit_expand(fn_header) + flt_summ <- m$out$tab$flt_summ + ms2 <- m$extr$ms2 + ms1 <- m$extr$ms1 + rt_min <- rt_in_min(m$conf$figures$rt_min) + rt_max <- rt_in_min(m$conf$figures$rt_max) + keytab <- flt_summ[,unique(.SD),.SDcol=c("adduct","ID")] + + repdoc <- header + for (n in 1:NROW(keytab)) { + select <- dtable(adduct=keytab$adduct[[n]], + ID=keytab$ID[[n]]) + + pdata_ms1 <- data4plot_ms1_cgram(ms1,select) + pdata_ms2 <- data4plot_ms2_cgram(ms2,select) + pdata_spec <- data4plot_ms2_spec(ms2,flt_summ,select) + + tabl_ms1 <- table_eic(pdata_ms1) + ## tabl_ms2 <- table_eic(pdata_ms2) # For the moment, no real + # info to be had here. + tabl_spec <- table_spec(pdata_spec) + + palette = plot_palette(pdata_ms1) + + + p_eic <- plot_eic_w_facet(pdata_ms1 = pdata_ms1, + pdata_ms2 = pdata_ms2, + rt_range = c(rt_min,rt_max), + palette = palette) + ggplot2::theme(legend.position = "bottom") + p_spec <- plot_spec_w_facet(pdata_ms2 = pdata_spec, + mz_range = c(NA_real_,NA_real_), + palette = palette) + ggplot2::theme(legend.position = "bottom") + eic_things <- plot_save_single(p_eic, + decotab = select, + figtag = "eic", + proj = m$conf$project, + tabl = tabl_ms1, + subdir = REP_TOPDIR, + extension = m$conf$figures$ext) + + + spec_things <- plot_save_single(p_spec, + decotab = select, + figtag = "spec", + proj = m$conf$project, + tabl = tabl_spec, + subdir = REP_TOPDIR, + extension = m$conf$figures$ext) + + report_chunk_header <- paste0("Adduct: ",keytab$adduct[[n]],"; ", + "ID: ",keytab$ID[[n]]) + + + report_tab_eic_cap <- sprintf("EIC for %s",keytab$ID[[n]]) + report_fn_eic <- eic_things$fn_plot + report_tab_eic <- if (shiny::isTruthy(eic_things$tab)) knitr::kable(eic_things$tab, caption = report_tab_eic_cap) else "" + + report_tab_spec_cap <- sprintf("Spectrum for %s",keytab$ID[[n]]) + report_fn_spec <- spec_things$fn_plot + report_tab_spec <- if (shiny::isTruthy(eic_things$tab)) knitr::kable(spec_things$tab, caption = report_tab_spec_cap) else "" + + repdoc <- c(repdoc,knitr::knit_expand(fn_chunk)) + message("(report) Knitting of chunk ",n," out of ",NROW(keytab)," has been completed.") + + } + fn_rep <- file.path(m$conf$project,"report.Rmd") + message("(report) Writing Rmd...") + cat(repdoc,file=fn_rep,sep = "\n") + message("(report) ...done.") + message("(report) Render start ...") + rmarkdown::render(fn_rep,output_dir = m$conf$project) + message("(report) ...done.") + m +} diff --git a/R/plotting.R b/R/plotting.R index 2ad3c3ab1e7a27e334f5bc3e2430e1d3e73e8011..0696f8a2b5550bc9cbe5871a7d198c33f6b8fc73 100644 --- a/R/plotting.R +++ b/R/plotting.R @@ -296,7 +296,7 @@ table_spec <- function(pdata) { tbl } -plot_fname_prefix <- function(decotab,proj) { +plot_fname_prefix <- function(decotab,proj,subdir=FIG_TOPDIR) { if (NROW(decotab)==0) return() adducts <- decotab[,adduct] ids <- decotab[,ID] @@ -308,7 +308,7 @@ plot_fname_prefix <- function(decotab,proj) { fname <- paste(fname,chunk,sep = "_") } - ddir <- file.path(proj,FIG_TOPDIR) + ddir <- file.path(proj,subdir) if (!dir.exists(ddir)) dir.create(ddir,recursive = T) fname <- paste0(fname,"__id_") @@ -319,20 +319,20 @@ plot_fname_prefix <- function(decotab,proj) { } -plot_save_single <- function(plot,decotab,extension,proj,tabl=NULL,figtag="") { +plot_save_single <- function(plot,decotab,extension,proj,subdir=FIG_TOPDIR,tabl=NULL,figtag="") { if (is.null(plot)) return() - - fname <- plot_fname_prefix(decotab,proj) + + fname <- plot_fname_prefix(decotab,proj,subdir=subdir) fnplot <- paste0(fname,"__",figtag,".",extension) - + if (extension == "rds" || extension == "RDS") { saveRDS(plot,file=fnplot) } else ggplot2::ggsave(filename = fnplot, plot = plot) - fntab <- paste0(fname,"__",figtag,".csv") if (! is.null(tabl)) data.table::fwrite(tabl,file=fntab,sep = ",") - - + list(plot=plot,tab=tabl,fn_plot=fnplot) + } + diff --git a/R/resources.R b/R/resources.R index ec273b882943bd79d09fabfeb81364983b5397c8..c11825cbbfb8b15513c8e2efcbcfe8b5a254dfa4 100644 --- a/R/resources.R +++ b/R/resources.R @@ -184,6 +184,7 @@ DEF_INDEX_SUMM <- c("set", "-qa_pass", "-ms1_int", "adduct","-mz") ## Top-level directory to store the figures FIG_TOPDIR <- "figures" +REP_TOPDIR <- "report" ## Figure filter FIG_DEF_FILTER <- "" diff --git a/inst/rmd/report_chunk.rmd b/inst/rmd/report_chunk.rmd new file mode 100644 index 0000000000000000000000000000000000000000..201f66394f95a24397819cef3c32c729262371e1 --- /dev/null +++ b/inst/rmd/report_chunk.rmd @@ -0,0 +1,36 @@ +## {{report_chunk_header}} + +### EICs + +```{r, echo=F} + +if (nchar("{{report_fn_eic}}")!=0) { + knitr::include_graphics("{{report_fn_eic}}") +} else { + "No good EIC found." +} + + +``` + +{{report_tab_eic}} + + + +### MS2 Spectrum + +```{r, echo=F} + +if (nchar("{{report_fn_spec}}")!=0) { + knitr::include_graphics("{{report_fn_spec}}") +} else { + "No good MS2 spectrum found." +} + +``` + + +{{report_tab_spec}} + + +\newpage diff --git a/inst/rmd/report_header.rmd b/inst/rmd/report_header.rmd new file mode 100644 index 0000000000000000000000000000000000000000..8cf1521ca8f8e1cfea9e6292732db88fb3edff21 --- /dev/null +++ b/inst/rmd/report_header.rmd @@ -0,0 +1,13 @@ +--- +output: + pdf_document: + extra_dependencies: ["float"] + +author: "{{report_author}}" +title: "{{report_title}}" +geometry: margin=0.5cm +--- + +```{r, echo=F} +knitr::opts_chunk$set(fig.pos = "!H", out.extra = "") +``` diff --git a/man/report.Rd b/man/report.Rd new file mode 100644 index 0000000000000000000000000000000000000000..6e1d8a76a5e1edd67b0fedbd06546cecf9c816a4 --- /dev/null +++ b/man/report.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/api.R +\name{report} +\alias{report} +\title{report} +\usage{ +report(m) +} +\description{ +report +}