R/rmdtext.R

Defines functions render_function_file render_taxon_file render_proteinGroups_file render_peptides_file render_MQsummary_file render_MQsummary render_rmd_server render_rmd_url rmdtext

Documented in render_MQsummary render_rmd_server render_rmd_url rmdtext

#' wrapper function of rmarkdown::render for opencpu
#'
#' Three general wrapper function to work with opencpu, which can render source r markdown code into html files, to be displayed on the client end
#'
#' @param text For rmdtext, a string, which is the rmardown coding itself
#' @return no direct return, but write an output.html to the temp session on the opencpu server
#'
#' @seealso \code{\link{render}}  \code{\link{knit}}
#'
#' @examples
#'   path_to_input <- system.file("rmd", rmd_file, package = "rmdocpu")
#'   myfile <- readLines(path_to_input)
#'   rmdtext(myfile)

#' @export
#'
#'
#'
rmdtext <- function(text){
  writeLines(text, con="input.Rmd");
  rmarkdown::render("input.Rmd", output_file="output.html");
  invisible();
}


#' wrapper function of rmarkdown::render for opencpu
#'
#' Three general wrapper function to work with opencpu, which can render source r markdown code into html files, to be displayed on the client end
#'
#' @param rmd_url For render_rmd_url, a string of public accessible url
#'
#' @return no direct return, but write an output.html to the temp session on the opencpu server
#' @seealso \code{\link{render}}  \code{\link{knit}}
#' @examples
#'   render_rmd_url("https://raw.githubusercontent.com/ningzhibin/rmdocpu/master/inst/rmd/input.Rmd")
#' @export
#'
#'
#'


render_rmd_url <- function(rmd_url){
  ## for test rmd_url https://raw.githubusercontent.com/ningzhibin/rmdocpu/master/inst/rmd/input.Rmd
  myfile <- RCurl::getURL(rmd_url)
  writeLines(myfile, con="input.Rmd");
  rmarkdown::render("input.Rmd",output_format = "html_document", output_file="output.html")

  invisible()
}




#' wrapper function of rmarkdown::render for opencpu
#'
#' Three general wrapper function to work with opencpu, which can render source r markdown code into html files, to be displayed on the client end
#'
#' @param rmd_file For render_rmd_server, a string of the file name on the same opencpu server in the same package, only for tests
#' @return no direct return, but write an output.html to the temp session on the opencpu server
#' @seealso \code{\link{render}}  \code{\link{knit}}
#' @examples
#'   path_to_input <- system.file("rmd", rmd_file, package = "rmdocpu")
#'   render_rmd_server(path_to_input)
#' @export
#'

render_rmd_server <- function(rmd_file){
  ## for test use input.Rmd
  path_to_input <- system.file("rmd", rmd_file, package = "rmdocpu")

  myfile <- readLines(path_to_input)
  writeLines(myfile, con="input.Rmd");
  rmarkdown::render("input.Rmd",output_format = "html_document", output_file="output.html")

  invisible()
}










#' wrapper function of rmarkdown::render for opencpu to render a report for maxquant summary
#'
#' This function works on the opencpu server end, to produce an html file to send to the front end to display.
#' It also works on the stand alone mode, to generate a report file, output.html in the currrent folder, using a public accessible rmd file on github. Of course, this need internect connection to run
#'
#'
#' @param data_table the table is a matrix/data.frame/tble on the server ond, json formted on the front end opencpu will do the conversion automatically
#'
#' @return no direct return, but write an output.html to the temp session on the opencpu server
#' @seealso \code{\link{render}}  \code{\link{knit}} \code{\link{render_MQsummary_file}}
#' @examples
#'   summary_table  <- read_tsv("22122017_Colon_Aging_summary.txt", col_names = TRUE)
#'   render_MQsummary(summary_table)
#' @export
#'
#'
#'




render_MQsummary <- function(data_table){
  myfile <- RCurl::getURL("https://raw.githubusercontent.com/ningzhibin/rmdocpu/master/inst/rmd/MQ_report_summary.Rmd")
  writeLines(myfile, con="input.Rmd");

  rmarkdown::render("input.Rmd",output_format = "html_document", params = list(summary_file_tbl =  data_table), output_file="output.html")

  invisible()
}


#' wrapper function of rmarkdown::render for opencpu to render a report for maxquant summary
#'
#' This function works on the opencpu server end, to produce an html file to send to the front end to display.
#' It also works on the stand alone mode, to generate a report file, output.html in the currrent folder, using a public accessible rmd file on github. Of course, this need internect connection to run
#'
#'
#' @param file the file (path), manditory, string is the tsv file, summary.txt
#' @param meta the file (path), optional,string is the tsv file, generated by MetaLab2.0, 1st columns as sample name, 2nd column as experiment name, 3rd column and after as grouping
#'
#' @return no direct return, but write an output.html to the temp session on the opencpu server
#' @seealso \code{\link{render}}  \code{\link{knit}} \code{\link{render_MQsummary}}
#' @examples
#'   # render_MQsummary_file(file ="summary1_simple.txt")
#    # render_MQsummary_file(file ="summary1_simple.txt", meta = "summary1_meta.txt" )
#' @export
#'
#'
#'



