I wonder if there is any function to put line numbers
with knitr
in .Rnw
. I found this discussion and these documents (here and here) but could not find the way to put line numbers. Any help will be highly appreciated. Thanks
相关问题
- R - Quantstart: Testing Strategy on Multiple Equit
- Using predict with svyglm
- Reshape matrix by rows
- Extract P-Values from Dunnett Test into a Table by
- split data frame into two by column value [duplica
相关文章
- How to convert summary output to a data frame?
- How to plot smoother curves in R
- Paste all possible diagonals of an n*n matrix or d
- ess-rdired: I get this error “no ESS process is as
- How to use doMC under Windows or alternative paral
- dyLimit for limited time in Dygraphs
- Saving state of Shiny app to be restored later
- How to insert pictures into each individual bar in
This solution uses the LaTeX listings package to create line numbers. I can only get them to work by accumulating across all code chunks, but I imagine there is a similar solution that will enumerate lines only within each chunk. Here's the .Rnw source:
The key parts of this are in the source hook, which is basically copied from here. The
firstnumber=last
tells listings to accumulate line numbers across listings. Without it, all lines are numbered 1 because knitr is putting each code line in its own listing.And here's the result:
If you want each code block to start numbering from 1, add a hook to reset the counter:
and then use
reset=TRUE
to activate the hook in each chunk you want:For use in HTML, you can set the chunk option
class.source
to pass custom css to the output: