Browse Source

Adding md2csv and tab2md functions.

main
marcelcosta 1 month ago
parent
commit
a4483c84e6
4 changed files with 62 additions and 0 deletions
  1. +7
    -0
      R/md2csv.R
  2. +10
    -0
      R/tab2md.R
  3. +28
    -0
      man/md2csv.Rd
  4. +17
    -0
      man/tab2md.Rd

+ 7
- 0
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"))
}

+ 10
- 0
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")
}

+ 28
- 0
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)
}

+ 17
- 0
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)
}

Loading…
Cancel
Save