spgarbet / tangram

Table Grammar package for R
66 stars 3 forks source link

CSV output and markdown #42

Open kylerove opened 5 years ago

kylerove commented 5 years ago

Is there a way to strip markdown formatting for specific output types. For example, I'm trying to export a table to CSV and import into Excel. Excel does not parse markdown, so the formatting is unnecessary. I was going to try creating my own style but it seems that markdown is applied to this regardless?

spgarbet commented 5 years ago

The contents of the cells are formatted with markdown in their default. It would be possible to strip it on output. Can you provide me a little more detail? Are you using "hmisc" for descriptive statistics?

All of the individual cell rendering functions are registered in a callback table (see R/cell-hmisc.R). It would be trivial to create a "plain_cell" callback that included no markdown whatsoever and specify that in the transform. I added this due to several requests for differing cell formats but using the same analysis, e.g. use commas instead of decimals in formatting or some variant of ratios/fractions.

spgarbet commented 5 years ago

Oh, the callbacks are specific for the transform being used (and one is also free to write their own transforms, but the Hmisc one goes a long way). I've got a new 'smd' one as well that's seeing some use.

kylerove commented 5 years ago

The callbacks are a little confusing to me, but after looking through the vignettes I'm understanding they are linked to the style name. I've been sticking to Hmisc for now. My code looks like this so far:

my_transform <- hmisc

my_transform[['Cell']][['fraction']] <- function(numerator, denominator, format=3, ...) {
  paste0(render_f(100*numerator/denominator, format),'%')
  }

my_transform[['Cell']][['iqr']] <- function(x,format,na.rm,names=TRUE,type=8,msd, quant = c(0.25, 0.75), ...) {
  iqr <- quantile(x, c(0.25, 0.75), na.rm, names, type)
  m <- median(x)
  if(is.na(format)) format <- format_guess(y)
  iqr_formatted <- sapply(iqr, function(x) render_f(x, format))
  paste0(m,' (',paste0(iqr_formatted,collapse="–"),')',collapse='')
  }

my_transform[['Cell']][['p']] <-  function(p, pformat="%1.3f", include_p=TRUE) {
  if(class(pformat) == "function") pformat(p)

  if(is.na(p) || is.nan(p) || p <0 || p>1) return("NA")

  y <- render_f(p, pformat)

  test <- grep("[^0\\.]+", y)
  if(length(test) > 0) {
    if(include_p) paste0("",y) else y
    }
  else {
    if(include_p) paste0("< ", substr(y, 1, nchar(y)-1), "1") else paste0("<", substr(y, 1, nchar(y)-1), "1")
    }
  }
my_transform[['Cell']][['fstat']] <- function(f, df1, df2, p, class=NULL, ...)
{
  cell(p, ..., class=c(class, "statistics"))
}

table1 <- tangram(group ~ 
                   age[1]
                  + followup_days[0]
                  + sex[0]
                  + match_vp_shunt[0]
                  + match_spina_dx[0]
                  + match_past_surgery[0]
                  + revision[0]
                  + ckd[0]
                  + pmh_immunosuppressed[0]
                  + pmh_uti[0]
                  + pmh_antibiotics[0]
                  + neurologic_lesion[0]
                  + neurologic_lesion_level[0]
                  + match_ambulation[0]
                  + asa[0]
                  + diagnosis_1[0], data=allSingle, transform=my_transform)

csv(table1,"table1.csv")
spgarbet commented 5 years ago

That looks correct. If that's working for you I can add it as a "plain" cell style in the main package.

kylerove commented 5 years ago

Yeah this works, just need row headers to be plain and that would be awesome!

Here is what the output of the CSV looks like in my spreadsheet program. It seems like the custom formatted cells are working well and don't have markdown. It's just the headers, which I wasn't sure how to address. (Still have some formatting work to do with stat's function column, but making progress!)

screen shot 2018-09-14 at 10 45 03 am

Thank you for this project. It is going to be a total lifesaver!

spgarbet commented 5 years ago

I haven't looked at the csv code in a long while. It's very much out of date. I think it might be adding all those formatting characters. Give me a few minutes and I may be able to fit this in.

spgarbet commented 5 years ago

Okay, I just deleted a bunch of outdated formatting code for csv. I think this gets rid of the formatting characters you are seeing. Install and let me know how that works for you.

kylerove commented 5 years ago

That's perfect! All the formatting characters are gone now. Thanks again for your responsiveness!

spgarbet commented 5 years ago

I'm going to leave this open and target it for adding a "plain" style option that has no formatting characters at all.