Specify a monospace font in `menu`

Language: R. Question: Can I specify a fixed-width font for the menu(..,graphics=T) function?

Explanation:

I recently asked this question about how a user can select a data frame row interactively:

 df <- data.frame(a=c(9,10),b=c('hello','bananas')) df.text <- apply( df, 1, paste, collapse=" | " ) menu(df.text,graphics=T) 

enter image description here

I want to | lined up. At the moment there are none; Fair enough, I didn’t fill columns of the same width. Therefore, I use format so that each column is the same width (I will write code later to automatically determine the width of the column, but ignore this time):

 df.padded <- apply(df,2,format,width=8) df.padded.text <- apply( df.padded, 1, paste, collapse=" | ") menu( df.padded.text,graphics=T ) 

enter image description here

See how it is still smelly? However, if I look at df.padded , I get:

 > df.padded ab [1,] " 9 " "hello " [2,] "10 " "bananas " 

Thus, each cell is definitely complemented by the same length.

The reason for this is probably because the default font for this (on my system, Linux anyway) is not a fixed width.

So my question is: Is it possible to specify a fixed-width font for the menu(..,graphics=T) function?

Update

@RichieCotton noticed that if you look at menu graphics=T , it calls select.list , which in turn calls tcltk::tk_select.list .

So, it looks like I will have to change tcltk parameters for this. From @jverzani:

 library(tcltk) tcl("option", "add", "*Listbox.font", "courier 10") menu(df.padded.text,graphics=T) 

enter image description here

Given that menu(...,graphics=T) calls tcltk::tk_select.list when graphics TRUE, I assume this is a viable option, since any distribution that could display a graphical menu also primarily would have tcltk on it, since it needs to call tk_select.list .

(Aside, I cannot find anything in the documentation that would give me a hint to try tcl('option','add',...) , not to mention that this parameter was called *Listbox.font !)

Another update - we examined the select.list and menu code in more detail, and it turns out on Windows (or if .Platform$GUI=='AQUA' is Mac?), tcltk::tk_select.list is not called at all, but instead only some internal code is used. Therefore, changing '* Listbox.font' will not affect this.

I think I'm just:

  • If tcltk exists, download it, set * Listbox.font to the courier and use tcltk::tk_select.list explicitly
  • if it is not, try menu(...,graphics=T) to at least get a graphical interface (which will not be monospaced, but better than nothing).
  • if that doesn't work either, then just go back to menu(...,graphics=F) , which will definitely work.

Thanks to everyone.

+47
r fixed-width monospace
Feb 08 '12 at 7:16
source share
2 answers

Another filling approach:

 na.pad <- function(x,len){ x[1:len] } makePaddedDataFrame <- function(l,...){ maxlen <- max(sapply(l,length)) data.frame(lapply(l,na.pad,len=maxlen),...) } x = c(rep("one",2)) y = c(rep("two",10)) z = c(rep("three",5)) makePaddedDataFrame(list(x=x,y=y,z=z)) 

The na.pad() function na.pad() advantage of the fact that R will automatically place the vector with NA if you try to index non-existent elements.

makePaddedDataFrame() simply finds the longest and substitutes the rest to the appropriate length.

+1
Jun 12 '12 at 22:00
source share

I don’t understand why you don’t want to use View (df) (get rowid, put the contents in temp. Data frame and show it with the View command)

Edit: well, just use the sprintf command

Create a function f to extract rows from a data frame object

 f <- function(x,sep1) { sep1=format(sep1,width=8) xa<-gsub(" ","",as.character(x[1])) a1 <- nchar(xa) xa=format(xa,width=8) xb=gsub(" ","",as.character(x[2])) b1 <- nchar(xb) xb=format(xb,width=8) format1=paste("%-",10-a1,"s%s%-",20-b1,"s",sep="") concat=sprintf(format1,xa,sep1,xb) concat } 

df <- data.frame(a=c(9,10),b=c('hello','bananas'))

df.text <- apply( df, 1, f,sep1="|")

menu(df.text,graphics=T)

Of course, the limits used in sprintf 10, 20 are the maximum length for the number of characters in a column of a data frame (a, b). You can change it to reflect it according to your data.

0
May 08 '12 at 3:57
source share



All Articles