Source lines of code "n"

For a code to be recognized as code here, it must have four lines. This can be done manually or using the brackets or labels. What if I want to do this via R? Obviously this can be done (just look at formatR). How does this happen with the least amount of coding?

So, for the following lines (dataframe and function), what is the best way (least code) to use R to indent each line exactly 4 spaces?

foo<-function(x,y){ z<-x*y super.z<-z^2 return(super.z) } 

and

  id hs.grad race gender age 1 ID1 yes asian female 32 2 ID2 yes white female 30 3 ID3 yes white female 34 4 ID4 yes black female 25 5 ID5 no white male 19 
+4
source share
4 answers

I decided to spend my time today on making this function exactly as I described it. I created a function that accepts data frames or functions and can be printed or a clip-board filed to the indent function. This will cause the code to contain as many spaces as required with a space argument (four spaces by default).

 indent <- function(object = "clipboard", space = 4) { y <- if (object == "clipboard") { as.list(readClipboard()) } else { strsplit(as.vector(object), "[\\n]") } spacer <- function(x) paste(paste(rep(" ", space - 2), collapse = ""), x) z <- if (object == "clipboard") { sapply(y, spacer) } else { lapply(y, spacer) } zz <- as.matrix(as.data.frame(z)) dimnames(zz) <- list(c(rep("", nrow(zz))), c("")) noquote(zz) } #========================================================== # Test it out!!!!!! #========================================================== indent(" id hs.grad race gender age 1 ID1 yes white male 37 2 ID2 yes white male 32 3 ID3 yes asian male 20 4 ID4 no black female 24 5 ID5 no white female 32") #========================================================== indent("ascii<-function(x, header=TRUE,...){ name <-textConnection(x) DF <- read.table(name, header, ...) close(name) on.exit(closeAllConnections()) DF }", space = 10) #============================================================ # THE NEXT TWO CAN BE CUT AND PASTED WITH THE CLIPBOARD ARG #============================================================ id hs.grad race gender age 1 ID1 yes white male 37 2 ID2 yes white male 32 3 ID3 yes asian male 20 4 ID4 no black female 24 5 ID5 no white female 32 indent("clipboard") #============================================================ ascii<-function(x, header=TRUE,...){ name <-textConnection(x) DF <- read.table(name, header, ...) close(name) on.exit(closeAllConnections()) DF } indent() #clipboard is the default arg not needed 
+1
source

Here's a little function that will format an object in the StackOverflow style:

 formatSO <- function(x) { y <- get(x, parent.frame()) d <- deparse(y) cat(" ", x, "<-", d[1], "\n") cat(paste(" ", d[-1], "\n", sep=""), sep="") } 

And try:

 > foo<-function(x,y){ + z<-x*y + super.z<-z^2 + return(super.z) + } > formatSO("foo") foo <- function (x, y) { z <- x * y super.z <- z^2 return(super.z) } > x <- 5:3 > formatSO("x") x <- c(5L, 4L, 3L) 
+4
source

Customization

 options(prompt = " ") 

will format any code you write in the console so that it is easy to paste. The output will not have four spaces at the beginning, although another workaround is probably required.

+3
source

formatR: Format R Code Automatically can be used for this purpose. Available here

 library(formatR) src <- c("foo<-function(x,y){ z<-x*y super.z<-z^2 return(super.z) } ") tidy.source(text = src, replace.assign = TRUE) foo <- function(x, y) { z <- x * y super.z <- z^2 return(super.z) } 
+2
source

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


All Articles