Thanks
a lot, Charles and Yihui,

Your
ideas are great! Both worked, although Yihui's solutions are a lot more
comprehensive.

BTW
as a comment on Charles idea, parse function works for loosen statement lines,
for not for the code blocks of a function. For example, the following code for 
vennCounts
function could not be sorted by using parse alone if we copy and paste into a
source file. However, if we comment the first line out as: # vennCounts <-
function (x, include = "both"), parse works and sorts the individual
lines as expected.

My
problem is well solved with your suggestions. But both ways, we used R’s own
resource/functionality. I am curious, are there any ways to break the long
lines automatically just in the text editor Emacs/ESS without using R? Thanks!

Heyi

 

vennCounts
<- function (x, include = "both")

  {

        x <- as.matrix(x)

            include <- match.arg(include,
c("both", "up", "down"))

            x <- sign(switch(include, both =
abs(x), up = x > 0, down = x < 0))

            nprobes <- nrow(x)

            ncontrasts <- ncol(x)

            names <- colnames(x)

            if (is.null(names))

                      names <-
paste("Group", 1:ncontrasts)

            noutcomes <- 2^ncontrasts

            outcomes <- matrix(0, noutcomes,
ncontrasts)

            colnames(outcomes) <- names

            for (j in 1:ncontrasts) outcomes[,
j] <- rep(0:1, times = 2^(j - 1), each = 2^(ncontrasts - j))

            xlist <- list()

            for (i in 1:ncontrasts) xlist[[i]]
<- factor(x[, ncontrasts - i + 1], levels = c(0, 1))

            counts <-
as.vector(table(xlist))

            structure(cbind(outcomes, Counts =
counts), class = "VennCounts")

      }

 



--- On Tue, 8/24/10, Yihui Xie <x...@yihui.name> wrote:

From: Yihui Xie <x...@yihui.name>
Subject: Re: [R] break the long R code lines automatically
To: "Charles C. Berry" <cbe...@tajo.ucsd.edu>
Cc: "heyi xiao" <xiaohey...@yahoo.com>, r-help@r-project.org
Date: Tuesday, August 24, 2010, 7:23 PM

Actually I've implemented Charles' idea in the function tidy.source()
in package 'animation' years ago. An example can be found here:
http://animation.yihui.name/animation:misc#tidy_up_r_source

BTW, you might be interested in the 'formatR' package if you are
somehow addicted to GUI:
http://yihui.name/en/2010/04/formatr-farewell-to-ugly-r-code/

Regards,
Yihui
--
Yihui Xie <xieyi...@gmail.com>
Phone: 515-294-6609 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA



On Tue, Aug 24, 2010 at 4:07 PM, Charles C. Berry <cbe...@tajo.ucsd.edu> wrote:
> On Tue, 24 Aug 2010, heyi xiao wrote:
>
>>
>>
>>
>>
>> Dear
>> all,
>>
>> I have
>> written some R source program with many thousands of lines. I didn’t
>> insert
>> line breaks automatically or manually for the long lines. But now I would
>> like
>> to edit the source code in Emacs/ESS to make it more formal as a package.
>> One of
>> the major problems here is how to break the long lines automatically.
>> Emacs auto-fill-mode
>> only works for the lines you are typing in currently, and fill commands
>> like M-q
>> (fill-paragraph) or M-x fill-region (fill-region) mess up the R code lines
>> as
>> they take a whole function/paragraph as a long line, and remove the
>> original
>> line breaks.
>>
>> I find
>> simple solutions for indenting code regions in Emacs/ESS, but no good ones
>> for
>> breaking code lines. However, I saw the nice multi-line codes in all
>> R/Bioconductor packages. Please let me know if you have any ideas on how
>> people
>> usually break the existent long R code lines automatically. I will really
>> appreciate your kind help!
>
>
> Not particualrly elegant, but a combination of parse and print will break
> long lines:
>
>
>> cat("y <- ",paste( 1:20,collapse=" + "),"\n","y2 <- ",
>
> +  paste( 1:20,collapse="+"),"\n",file="testwrap.R")
>>
>> for (iexpr in parse("testwrap.R")) print(iexpr)
>
> y <- 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 +
>    14 + 15 + 16 + 17 + 18 + 19 + 20
> y2 <- 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 +
>    14 + 15 + 16 + 17 + 18 + 19 + 20
>>
>> nchar(readLines("testwrap.R"))
>
> [1] 95 59
>>
>
> and of course you will want 'sink' or some such to save the lines.
>
> HTH,
>
> Chuck
>
>
>>
>> Heyi
>>
>>
>>
>>
>>
>>        [[alternative HTML version deleted]]
>>
>>
>
> Charles C. Berry                            (858) 534-2098
>                                            Dept of 
> Family/Preventive
> Medicine
> E mailto:cbe...@tajo.ucsd.edu               UC San Diego
> http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901
>
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



      
        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to