![]() ![]() electron (and anything built on top of it) is not “mimimal”. I’d also like to nip something in the bud. There is a lot more potential for your app now that it isn’t encumbered by electron! Someone can turn it into a service or run it on a BSD! If (is.character(mtcars]) || is.It looks like you made a web version! - I wish everyone that made electron “apps” had the foresight to make web version (do they even need to be different repo?!). You can certainly prune some functions, including kable itself which requires functions in other files.Įdit #2: since you said renjin doesn't support *apply functions (a comment suggests that is incorrect, but I'll continue for the sake of argument), here's a for-loop implementation that includes alignment and |-escaping: mtcars$mpg <- "2|1.0" # just a reminder that it's here You can't just grab the single function as it uses several helper functions also in that file. It takes a character matrix, not a ame, so kable_markdown(as.matrix(mtcars)). For that matter, just grab knitr/R/table.R) and use kable_markdown which does the pipe-escaping for you. This doesn't work well when the pipe is within a code block, though a workaround was suggested here: Īt this point, as suggested, you're somewhat reimplementing knitr::kable. # ^^^^^^ this is the pipe, interpreted correctly in markdown You can escape it manually on all character (or add in factors, too) columns: ischar <- vapply(mtcars, is.character, logical(1)) ![]() Ischar <- vapply(mtcars, is.character, logical(1)) # "|:-|-:|-:|-:|-:|-:|-:|-:|-:|-:|-:|-:|"Īnd when you're finally ready to save, use cat(txt2, file = "sometable.md") (or writeLines).Įdit #1: note that the other suggested answers (including mine above) do not address pipe symbols within the content: mtcars$mpg <- "2|1.0" Txt <- capture.output(write.table(mtcars, stdout(), quote = FALSE, sep = "|", row.names = FALSE)) ![]() # TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSEĭashes <- paste(ifelse(ischar, ":-", "-:"), collapse = "|") This uses the alignment row of the markdown table format: (ischar <- vapply(mtcars, is.character, logical(1))) If you are concerned about alignment, you can check for characters (and perhaps others, over to you). # "|rowname|mpg|cyl|disp|hp|drat|wt|qsec|vs|am|gear|carb|" Write.table(mtcars, stdout(), quote = FALSE, sep = "|", row.names = FALSE) Now the work: dashes <- paste(rep("-", ncol(mtcars)), collapse = "|") # rowname mpg cyl disp hp drat wt qsec vs am gear carb ![]() ( Edit #3: I had some small bugs relating to presence of row names, so to simplify things I'm going to remove them completely from the sample data.) mtcars$rowname <- rownames(mtcars) Since you want to put it into markdown, I think it's safe to say that the table size is manageable, so performance is not a factor. Hints for how to write only the header data and the table header separator are also welcome. The Markdown flavour in question looks like: |Header|Header|Header| What is the most efficient way to transmogrify a CSV file into a Markdown format without relying on third-party libraries? Such string manipulation doesn't seem very R-like, though. That is, fix the problems with the output that'll resemble: Geothermal|1250.0|Electricity|0.0| Then capture everything as a string, concatenate a leading pipe, and finally trim the extraneous ending pipe. It should be possible to do something like (note the extra eol pipe): write.table( The output appears as follows: Geothermal|1250.0|Electricity|0.0|īut each line should appear with a | prefix, as follows: |Geothermal|1250.0|Electricity|0.0| Thus for the following: f <- read.csv('planning.csv') The write.table command has an eol option, but no corresponding sol option. The application uses the R engine Renjin, which does not support knitr, kable, or pandoc. The objective is to read from a CSV file and write the contents in a Markdown table format. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |