1

I would like to ask if there is a way to align text after we have entered it in gtext. For example

x <- c(123.4, 5.6)
y <- c(1.2, 3.657)
z <- c(12345.6, 789.4)

df <- data.frame(x, y, z)

df.co <- capture.output(df) # get df as text

for (i in 1:length(df.co))
{
    str.split <- strsplit(df.co, "\\s+") # split every line in its components
}

w3 <- gwindow()
gt3 <- gtext(container=w3)

for (i in 1:length(str.split)) # length(str.split)=3
{
for (j in 1:length(str.split[[i]])) # length(str.split[[i]])=2
    {
        str.split[[i]][[j]] <- paste(str.split[[i]][[j]],"\t",sep="",collapse="")  # add tab to each component
    }
    str.split[[i]] <- paste(str.split[[i]], sep="", collapse="") # join to one line
    insert(gt3, str.split[[i]])
}

This way we mimic the R console. Thank you a lot, in advance

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
Apostolos Polymeros
  • 823
  • 1
  • 8
  • 19

2 Answers2

2

Make sure you use a monospace font. If you don't then it's very very difficult to align things. Monospace fonts have each character taking up the same amount of space.

# Instead of
insert(gt3, str.split[[i]])
# Try this
insert(gt3, str.split[[i]], font.attr = c(family="monospace"))
Dason
  • 60,663
  • 9
  • 131
  • 148
2

There is no way to align text in the sense of right-align/centre/etc in gWidgets.

I recommend monospace fonts as @Dason suggested (although that seems to be the default for me).

In terms of your code, you can clean it up a little:

  • You don't need the loop in creation of str.split (nothing depends on i and you are writing over str.split each time)
  • You can eliminate the loops that you use to put the tabs in str.split[[i]]
  • You don't need to add each line of str.split in separately, you can do it all at once.

In summary:

x <- c(123.4, 5.6)
y <- c(1.2, 3.657)
z <- c(12345.6, 789.4)

df <- data.frame(x, y, z)

df.co <- capture.output(df) # get df as text

# @@ don't need loop
str.split <- strsplit(df.co, "\\s+") # split every line in its components

w3 <- gwindow()
gt3 <- gtext(container=w3)

# @@ collapse each str.split[[i]] by joining with '\t'.
str.joined <- sapply(str.split,function(bits) paste(bits,collapse='\t'))
insert(gt3,str.joined)

Alternately, you can replace everything after df.co <- capture.output(df) by:

insert(gt3,df.co)

(do the two side-by-side and compare: insert(gt3,df.co) right-aligns each column of the dataframe, exactly as typing df would show; the method you have (insert(gt3,str.joined)) left-aligns each column.

# insert(gt3,str.joined)/method in your question
    x       y       z   
1   123.4   1.200   12345.6
2   5.6     3.657   789.4

# insert(gt3,df.co)
      x     y       z
1 123.4 1.200 12345.6
2   5.6 3.657   789.4
mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194
  • Hmm. My font definitely doesn't default to monospace. Any ideas on how to set those defaults? – Dason Jan 09 '12 at 01:59
  • Wouldn't have a clue :/ It might depend on the base graphics package being used (although I get monospace for both gWidgetstcltk and gWidgetsRgtk2). For what it's worth, I get monospace in gedit/gtext but otherwise (Arial-ish?) for buttons/labels/etc. – mathematical.coffee Jan 09 '12 at 02:02
  • Oh well. Looks like I'll just keep setting the font.attr manually when I have to. Good job cleaning up their code by the way. – Dason Jan 09 '12 at 02:35
  • You don't need text justification in `gWidgets` itself, because you can use `format` on your string and pass that to the widget. Compare `x <- c("foo", "football"); format(x, justify = "left")` with `format(x, justify = "right")`. – Richie Cotton Jan 09 '12 at 13:10
  • The default font is not set by gWidgets but inherited from the system defaults. You might look there. Otherwise, use the font.attr argument for the constructor. As for justification, thanks Richie! – jverzani Jan 09 '12 at 13:58
  • font.attr = c(family="monospace") works well with the aligning but changes my font format (font type and size). Anyway to keep my current format? – SilverSpoon Nov 07 '12 at 10:16