Change Default Prompt and Output Line Prefix in R

Change default prompt and output line prefix in R?

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

cleanCode <- function() {
if (.Platform$OS.type == "unix" && .Platform$pkgType == "mac.binary") {
to_edit <- readLines(pipe("pbpaste")) # Mac ONLY solution
} else {
to_edit <- readLines("clipboard") # Windows/Unix solution
}
opts <- options()
cmdPrompts <- paste("^", opts$prompt, "|^", opts$continue, sep="")

# can someone help me here? how to escape the + to \\+, as well as other special chars

id_commands <- grep("^> |^\\+ ", to_edit) # which are command or continuation lines
to_edit[id_commands] <- sub("^> |^\\+ ", "", to_edit[id_commands]) # remove prompts
to_edit[-id_commands] <- paste(" # ", to_edit[-id_commands]) # comment output
writeLines(to_edit)
}

which lets me highlight and copy a portion of the interactive session.

So, for example, I can use this to copy

> x <- 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

to the clipboard and with a simple call to

> cleanCode() 

produces output such as

x <- 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

which someone could quickly highlight and copy & 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.

Thank you Jake, Marek, and everyone else... all of the responses have been helpful!

Display Python prompt in code chunk output

You can use the knitr chunk option prompt = TRUE after you set options(prompt = '>>> ') in a previous code chunk, e.g.

```{r include=FALSE}
options(prompt = '>>> ')
```

```{python, prompt=TRUE}
print("hello world")
```

And adding options(continue = '>>> ') will add the >>> prompt prefix for multiple lines.

How to make the output recognizable in Sweave

I'm not sure if you know that knitr can also compile .Rnw documents (Sweave), in addition to .Rmd (R Markdown). You just pass the file path to knitr::knit(), e.g.,

knitr::knit('test.Rnw')

And you will get the .tex output file. If you want to get the PDF output directly, you may call:

knitr::knit2pdf('test.Rnw')

By default, knitr doesn't add the prompt character > or + to your code in the output.

shell script: Returning wrong output

Here's a solution using tostream instead of to_entries to facilitate simultaneous access to the full path and its value:

jq -r '
.color | tostream | select(.[0][-1] == "value" and has(1)) | .[0][:-1]+.[1:] | join(":")
' "$figma_json"
white:#ffffffff
gray:50:#fafafaff

Demo



Related Topics



Leave a reply



Submit