render_MQsummary_file <- function(file, meta = NULL, output_format = "html_document"){

  data_table <- read.delim(file, header = TRUE,check.names = FALSE, stringsAsFactors = FALSE)

  myfile <- RCurl::getURL("https://gitlab.com/iMetaLab/rmdocpu/-/raw/master/MQ_report_summary.Rmd")
  writeLines(myfile, con="input.Rmd");

  if(!is.null(meta)){
    meta_table <- read.delim(meta, header = TRUE, check.names = FALSE, stringsAsFactors = FALSE) # with meta file
    rmarkdown::render("input.Rmd",output_format = output_format, params = list(summary_file_tbl =  data_table, meta_table = meta_table), output_file="output.html")

  }else{
    rmarkdown::render("input.Rmd",output_format = output_format, params = list(summary_file_tbl =  data_table), output_file="output.html")
  }

  invisible()
}



#' wrapper function of rmarkdown::render for opencpu to render a report for peptides.txt
#'
#' This function works on the opencpu server end, to produce an html file, which can then be retrieved by javascript or any other customer api tool.
#' It also works on the stand alone mode, to generate a report file, output.html in the currrent folder, using a public accessible rmd file on github.
#' Of course, this need internect connection to run
#'
#'
#' @param file the file (path), manditory, string is the tsv file, has to be peptide files
#' @param meta the file (path), optional, string is the tsv file, generated by MetaLab2.0, 1st columns as sample name, 2nd column as experiment name, 3rd column and after as grouping
#'
#' @return no direct return, but write an output.html to the temp session on the opencpu server
#' @seealso \code{\link{render}}  \code{\link{knit}} \code{\link{render_MQsummary}} \code{\link{render_MQsummary_file}} \code{\link{render_proteinGroups_file}}
#' @examples
#'   # render_peptides_file(file ="peptides3_simple.txt")
#    # render_peptides_file(file ="peptides3_simple.txt", meta = "peptides3_meta.txt" )
#' @export
#'
#'
#'

render_peptides_file <- function(file, meta = NULL){

  data_table  <- read.delim(file, header = TRUE,check.names = FALSE, stringsAsFactors = FALSE) # NOTE the read in options

  myfile <- RCurl::getURL("https://gitlab.com/iMetaLab/rmdocpu/-/raw/master/MQ_report_peptides.Rmd")
  writeLines(myfile, con="input.Rmd");

  if(!is.null(meta)){
    meta_table <- read.delim(meta, header = TRUE, check.names = FALSE, stringsAsFactors = FALSE) #with meta file

    rmarkdown::render("input.Rmd",output_format = "html_document", params = list(input_datatable =  data_table, meta_table = meta_table), output_file="output.html")

  }else{
    rmarkdown::render("input.Rmd",output_format = "html_document", params = list(input_datatable =  data_table), output_file="output.html")
  }

  invisible()
}




#' wrapper function of rmarkdown::render for opencpu to render a report for proteinGroup.txt
#'
#' This function works on the opencpu server end, to produce an html file, which can then be retrieved by javascript or any other customer api tool.
#' It also works on the stand alone mode, to generate a report file, output.html in the currrent folder, using a public accessible rmd file on github.
#' Of course, this need internect connection to run
#'
#'
#' @param file the file path, string is the tsv file, proteinGroups.txt
#' @param meta the file (path), optional, string is the tsv file, generated by MetaLab2.0, 1st columns as sample name, 2nd column as experiment name, 3rd column and after as grouping
#'
#' @return no direct return, but write an output.html to the temp session on the opencpu server
#' @seealso \code{\link{render}}  \code{\link{knit}} \code{\link{render_MQsummary}} \code{\link{render_MQsummary_file}} \code{\link{render_peptides_file}}
#' @examples
#'   # render_proteinGroups_file(file ="proteinGroups.txt")
#    # render_proteinGroups_file(file ="proteinGroups.txt", meta = "proteingroups3_meta.txt" )
#' @export
#'
#'
#'

