Changing width.cutoff for tidy=T

414 views
Skip to first unread message

Joe

unread,
Apr 9, 2012, 7:53:37 PM4/9/12
to kn...@googlegroups.com
Is there a way to change the width.cutoff for chunks where tidy=T? I'd like the width.cutoff to be wider. I tried setting this output hook, but it didn't work.

<<hooks, echo = F,cache=F>>=
  knit_hooks$set(
  source = function(x,options){
    if(options$tidy){
      return(tidy.source(text = x, width.cutoff = 0.9*getOption("width")))
    }
  }
@

Thanks!
-Joe



Yihui Xie

unread,
Apr 9, 2012, 8:02:29 PM4/9/12
to Joe, kn...@googlegroups.com
If you want an individual width for a chunk, the answer is no, there
is not such an option at the chunk level. The width can only be set at
the global level as options(width = ...), so you have at best this
solution for the time being:

<<>>=
options(width = 50)
<<real-chunk>>=
# R code here
<<>>=
options(width = 75)
@

If you want a global width, it will be much easier; just set it in
your first chunk.

BTW, note there was a change in the formatR package, on which this
functionality is based
(http://cran.r-project.org/web/packages/formatR/NEWS):

o the default value of the 'width.cutoff' argument in tidy.source()
is getOption('width') now; in the past it was 75% of that width
which was less intuitive


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

Joe

unread,
Apr 9, 2012, 8:11:54 PM4/9/12
to kn...@googlegroups.com, Joe
Updating formatR did the trick for me, thanks!

Yihui Xie

unread,
Aug 11, 2012, 9:57:30 PM8/11/12
to Roger Close, kn...@googlegroups.com, Joe
Unfortunately, no, formatR cannot break a character string into
several lines. I know that is a little bit annoying. One workaround is
to manually break that string into shorter pieces and paste() them
together.

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


On Sat, Aug 11, 2012 at 4:08 AM, Roger Close <roger...@gmail.com> wrote:
> Hi Yihui,
> I find that code sometimes still goes off the edge of the page regardless of
> what width= is set to be, usually if it's a long string in quotation marks.
> I can fix things up by rearranging the order of arguments in the code, but
> is there a way to (for example) automatically force each argument onto a new
> line if it when using tidy=TRUE?
> Thanks,
> Roger
Reply all
Reply to author
Forward
0 new messages