Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ys_start_yaml() function #135

Open
andreashandel opened this issue Jun 27, 2022 · 3 comments
Open

ys_start_yaml() function #135

andreashandel opened this issue Jun 27, 2022 · 3 comments

Comments

@andreashandel
Copy link

andreashandel commented Jun 27, 2022

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)
  2. save created text as yml file in specified or default location
  3. optionally, use file.edit(newyaml.yml) to open the yml file in the editor for the user to further edit.
    }
@andreashandel
Copy link
Author

andreashandel commented Jul 25, 2022

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
Copy link
Contributor

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
Copy link
Author

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants