grid.locator              package:grid              R Documentation

_C_a_p_t_u_r_e _a _M_o_u_s_e _C_l_i_c_k

_D_e_s_c_r_i_p_t_i_o_n:

     Allows the user to click the mouse once within the current
     graphics device and returns the location of the mouse click within
     the current viewport, in the specified coordinate system.

_U_s_a_g_e:

     grid.locator(unit = "native")

_A_r_g_u_m_e_n_t_s:

    unit: The coordinate system in which to return the location of the
          mouse click. See the 'unit' function for valid coordinate
          systems.

_D_e_t_a_i_l_s:

     This function is modal (like the base function 'locator') so the
     command line and graphics drawing is blocked until the use has
     clicked the mouse  in the current device.

_V_a_l_u_e:

     A unit object representing the location of the mouse click within
     the current viewport, in the specified coordinate system.

     If the user did not click mouse button 1, the function (invisibly)
     returns 'NULL'.

_A_u_t_h_o_r(_s):

     Paul Murrell

_S_e_e _A_l_s_o:

     'viewport', 'unit', 'locator'

_E_x_a_m_p_l_e_s:

     if (interactive()) {
       ## Need to write a more sophisticated unit as.character method
       unittrim <- function(unit) {
         sub("^([0-9]+|[0-9]+[.][0-9])[0-9]*", "\\1", as.character(unit))
       }
       do.click <- function(unit) {
         click.locn <- grid.locator(unit)
         grid.segments(unit.c(click.locn$x, unit(0, "npc")),
                       unit.c(unit(0, "npc"), click.locn$y),
                       click.locn$x, click.locn$y,
                       gp=gpar(lty="dashed", col="grey"))
         grid.points(click.locn$x, click.locn$y, pch=16, size=unit(1, "mm"))
         clickx <- unittrim(click.locn$x)
         clicky <- unittrim(click.locn$y)
         grid.text(paste("(", clickx, ", ", clicky, ")", sep=""),
                   click.locn$x + unit(2, "mm"), click.locn$y,
                   just="left")
       }
       do.click("inches")
       pushViewport(viewport(width=0.5, height=0.5,
                              xscale=c(0, 100), yscale=c(0, 10)))
       grid.rect()
       grid.xaxis()
       grid.yaxis()
       do.click("native")
       popViewport()  
     }

