1

I am increasingly editing SO posts. I guess you all know those cases when the OP tries to produce a MCVE and dputs their large sample data frames into the question - taking many many lines of code, although they could be formatted to one line. Removing the white space would usually do the job and increase the readability of the post, without damaging its reproducibility.
But, I have now often enough stupidly deleted this manually and just have no clue how to do this better. I don't see how to get the whole block evaluated as a string, in order to use "conventional" 'replace whitespace in string', because the block contains many quotation marks which would need escaping.

An example (with a comparatively small data frame):

structure(list(a = 1:5, b = structure(1:5, .Label = c("a", "b", 
"c", "d", "e"), class = "factor")), class = "data.frame", row.names = c(NA, 
-5L))

should be reduced to :

structure(list(a=1:5,b=structure(1:5,.Label=c("a","b","c","d","e"),class="factor")),class="data.frame",row.names=c(NA,-5L))
tjebo
  • 21,977
  • 7
  • 58
  • 94

1 Answers1

1

This is how I go about it, making use of the great styler package. First: copy the dput and use the "Style Selection" option under the Addins menu in RStudio. This gives:

structure(list(a = 1:5, b = structure(1:5, .Label = c(
  "a", "b",
  "c", "d", "e"
), class = "factor")), class = "data.frame", row.names = c(
  NA,
  -5L
))

Then, we can use Alt + drag on the left edge of the script and then press Backspace to delete the line breaks, leaving this (note the spurious spaces left by the auto-indentation)

structure(list(a = 1:5, b = structure(1:5, .Label = c(  "a", "b",  "c", "d", "e"), class = "factor")), class = "data.frame", row.names = c(  NA,  -5L))

Then, "Style Selection" one more time to make whitespace consistent:

structure(list(a = 1:5, b = structure(1:5, .Label = c("a", "b", "c", "d", "e"), class = "factor")), class = "data.frame", row.names = c(NA, -5L))

Overall, given a dput this usually takes only a few seconds. The main limitation is that this will not work with truly gigantic dput outputs, but by that point it is really far from anything resembling a minimal reprex anyway.

Calum You
  • 14,687
  • 4
  • 23
  • 42
  • awesome. That's a cool feature. I will wait for some other thoughts, but I guess this one is hard to beat. – tjebo Nov 12 '18 at 23:30