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
Why Is Foreach() %Do% Sometimes Slower Than For
R: How to Display Clustered Matrix Heatmap (Similar Color Patterns Are Grouped)
Replacing the "Print" Function in Knitr Chunk Evaluation
K-Means Clustering in R on Very Large, Sparse Matrix
Row-By-Row Operations and Updates in Data.Table
How to Upload a File to a Server via Ftp Using R
Highlighting Individual Axis Labels in Bold Using Ggplot2
Add One Column Below Another in a Data.Frame in R
How to Add a Line to One of the Facets
How to Directly Perform Write.CSV in R into Tar.Gz Format
Extracting Decimal Numbers from a String
Error When Using Dplyr Inside of a Function
Ggplot2 Draw Dashed Lines of Same Colour as Solid Lines Belonging to Different Groups