Skip to content
Back to formatted view

Raw Message

Message-ID: <f8e6ff050812090915p6f5a7cb4wbec568aaa07810db@mail.gmail.com>
Date: 2008-12-09T17:15:13Z
From: Hadley Wickham
Subject: Replacing tabs with appropriate number of spaces
In-Reply-To: <684D8670-21FF-4DF5-8656-F2894DE98418@plessthan.com>

On Tue, Dec 9, 2008 at 10:39 AM, Dennis Fisher <fisher at plessthan.com> wrote:
> Colleagues,
>
> Platform: OS X (but issue applies to all platforms)
> Version: 2.8.0
>
> I have a mixture of text and data that I am outputting via R to a pdf
> document (using a fixed-width font).  The text contains tabs that align
> columns properly with a fixed-width font in a terminal window.  However,
> when the PDF document is created, the concept of a tab is not invoked
> properly and columns do not align.
>
> I could use brute force as follows:
> 1.  identify lines of text containing tabs
> 2.  strsplit on tabs
> 3.  count characters preceding the tab, then replace the tab with the
> appropriate number of spaces (e.g., if the string preceding the tab has 29
> characters, add 3 spaces), then paste(..., sep="")

Tabs usually just expand to a fixed number of spaces - so gsub("\t","
  ", text) would do the job.

Hadley

-- 
http://had.co.nz/