[R] window manager interface commands for linux

Jim Lemon jim at bitwrit.com.au
Tue Nov 29 11:51:19 CET 2011


Ana wrote:

 > How can i replicate this in Linux:
 > source(file.choose())
 >

Hi Ana,
There is probably some way to do this with a shell script, but I am 
unaware of it. If you have the marvelous Tcl-Tk scripting language on 
your Linux box, you could write something like this:

#!/usr/bin/wish
# sourceRfile a script to run an R command file

# SelectFile - A module for selecting a filename from a directory
# call "SelectFile mask" where 'mask' contains a mask or filename
# default mask = *

proc GetFileList filemask {
  set filelist [ glob -nocomplain $filemask ]
  return $filelist
}

proc StringElement cmplist {
  if { [ llength $cmplist ] > 1 } {
   set cmpstring [ lindex $cmplist 0 ]
   set cmpchar [ lindex $cmplist 1 ]
   set stringlength [ string length $cmpstring ]
   set charlength [ string length $cmpchar ]
   set stringindex 0
   while { $stringindex < $stringlength } {
    set charindex 0
    while { $charindex < $charlength } {
     if { [ string index $cmpstring $stringindex ] ==\
          [ string index $cmpchar $charindex ] } { return 1 }
     incr charindex
    }
    incr stringindex
   }
  }
  return 0
}

proc SelectFile mask {
  global filename
  global done
  global rindex

  if { [ string length $mask ] == 0 } {
   set mask "*"
  }
  set path [ pwd ]
  set done 0
  set filename ""
  set rindex 0
  while { !$done } {
   frame .sff -bd 3 -relief raised
   label .sff.path -text $path
   entry .sff.name
   label .sff.filelabel -text "Directories"
   frame .sff.fileframe
   label .sff.dirlabel -text "Files"
   frame .sff.dirframe
   frame .sff.buttonframe
   listbox .sff.fileframe.flist -selectmode single \
    -yscroll ".sff.fileframe.yscroll set"
   scrollbar .sff.fileframe.yscroll -relief sunken  \
    -command ".sff.fileframe.flist yview"
   listbox .sff.dirframe.dlist -selectmode single \
    -yscroll ".sff.dirframe.yscroll set"
   scrollbar .sff.dirframe.yscroll -relief sunken  \
    -command ".sff.dirframe.dlist yview"
   set filelist [ GetFileList $mask ]
   lsort filelist
   set fileindex 0
   foreach filein $filelist {
    if { [ file isfile $filein ] } {
     .sff.fileframe.flist insert $fileindex $filein
     incr fileindex
    }
   }
   set dirlist [ GetFileList "*" ]
   lsort dirlist
   set dlist [ list ]
   set dirindex 0
   if { $path != "/" } {
    .sff.dirframe.dlist insert $dirindex ".."
    incr dirindex
   }
   foreach dirin $dirlist {
    if { [ file isdirectory $dirin ] } {
     .sff.dirframe.dlist insert $dirindex $dirin
     incr dirindex
    }
   }
   bind .sff.fileframe.flist <ButtonRelease-1> {
    .sff.name delete 0 end
    .sff.name insert 0 [ .sff.fileframe.flist get\
    [ .sff.fileframe.flist curselection ] ]
   }
   button .sff.buttonframe.ok -text OK -command {
    if { [ llength [ .sff.dirframe.dlist curselection ] ] } {
     set filename [ .sff.dirframe.dlist get [ .sff.dirframe.dlist 
curselection ] ]
    } else {
     if { [ llength [ .sff.fileframe.flist curselection ] ] } {
      set filename [ .sff.fileframe.flist get [ .sff.fileframe.flist 
curselection ] ]
     } else {
      if { [ llength [ .sff.name get ] ] } {
       set filename [ .sff.name get ]
      }
     }
    }
    destroy .sff
   }
   button .sff.buttonframe.cancel -text Cancel -command {
    set rindex -1
    set done 1
    set filename ""
    destroy .sff
   }
   bind .sff.name <Return> {
    set filename [ .sff.name get ]
    destroy .sff
   }
   place .sff -relx 0.5 -rely 0.5 -anchor center -width 400\
   -height 300
   place .sff.path -relx 0.5 -rely 0.02 -anchor n
   place .sff.name -relx 0.5 -rely 0.12 -anchor n
   place .sff.filelabel -relx 0.05 -rely 0.22
   place .sff.dirlabel -relx 0.55 -rely 0.22
   place .sff.dirframe -relx 0.05 -rely 0.3 -relwidth 0.43\
   -relheight 0.5
   place .sff.fileframe -relx 0.55 -rely 0.3 -relwidth 0.43\
   -relheight 0.5
   place .sff.fileframe.flist -relx 0 -relwidth 0.85 -relheight 1
   place .sff.fileframe.yscroll -relx 0.9 -rely 0.5\
   -anchor center -relheight 1
   place .sff.dirframe.dlist -relx 0 -relwidth 0.85 -relheight 1
   place .sff.dirframe.yscroll -relx 0.9 -rely 0.5\
   -anchor center -relheight 1
   place .sff.buttonframe -relx 0.5 -rely 0.9 -anchor center
   pack .sff.buttonframe.ok -side left
   pack .sff.buttonframe.cancel -side right
   .sff.name insert 0 $mask
   focus .sff.name
   tkwait window .sff
   if { [ file isdirectory $filename ] } {
    cd $filename
    set path [ pwd ]
   } elseif { [ StringElement [ list $filename "*?\[\]" ] ] } {
    set mask $filename
   } else {
    set done 1
   }
  }
  return $filename
}

wm geometry . 400x400
set Rfile [ SelectFile "*.R" ]
exec R CMD BATCH $Rfile
exit

Then simply create the following file named "helloR.R" in the same 
directory:

sink("helloR.out")
cat("Hello, R\n")
sink()

chmod +x sourceRfile.tk

./sourceRfile.tk

Select the file "helloR.R" from the resulting menu, and that file will 
be "sourced" by R. However, this is a bloody roundabout way to source a 
file in R.

Jim



More information about the R-help mailing list