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

Add new str_dedent function #514

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Add new str_dedent function #514

wants to merge 4 commits into from

Conversation

chrimaho
Copy link

One incredibly helpful function in Python is the textwrap.dedent function. Under the hood, this function uses regex to strip any leading spaces, while maintaining any internal indentation within a chunk of code.

This addition here re-implements the same functionality using native R code.

I've ensured to include 4 different unit tests for the same.

@arnaudgallou
Copy link
Contributor

arnaudgallou commented Sep 5, 2024

I'd also like to have a dedent() function in R. Two comments:

  • Wouldn't it make sense to trim leading and trailing whitespaces in the output or add an argument to do so? That would be very useful when making character strings from multi-line text in R:

    str_dedent("
      This is a long sentence that starts on the first line,
      continues on the second,
      and ends on the third one.
    ")

    For readability purposes, it's better to start the text on its own line here. We wouldn't want to keep that blank line at the beginning though. Passing the output to str_trim() every time in that situation would be cumbersome.

  • The function currently doesn't support the following situation (when python's textwrap.dedent() does):

    str_dedent("
        foo
      bar
    ")

    It's probably uncommon enough not to support it but that could be detailed in the function documentation.

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

Successfully merging this pull request may close these issues.

3 participants