Skip to content

feature request: raw=TRUE in write_yaml #123

@bschilder

Description

@bschilder

Hello!,

Thanks for the awesome package. I'm currently using it to read/write GitHub Actions (GHA) workflow files in my new R package / GH Action called rworkflows.

I have a scenario where I have a GHA workflow yaml that has the key "on". Thanks to the handler arg I managed to read in the yaml file.

However, I couldn't seem to figure out how to prevent the "on" key being written as 'on': (in single quotes) instead of on: when writing back to disk with write_yaml. This is an issue for GHA as their parser doesn't recognize keys with quotes.

After some playing around, I figured out that the solution was substituting the quoted character and then using file(..., raw=TRUE), but this feature isn't currently exposed to users via write_yaml (as far as I can tell).

Reprex

path <- tempfile(fileext = ".yml")
yml <- yaml::read_yaml("https://raw.githubusercontent.com/neurogenomics/rworkflows/master/inst/yaml/rworkflows_template.yml")

Issue

yaml::write_yaml(yml, path)

yaml file looks like:

name: rworkflows
'on':
  push:
  - master
  - main
  pull_request:
  - master
  - main

Solution

result <- gsub(shQuote("on"),"on",yaml::as.yaml(yml))
path <- file(path, "w", encoding = "UTF-8", raw=TRUE)
open(path, "w")
on.exit(close(path))
cat(result, file = path, sep = "")
name: rworkflows
on:
  push:
  - master
  - main
  pull_request:
  - master
  - main

If it's helpful, I'd be happy to implement the raw arg in write_yaml and make a Pull Request. Does that sound ok to you (maintainer[s])?

Thanks!,
Brian

Session info

``` R version 4.2.1 (2022-06-23) Platform: x86_64-apple-darwin17.0 (64-bit) Running under: macOS Monterey 12.4

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] yaml_2.3.6

loaded via a namespace (and not attached):
[1] compiler_4.2.1 tools_4.2.1

</details>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions