From a4483c84e6786a7cf859235e8fdacb89b29ec36d Mon Sep 17 00:00:00 2001 From: marcelcosta Date: Fri, 11 Oct 2024 10:42:29 +0200 Subject: [PATCH] Adding md2csv and tab2md functions. --- R/md2csv.R | 7 +++++++ R/tab2md.R | 10 ++++++++++ man/md2csv.Rd | 28 ++++++++++++++++++++++++++++ man/tab2md.Rd | 17 +++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 R/md2csv.R create mode 100644 R/tab2md.R create mode 100644 man/md2csv.Rd create mode 100644 man/tab2md.Rd diff --git a/R/md2csv.R b/R/md2csv.R new file mode 100644 index 0000000..0bb1087 --- /dev/null +++ b/R/md2csv.R @@ -0,0 +1,7 @@ +md2csv<-function(text, rm_blank=T){ + text<-strsplit(text, "\n")[[1]] + text<-gsub("^[ |]*|[|][ ]*$","",text) + if(rm_blank){text<-gsub(" ","",text)} + text<-gsub("[|]",",",text) + return(cat(text,sep = "\n")) +} diff --git a/R/tab2md.R b/R/tab2md.R new file mode 100644 index 0000000..a0276fc --- /dev/null +++ b/R/tab2md.R @@ -0,0 +1,10 @@ +tab2md<-function(table){ + samples_csv<-table + table<-apply(table, 1, paste, collapse="|") + table<-gsub("^","|",table) + table<-gsub("$","|",table) + table<-c(paste0("|",paste(colnames(samples_csv), collapse="|"),"|"), + paste0("|",paste(rep("---",ncol(samples_csv)), collapse="|"),"|"), + table) + cat(table, sep="\n") +} diff --git a/man/md2csv.Rd b/man/md2csv.Rd new file mode 100644 index 0000000..b4fefef --- /dev/null +++ b/man/md2csv.Rd @@ -0,0 +1,28 @@ +\name{md2csv} +\alias{md2csv} +\title{md2csv} +\usage{ +md2csv(text, rm_blank =T) +} +\arguments{ + \item{text}{An md text that you want to convert to csv. Note that you must eliminate the line that separates title from the rest.} + \item{text}{Bolean to specify if you want to remove spaces or not.} +} +\description{ +This function converts an Markdown (md) table into csv format. +} +\examples{ +text<-"|Letter|Number| +|A| 1| +|B| 2| +|C| 3| +|D| 4| +|E| 5| +|F| 6| +|G| 7| +|H| 8| +|I| 9| +|J|10|" +md2csv(text) + +} diff --git a/man/tab2md.Rd b/man/tab2md.Rd new file mode 100644 index 0000000..c0e6d59 --- /dev/null +++ b/man/tab2md.Rd @@ -0,0 +1,17 @@ +\name{tab2md} +\alias{tab2md} +\title{tab2md} +\usage{ +tab2md(text) +} +\arguments{ + \item{text}{A data.frame that you want to convert to Markdown.} +} +\description{ +This function converts a data.frame into Markdown (md) format. +} +\examples{ +table<-data.frame("Letter"=LETTERS[1:10], "Number"=1:10) +tab2md(table) + +}