Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So, I very much like Jake and Marek's solutions. Jake's is straightforward, but doesn't address the output formatting part of the problem. Marek's was a bit cumbersome, so I wrapped it up in a function resulting in</p> <pre><code>cleanCode &lt;- function() { if (.Platform$OS.type == "unix" &amp;&amp; .Platform$pkgType == "mac.binary") { to_edit &lt;- readLines(pipe("pbpaste")) # Mac ONLY solution } else { to_edit &lt;- readLines("clipboard") # Windows/Unix solution } opts &lt;- options() cmdPrompts &lt;- paste("^", opts$prompt, "|^", opts$continue, sep="") # can someone help me here? how to escape the + to \\+, as well as other special chars id_commands &lt;- grep("^&gt; |^\\+ ", to_edit) # which are command or continuation lines to_edit[id_commands] &lt;- sub("^&gt; |^\\+ ", "", to_edit[id_commands]) # remove prompts to_edit[-id_commands] &lt;- paste(" # ", to_edit[-id_commands]) # comment output writeLines(to_edit) } </code></pre> <p>which lets me highlight and copy a portion of the interactive session. </p> <p>So, for example, I can use this to copy</p> <pre><code>&gt; x &lt;- rnorm(20) &gt; plot(x) &gt; summary(x) Min. 1st Qu. Median Mean 3rd Qu. Max. -2.34000 -0.86010 -0.21940 -0.43340 0.04383 1.06400 &gt; str(x) num [1:20] -1.568 -0.219 -1.951 1.064 0.768 ... &gt; sd(x) [1] 0.8932958 </code></pre> <p>to the clipboard and with a simple call to</p> <pre><code>&gt; cleanCode() </code></pre> <p>produces output such as</p> <pre><code>x &lt;- rnorm(20) plot(x) summary(x) # Min. 1st Qu. Median Mean 3rd Qu. Max. # -2.34000 -0.86010 -0.21940 -0.43340 0.04383 1.06400 str(x) # num [1:20] -1.568 -0.219 -1.951 1.064 0.768 ... sd(x) # [1] 0.8932958 </code></pre> <p>which someone could quickly highlight and copy &amp; paste into an R session to execute the code and compare their output. Of course, in this case, they'll get different results, since I'm basing the example on random data.</p> <p>Thank you Jake, Marek, and everyone else... all of the responses have been helpful!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload