R/makeShiny.R

Defines functions makeShiny

Documented in makeShiny

#' Generate a shiny app to explore process data interactively
#'
#' This function generates a customized shiny app for interactive exploration of the processed and annotated screen data
#'
#' @param screenData a data frame that contains the processed screen data generated by \code{readScreen()} function
#' @param sampleAnnotations a vector of character strings, specifying the columns that should be used as sample annotations.
#' The information in those columns can be used to label samples in visualization or to perform association test.
#' @export
#' @import dplyr
#' @return This function creates a folder named 'shiny' in the working direction.
#' In the 'shiny' folder, 'app.R' is the R script for the shiny app and 'shinyData.RData' contains the processed data for the shiny app.
#' @examples
#' # load processed data
#' data(screenData_normalized)
#'
#' # make html report
#' makeShiny(screenData_normalized)
#'

makeShiny <- function(screenData, sampleAnnotations = c("sampleID", "patientID")) {
    dir.create("./shiny", showWarnings = FALSE)
    # add file name column
    if (!"fileName" %in% sampleAnnotations)
        sampleAnnotations <- c(sampleAnnotations, "fileName")

    # check if sample annotations are in the data
    notFound <- sampleAnnotations[!sampleAnnotations %in% colnames(screenData)]
    if (length(notFound != 0)) {
        stop(paste0(paste0(notFound, collapse = ","),
                    " not found in the column names of the screen data"))
    }

    # change character values to factors, for filtering
    screenData <- mutate_if(screenData, is.character, as.factor)

    # change batch to factor
    if ("batch" %in% colnames(screenData)) {
        screenData <- dplyr::mutate(screenData, batch = as.factor(batch))
    } else screenData <- dplyr::mutate(screenData, batch = as.factor(0))

    # calculate AUC using linear-log trapezoidal method
    if ("normVal" %in% colnames(screenData) & !"meanViab" %in% colnames(screenData)) {
        summariseScreen(screenData, method = "average")
    }


    # remove group
    screenData <- ungroup(screenData)

    save(screenData, sampleAnnotations, file = "./shiny/shinyData.RData")

    # template file
    tempShiny <- system.file("app.R", package = "DrugScreenExplorer")
    file.copy(tempShiny, "./shiny/app.R", overwrite = TRUE)

}
lujunyan1118/DrugScreenExplorer_dev documentation built on Dec. 21, 2021, 12:42 p.m.