Open andreashandel opened 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)
}
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
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 :)
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) {