How can I save a specified number of lines from R's history to a file?


This is a little frustrating, and I'm sure there's an easy answer.

history(max.show=N) will display N lines of history on to the terminal. savehistory(file) will save a number of lines of history to a file, depending on some environment variable. What I would like to do is

savehistory(file, max.show=N)

To do this in one line instead of having to copy or trawl through a history file for the lines I want would make things much easier.

Is there a quick function/way to save a specified number of lines to a specified file?


I think your best bet is to abuse the history function:

history2file <- function (fname, max.show = 25, reverse = FALSE, pattern, ...) { ## Special version of history() which dumps its result to 'fname' file1 <- tempfile("Rrawhist") savehistory(file1) rawhist <- readLines(file1) unlink(file1) if (!missing(pattern)) rawhist <- unique(grep(pattern, rawhist, value = TRUE, ...)) nlines <- length(rawhist) if (nlines) { inds <- max(1, nlines - max.show):nlines if (reverse) inds <- rev(inds) } else inds <- integer() writeLines(rawhist[inds], fname) } history2file("/tmp/bla")

I would however stimulate you to start working in script files directly, and not do stuff on the command line and then later try to piece a script together.


  • selecting every Nth column in using SQLDF or read.csv.sql
  • Per Machine App Registration
  • Exception handling as per java coding standards
  • Finding parents in a tree hierarchy for a given child LINQ (lambda expression)
  • Quickly or concisely determine the longest string per column in a row-based data collection
  • How to sort things out in ListView?
  • C++ accessing vector
  • Whats the right place for testhelper-classes? (phpunit/best practise)
  • GitHub default README markup
  • Iterate twice through a DataReader
  • SQL query to group by maximal sets of a column having inner consecutive distances below a threshold
  • How to autopopulate a field in SugarCRM form
  • Invalid Date on validation Date of js
  • std::remove_copy_if_ valgrind bytes in block are possibly lost in loss record
  • Calling Worksheet functions from vba in foreign language versions of Excel
  • How to have background script and something similar to a default popup?
  • d3 v4 drag and drop with TypeScript
  • Functions in global context
  • jQuery show() function is not executed in Safari if submit handler returns true
  • RectangularRangeIndicator format like triangular using dojo
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Perl system calls when running as another user using sudo
  • How to model a transition system with SPIN
  • How do you troubleshoot character encoding problems?
  • XCode can't find symbols for a specific iOS library/framework project
  • How to format a variable of double type
  • how does django model after text[] in postgresql [duplicate]
  • Cant find why the layout is getting smaller
  • How to stop GridView from loading again when I press back button?
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • How to Embed XSL into XML
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?