Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6564849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:01:13+00:00 2026-05-25T14:01:13+00:00

This question is related to my question about Roxygen. I want to write a

  • 0

This question is related to my question about Roxygen.

I want to write a new function that does word wrapping of strings, similar to strwrap or stringr::str_wrap, but with the following twist: Any elements (substrings) in the string that are enclosed in quotes must not be allowed to wrap.

So, for example, using the following sample data

test <- "function(x=123456789, y=\"This is a long string argument\")"
cat(test)
function(x=123456789, y="This is a long string argument")

strwrap(test, width=40)
[1] "function(x=123456789, y=\"This is a long"
[2] "string argument\")"      

I want the desired output of a newWrapFunction(x, width=40, ...) to be:

desired <- c("function(x=123456789, ", "y=\"This is a long string argument\")")
desired
[1] "function(x=123456789, "               
[2] "y=\"This is a long string argument\")"

identical(desired, newWrapFunction(tsring, width=40))
[1] TRUE

Can you think of a way to do this?


PS. If you can help me solve this, I will propose this code as a patch to roxygen2. I have identified where this patch should be applied and will acknowledge your contribution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:01:13+00:00Added an answer on May 25, 2026 at 2:01 pm

    Here’s what I did to get strwrap so it would not break single quoted sections on spaces:
    A) Pre-process the “even” sections after splitting by the single-quotes by substituting “~|~” for the spaces:
    Define new function strwrapqt

     ....  
     zz <- strsplit(x, "\'") # will be only working on even numbered sections
       for (i in seq_along(zz) ){ 
           for (evens in seq(2, length(zz[[i]]), by=2)) {
                zz[[i]][evens] <- gsub("[ ]", "~|~", zz[[i]][evens])}
                           }
     zz <- unlist(zz) 
      .... insert just before
     z <- lapply(strsplit) ...........
    

    Then at the end replace all the “~|~” with spaces. It might be necessary to doa lot more thinking about the other sorts of whitespace “events” to get a fully regular treatment.

    ....
     y <- gsub("~\\|~", " ", y)
    ....
    

    Edit: Tested @joran’s suggestion. Matching single and double quotes would be a difficult task with the methods I am using but if one were willing to consider any quote as equally valid as a separator target, one could just use zz <- strsplit(x, "\'|\"") as the splitting criterion in the code above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is related to the post about having abstract at the titlepage. I
This is related to this question I asked earlier about syntax highlighting user-defined blocks
Somewhat related to this question , but in the absence of any answer about
This question is related to (but perhaps not quite the same as): Does Django
This question is related to one of my others about C: What can you
This question is related to a few tools that are returning errors and that
In relation with this question about how to write long SQL queries within C#.,
This question is related to a iOS project written in ObjectiveC that makes use
I have a question which is related to this question that was asked by
This is a question related to getting Drupal CCK fields (just in case that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.