| 1 | #' Shell Execution via cmd windows |
| 2 | #' |
| 3 | #' This function is an extension to the \code{shell.exec}() which is a native |
| 4 | #' function of R |
| 5 | #' |
| 6 | #' \code{shell.exec("a.txt")} will open a windows (notepad) to edit the file |
| 7 | #' \code{a.txt} in windows system. However, the notepad will block the (parent) |
| 8 | #' active R windows, i.e. \code{SW.cmd = 5} as \code{SH_SHOW} by default. |
| 9 | #' |
| 10 | #' The \code{shellexec.wcc("a.txt", SW.cmd = 7L)} will open the notepad, but in |
| 11 | #' a minimized window. Therefore, there is no blocking to the active R windows. |
| 12 | #' See the website in the references section to see more options to control the |
| 13 | #' behavior of new windows. Possible choices are |
| 14 | #' |
| 15 | #' \code{SW_SHOW (5)}: Activates the window and displays it in its current size |
| 16 | #' and position. |
| 17 | #' |
| 18 | #' \code{SW_SHOWMINIMIZED (2)}: Activates the window and displays it as a |
| 19 | #' minimized window. |
| 20 | #' |
| 21 | #' \code{SW_SHOWMINNOACTIVE (7)}: Displays the window as a minimized window. |
| 22 | #' The active window remains active. |
| 23 | #' |
| 24 | #' \code{SW_SHOWNA (8)}: Displays the window in its current state. The active |
| 25 | #' window remains active. |
| 26 | #' |
| 27 | #' @param file |
| 28 | #' a file name as in \code{shell.exec}() |
| 29 | #' @param SW.cmd |
| 30 | #' a SW_* command of showing windows |
| 31 | #' |
| 32 | #' @return |
| 33 | #' A new windows with certain applications depending on the association |
| 34 | #' of the input \code{file}. |
| 35 | #' |
| 36 | #' @author Wei-Chen Chen \email{wccsnow@@gmail.com}. |
| 37 | #' |
| 38 | #' |
| 39 | #' @references Microsoft, Windows Dev Center: Windows desktop applications > |
| 40 | #' Develop > Desktop technologies > Desktop Environment > The Windows Shell > |
| 41 | #' Shell Reference > Shell Functions > ShellExecute |
| 42 | #' |
| 43 | #' \code{https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx} |
| 44 | #' |
| 45 | #' @examples |
| 46 | #' \dontrun{ |
| 47 | #' library(pbdZMQ, quietly = TRUE) |
| 48 | #' |
| 49 | #' shellexec.wcc("a.txt", 5L) |
| 50 | #' } |
| 51 | #' |
| 52 | #' @keywords internal |
| 53 | #' @seealso \code{shell.exec()}. |
| 54 | #' @rdname u0_shellexec.wcc |
| 55 | shellexec.wcc <- function(file, SW.cmd = 7L){ |
| 56 | if(length(file) == 1 && is.character(file)){ |
| 57 | fn.enc <- Encoding(file) |
| 58 | if(fn.enc == "latin1"){ |
| 59 | fn.enc <- 1L |
| 60 | } else if(fn.enc == "UTF-8"){ |
| 61 | fn.enc <- 2L |
| 62 | } else if(fn.enc == "bytes"){ |
| 63 | fn.enc <- 3L |
| 64 | } else if(fn.enc == "unknown"){ |
| 65 | fn.enc <- 4L |
| 66 | } |
| 67 | .Call("shellexec_wcc", file, as.integer(SW.cmd), as.integer(fn.enc), |
| 68 | PACKAGE="pbdZMQ") |
| 69 | } else{ |
| 70 | stop("file should be a character vector of length 1.") |
| 71 | } |
| 72 | } |
| 73 |