[R] R Markdown (Rstudio) Limit Results in knit Pdf

Michael Friendly friendly at yorku.ca
Sun Oct 5 02:10:10 CEST 2014


This is a knitr-specific question, and you are probably better off 
posting to the stackoverflow knitr questions site,
http://stackoverflow.com/questions/tagged/knitr

Nonetheless, here is what I use to add an output.lines options to chunk 
output.  This works for me using LaTeX output;  you can try it with 
markdown...

   # knitr hook function to allow an output.lines option
   # e.g.,
   #   output.lines=12 prints lines 1:12 ...
   #   output.lines=1:12 does the same
   #   output.lines=3:15 prints lines ... 3:15 ...
   #   output.lines=-(1:8) removes lines 1:8 and prints ... 9:n ...
   #   No allowance for anything but a consecutive range of lines

   hook_output <- knit_hooks$get("output")
   knit_hooks$set(output = function(x, options) {
      lines <- options$output.lines
      if (is.null(lines)) {
        return(hook_output(x, options))  # pass to default hook
      }
      x <- unlist(strsplit(x, "\n"))
      more <- "..."
      if (length(lines)==1) {        # first n lines
        if (length(x) > lines) {
          # truncate the output, but add ....
          x <- c(head(x, lines), more)
        }
      } else {
        x <- c(if (abs(lines[1])>1 | lines[1]<0) more else NULL,
               x[lines],
               if (length(x)>lines[abs(length(lines))]) more else NULL
              )
      }
      # paste these lines together
      x <- paste(c(x, ""), collapse = "\n")
      hook_output(x, options)
    })



On 10/4/2014 11:08 AM, Teis M. Kristensen wrote:
> Hi all,
>
> I am writing here because I would like to limit the number of lines that are produced from a function when I knit my markdown document in R.
>
> The code is written down as following and gives 50+ lines of data when run. My goal is to only have 9 lines of code produced by the sedist function.
>
> ```{r, results=1:9}
> sedist(FILENAME, method="correlation")
> ```
>
> I have tried using {r, message=1:9}, {r, Hide=1:9} and similar.
>
> Please let me if you have a solution.
>
> Best,
> Teis Moeller Kristensen
> School of Communication and Information
> Rutgers University
> Office ANX A - 103
>
>
> 	[[alternative HTML version deleted]]
>


-- 
Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept. & Chair, Quantitative Methods
York University      Voice: 416 736-2100 x66249 Fax: 416 736-5814
4700 Keele Street    Web:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA



More information about the R-help mailing list