metrumresearchgroup / yspec

Data Specification for Pharmacometrics
https://metrumresearchgroup.github.io/yspec
5 stars 2 forks source link

ys_start_yaml() function #135

Open andreashandel opened 2 years ago

andreashandel commented 2 years ago

A function that takes a data set (as loaded by e.g. read_xpt) and uses the variable names to start a template yaml file with the same file name.

User could optionally provide variable names to only start the template with some variable names.

User could also optionally provide a location for the yaml file (default could be /data/derived).

ys_start_yaml <- function(data, varnames, location) {

  1. take either all variables or supplied variables, write yml file with those variables. Add dummy/template text for user to replace with their own (e.g. the whole SETUP__ section and the short: entries for each variable)
    1. save created text as yml file in specified or default location
    2. optionally, use file.edit(newyaml.yml) to open the yml file in the editor for the user to further edit. }
andreashandel commented 2 years ago

I ended up writing a function that does what I want, works ok for my purposes now. Pasting code below.

@kylebaron if you think that would be a good addition, I'd be happy to turn it into a proper function and submit as PR. Just let me know.

Way to call function:

newyml <- start_yspec(varnames = colnames(rawdat), path = deriveddataDir, name = "ex2datav1.yml") file.edit(newyml)

The function:

# takes a vector of column/variable names 
# and produces a starter yspec YAML file for further editing
start_yspec <- function(varnames, path, name)
{
  # add error checking of inputs here later

  # create full name and path for file to be created
  file_path_and_name <- file.path(path,name)
  # start string at top of YAML
  ys <- "SETUP__:\n    description: yspec starter template\n    sponsor: MetrumRG\n    projectnumber: PROJ123\n"
  # turn each variable name into a YAML entry
  for (n in 1:length(varnames))
  {
    ys <- paste0(ys,varnames[n],":\n  short: ",varnames[n],'\n')
  }
  # once all text is generated, write to YAML file
  sink(file_path_and_name)
  cat(ys)
  sink()
  # return full file name for easy opening/editing by e.g. file.edit()
  return(file_path_and_name)
}
kylebaron commented 2 years ago

Thanks, @andreashandel; we have had lots of people requesting this sort of thing before, but resisted it in favor of a model where the yaml file gets manually created prior to creating the data. This is by design to get the user to interact more carefully with the spec and the data. So far, I think this has been successful. Every once in a while, I've gotten the itch to do this type of thing, but I think we're better off with this more disciplined approach. Thanks for the idea and for taking the time to prototype the code.

Kyle

andreashandel commented 2 years ago

Ok. I find YAML files with their picky indentation rules tricky to work with, so I still believe a very simple skeleton could be a good helper for most folks like me. For now I'll just continue using it for myself. If you ever decide to want one, happy to write and/or test :)