render_proteinGroups_file <- function(file, meta = NULL){

  #data_table  <- readr::read_tsv(file, col_names = TRUE)
  data_table  <- read.delim(file, header = TRUE,check.names = FALSE, stringsAsFactors = FALSE) # NOTE the read in options

  myfile <- RCurl::getURL("https://gitlab.com/iMetaLab/rmdocpu/-/raw/master/MQ_report_proteinGroups.Rmd")
  writeLines(myfile, con="input.Rmd");

  if(!is.null(meta)){
    meta_table <- read.delim(meta, header = TRUE, check.names = FALSE, stringsAsFactors = FALSE) # with meta file

    rmarkdown::render("input.Rmd",output_format = "html_document", params = list(input_datatable =  data_table, meta_table = meta_table), output_file="output.html")

  }else{
    rmarkdown::render("input.Rmd",output_format = "html_document", params = list(input_datatable =  data_table), output_file="output.html")
  }

  invisible()
}



#' wrapper function of rmarkdown::render for opencpu to render a report for MetaLab_taxonomy.xlsx
#'
#' This function works on the opencpu server end, to produce an html file, which can then be retrieved by javascript or any other customer api tool.
#' It also works on the stand alone mode, to generate a report file, output.html in the currrent folder, using a public accessible rmd file on github.
#' Of course, this need internect connection to run
#'
#'
#' @param file the file path, string is the tsv file, taxon.txt
#' @param meta the file (path), optional, string is the tsv file, generated by MetaLab2.0, 1st columns as sample name, 2nd column as experiment name, 3rd column and after as grouping
#'
#' @return no direct return, but write an output.html to the temp session on the opencpu server
#' @seealso \code{\link{render}}  \code{\link{knit}} \code{\link{render_MQsummary}} \code{\link{render_MQsummary_file}} \code{\link{render_proteinGroups_file}}
#' @examples
#'   render_taxon_file(file = "MetaLab_taxonomy.xlsx")
#'   render_taxon_file(file = "MetaLab_taxonomy.xlsx", meta = "meta.txt")
#' @export
#'
#'
#'



render_taxon_file <- function(file,meta = NULL){

  data_table  <-  read.delim(file, header = TRUE,check.names = FALSE,sep = ",", stringsAsFactors = FALSE) # readin from xl file


  myfile <- RCurl::getURL("https://gitlab.com/iMetaLab/rmdocpu/-/raw/master/ML_report_taxonomy.Rmd")
  writeLines(myfile, con="input.Rmd");

  if(!is.null(meta)){
    meta_table <- read.delim(meta, header = TRUE, check.names = FALSE, stringsAsFactors = FALSE) # with meta file
    rmarkdown::render("input.Rmd",output_format = "html_document", params = list(input_datatable =  data_table, meta_table = meta_table), output_file="output.html")

  }else{
    rmarkdown::render("input.Rmd",output_format = "html_document", params = list(input_datatable =  data_table), output_file="output.html")
  }

  invisible()
}






#' wrapper function of rmarkdown::render for opencpu to render a report for function.csv
#'
#' This function works on the opencpu server end, to produce an html file, which can then be retrieved by javascript or any other customer api tool.
#' It also works on the stand alone mode, to generate a report file, output.html in the currrent folder, using a public accessible rmd file on github.
#' Of course, this need internect connection to run
#'
#'
#' @param file the file path, string is the tsv file, function.txt
#' @param meta the file (path), optional, string is the tsv file, generated by MetaLab2.0, 1st columns as sample name, 2nd column as experiment name, 3rd column and after as grouping
#'
#' @return no direct return, but write an output.html to the temp session on the opencpu server
#' @seealso \code{\link{render}}  \code{\link{knit}} \code{\link{render_MQsummary}} \code{\link{render_MQsummary_file}} \code{\link{render_proteinGroups_file}}
#' @examples
#'   render_function_file(file = "function.csv")
#'   render_function_file(file = "function.csv", meta = "meta.txt")
#' @export
#'
#'
#'



render_function_file <- function(file, meta = NULL){

  #data_table  <- readxl::read_excel(file, sheet = 2) # readin from xl file
  data_table <- rio::import(file, check.names = TRUE)

  myfile <- RCurl::getURL("https://gitlab.com/iMetaLab/rmdocpu/-/raw/master/ML_report_function.Rmd")
  writeLines(myfile, con="input.Rmd");

  if(!is.null(meta)){
    meta_table <- read.delim(meta, header = TRUE, check.names = FALSE, stringsAsFactors = FALSE) # with meta file
    rmarkdown::render("input.Rmd",output_format = "html_document", params = list(input_datatable =  data_table, meta_table = meta_table), output_file="output.html")

  }else{
    rmarkdown::render("input.Rmd",output_format = "html_document", params = list(input_datatable =  data_table), output_file="output.html")
  }

  invisible()
}




# the rendering function needs to right a temperary file, whether in the same path as the input, or defined by the user
# if the input is on the server, usually the path is not writable, therefore you need to set the intermediate_dir
# an workround way is to read in the file, and write into a temp file. therefore everything is going to be in the temp file
ningzhibin/rmdocpu documentation built on Feb. 3, 2022, 9:30 p.m.