How to convert code to a more readable form in R

I will copy the code from the terminal to post it here. It is in the following form:

> ddf2 = ddf[ddf$stone_ny>'stone',] # this is first command > ddf2[!duplicated(ddf2$deltnr),] # second command deltnr us stone_ny stone_mobility 4 1536 63 stone mobile 10 1336 62 stone mobile 

The first two lines are commands when the next 3 lines are output. However, this cannot be copied back to the R-terminal here, since the commands begin with '>'. How can I convert this to:

 ddf2 = ddf[ddf$stone_ny>'stone',] # this is first command ddf2[!duplicated(ddf2$deltnr),] # second command # deltnr us stone_ny stone_mobility #4 1536 63 stone mobile #10 1336 62 stone mobile 

To make it suitable for copying here.

I tried:

 text [1] "> ddf2 = ddf[ddf$stone_ny>'stone',] # this is first command\n> ddf2[!duplicated(ddf2$deltnr),] # second command\n deltnr us stone_ny stone_mobility \n4 1536 63 stone mobile \n10 1336 62 stone mobile " text2 = gsub('\n','#',text) text2 = gsub('#>','\n',text2) text2 = gsub('#','\n#',text2) text2 [1] "> ddf2 = ddf[ddf$stone_ny>'stone',] \n# this is first command\n ddf2[!duplicated(ddf2$deltnr),] \n# second command\n# deltnr us stone_ny stone_mobility \n#4 1536 63 stone mobile \n#10 1336 62 stone mobile " 

But it cannot be inserted into the terminal.

+5
source share
2 answers

I was waiting for the opportunity to share this function stored in a .Rprofile file. Although he may not answer exactly your question, I feel that he is doing something very close to what you need. This way, you can get some ideas by looking at its code. And others may find it useful as it is. Function:

 SO <- function(script.file = '~/.active-rstudio-document') { # run the code and store the output in a character vector tmp <- tempfile() capture.output( source(script.file, echo = TRUE, prompt.echo = "> ", continue.echo = "+ "), file = tmp) out <- readLines(tmp) # identify lines that are comments, code, results idx.comments <- grep("^> [#]{2}", out) idx.code <- grep("^[>+] ", out) idx.blank <- grep("^[[:space:]]*$", out) idx.results <- setdiff(seq_along(out), c(idx.comments, idx.code, idx.blank)) # reformat out[idx.comments] <- sub("^> [#]{2} ", "", out[idx.comments]) out[idx.code] <- sub("^[>+] ", " ", out[idx.code]) out[idx.results] <- sub("^", " # ", out[idx.results]) # output cat(out, sep = "\n", file = stdout()) } 

This SO feature allows me to quickly format my answers to questions on this very StackOverflow website. My workflow is as follows:

1) In RStudio, write my answer in an untitled script (in the upper left quadrant). For instance:

 ## This is super easy, you can do set.seed(123) # initialize x x <- 0 while(x < 0.5) { print(x) # update x x <- runif(1) } ## And voila. 

2) At the top of the screen, click the "Source" button. It will execute the code in the console, which is actually not what we are doing: rather, it will have the side effect of saving the code to the default file '~ / .active-rstudio-document.

3) Run SO() from the console (lower left quadrant), which will call the code (again ...) from the saved file, capture the output and print it in SO-friendly format:

 This is super easy, you can do set.seed(123) # initialize x x <- 0 while(x < 0.5) { print(x) # update x x <- runif(1) } # [1] 0 # [1] 0.2875775 And voila. 

4) Copy-paste into stackoverflow and do.

Note. For code that takes some time to execute, you can not run it twice by saving the script to a file (for example, "xyz.R") instead of clicking the "Source" button. Then run SO("xyz.R") .

+7
source

You can try cat with the ifelse condition.

 cat(ifelse(substr(s <- strsplit(text, "\n")[[1]], 1, 1) %in% c("_", 0:9, " "), paste0("# ", s), gsub("[>] ", "", s)), sep = "\n") 

that leads to

 ddf2 = ddf[ddf$stone_ny>'stone',] # this is first command ddf2[!duplicated(ddf2$deltnr),] # second command # deltnr us stone_ny stone_mobility # 4 1536 63 stone mobile # 10 1336 62 stone mobile 

"_" and 0:9 are there because one of the rules in R is that a function cannot start with _ or a digit. You can customize it according to your needs.

+2
source

Source: https://habr.com/ru/post/1203520/


All Articles