Search code examples
yamlrstudionewlineline-breaksswirl

Adding new line/line break while writing swirl lesson "lesson.yaml" with Class "text" in RStudio


I am writing a swirl lesson using swirlify package functions in RStudio.
Below is how lesson.yaml file looks like now

- Class: text
  Output: Welcome to Part 1 Playing with Numbers!!!

Output for which looks like

enter image description here

How to insert a new line or line break after Welcome to Part 1 in lesson.yaml file above, so that it displays the output as below when I run the demo_lesson() command again after saving the lesson.yaml file

| Welcome to Part 1

| Playing with Numbers!!!


Solution

  • Using YAML, you can use any of these equivalent approaches:

    • Quoted string with escape
    - Class: text
      Output: "Welcome to Part 1\nPlaying with Numbers!!!"
    
    • Literal scalar
    - Class: text
      Output: |-
        Welcome to Part 1
        Playing with Numbers!!!
    

    (| starts a literal scalar and - tells YAML to drop the final line break.)

    • Multiline scalar
    - Class: text
      Output:
        Welcome to Part 1
    
        Playing with Numbers!!!
    

    (since one line break gets folded into a space, you need two line breaks.)

    Since I do not know whether swirlify nicely handles line breaks in the string, I guess you could also do

    - Class: text
      Output: Welcome to Part 1
    - Class: text
      Output: Playing with Numbers!!!