Language: R. Question: Can I specify fixed width font for the menu(..,graphics=T)
function?
Explanation:
I recently asked this question on how to have a user select a row of a data frame interactively:
df <- data.frame(a=c(9,10),b=c('hello','bananas'))
df.text <- apply( df, 1, paste, collapse=" | " )
menu(df.text,graphics=T)
I'd like the |
to line up. They don't at the moment; fair enough, I haven't padded out the columns to the same width. So I use format
to get every column to the same width (later I'll write code to automagically determine the width per column, but let's ignore that for now):
df.padded <- apply(df,2,format,width=8)
df.padded.text <- apply( df.padded, 1, paste, collapse=" | ")
menu( df.padded.text,graphics=T )
See how it's still wonky? Yet, if I look at df.padded
, I get:
> df.padded
a b
[1,] " 9 " "hello "
[2,] "10 " "bananas "
So each cell is definitely padded out to the same length.
The reason for this is probably because the default font for this (on my system anyway, Linux) is not fixed width.
So my question is:
Can I specify fixed width font for the menu(..,graphics=T)
function?
Update
@RichieCotton noticed that if you look at menu
with graphics=T
it calls select.list
, which in turn calls tcltk::tk_select.list
.
So it looks like I'll have to modify tcltk
options for this. From @jverzani:
library(tcltk)
tcl("option", "add", "*Listbox.font", "courier 10")
menu(df.padded.text,graphics=T)
Given that menu(...,graphics=T)
calls tcltk::tk_select.list
when graphics
is TRUE, my guess is that this is a viable option, as any distro that would be capable of displaying the graphical menu
in the first place would also have tcltk
on it, since it needs to call tk_select.list
.
(As an aside, I can't find anything in the documentation that would give me the hint to try tcl('option','add',...)
, let alone that the option was called *Listbox.font
!)
Another update -- had a closer look at the select.list
and menu
code, and it turns out on Windows (or if .Platform$GUI=='AQUA'
-- is that Mac?), the tcltk::tk_select.list
isn't called at all, and it's just some internal code instead. So modifying '*Listbox.font' won't affect this.
I guess I'll just:
- if tcltk is there, load it, set the *Listbox.font to courier, and use
tcltk::tk_select.list
explicitly - if it isn't there, try
menu(...,graphics=T)
to at least get a graphical interface (which won't be monospace, but is better than nothing) - if that fails too, then just fallback to
menu(...,graphics=F)
, which will definitely work.
Thanks all.
menu
callsselect.list
, which disappears into external code. You might have more luck hackingtcltk::tk_select.list
. – Leaseholdertcltk
. Do you know if it is standard in all R distros? I'd just like to minimise the number of external packages users have to install (as I have quite a number already). Cheers. – Hicktcltk
is standard on Windows but not Linux (unsure about OSX). You can test for support withcapabilities("tcltk")
. – Leaseholderlibrary(tcltk); tcl("option", "add", "*Listbox.font", "times 16 bold ")
will do it. – Asceticismtcl('option',...)
(I'm on Fedora, R 2.14, andcapabilitites('tcltk')
is also TRUE for me, although I remember explicitly installing a whole bunch of gui packages). I had just hoped there was something inoptions
I could manipulate without loading a gui library. – Hick