[Sciviews-commits] r483 - in pkg/svMisc: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Jul 7 18:28:10 CEST 2012
Author: phgrosjean
Date: 2012-07-07 18:28:09 +0200 (Sat, 07 Jul 2012)
New Revision: 483
Added:
pkg/svMisc/R/fileEdit.R
pkg/svMisc/man/fileEdit.Rd
Modified:
pkg/svMisc/DESCRIPTION
pkg/svMisc/NAMESPACE
pkg/svMisc/NEWS
pkg/svMisc/R/svMisc-internal.R
pkg/svMisc/man/svMisc-package.Rd
pkg/svMisc/man/systemFile.Rd
Log:
Added fileEdit() in the svMisc package
Modified: pkg/svMisc/DESCRIPTION
===================================================================
--- pkg/svMisc/DESCRIPTION 2012-07-02 13:45:55 UTC (rev 482)
+++ pkg/svMisc/DESCRIPTION 2012-07-07 16:28:09 UTC (rev 483)
@@ -1,7 +1,7 @@
Package: svMisc
Type: Package
-Version: 0.9-66
-Date: 2012-07-01
+Version: 0.9-67
+Date: 2012-07-07
Title: SciViews GUI API - Miscellaneous functions
Authors at R: c(person("Philippe", "Grosjean", role = c("aut", "cre"),
email = "phgrosjean at sciviews.org"),
Modified: pkg/svMisc/NAMESPACE
===================================================================
--- pkg/svMisc/NAMESPACE 2012-07-02 13:45:55 UTC (rev 482)
+++ pkg/svMisc/NAMESPACE 2012-07-07 16:28:09 UTC (rev 483)
@@ -24,6 +24,7 @@
descFun,
evalRjson,
existsTemp,
+ fileEdit,
getEnvironment,
getTemp,
guiCmd,
Modified: pkg/svMisc/NEWS
===================================================================
--- pkg/svMisc/NEWS 2012-07-02 13:45:55 UTC (rev 482)
+++ pkg/svMisc/NEWS 2012-07-07 16:28:09 UTC (rev 483)
@@ -1,5 +1,10 @@
= svMisc News
+== Changes in svMisc 0.9-67
+
+* Added the fileEdit() function.
+
+
== Changes in svMisc 0.9-66
* Added function isJGR().
Added: pkg/svMisc/R/fileEdit.R
===================================================================
--- pkg/svMisc/R/fileEdit.R (rev 0)
+++ pkg/svMisc/R/fileEdit.R 2012-07-07 16:28:09 UTC (rev 483)
@@ -0,0 +1,56 @@
+fileEdit <- function (file, template = NULL, replace = FALSE, wait = FALSE,
+editor = getOption("fileEditor"))
+{
+ ## Fallback to "editor", in case no fileEditor is provided
+ if (isWin()) {
+ if (!length(editor)) editor <- getOption("editor")
+ } else if (!grepl("%s", editor)) {
+ cmd <- paste('which ', '"', editor, '"', sep = "")
+ if (!length(system(cmd, intern = TRUE))) {
+ ## Fall back to the default editor (if any)
+ editor <- getOption("editor")
+ }
+ }
+
+ ## If the file does not exists, create one (possibly from template)
+ if (isTRUE(replace) || !file.exists(file)) {
+ if (!length(template)) {
+ file.create(file)
+ } else if (file.exists(template)) {
+ file.copy(template, file, overwrite = TRUE, copy.mode = FALSE)
+ } else { # Template file not found!
+ warning("Template file '", template,
+ '" not found, starting from an empty file')
+ file.create(file)
+ }
+ }
+
+ if (!interactive() || editor == "") {
+ ## Do nothing, issue, a warning!
+ warning("Cannot edit '", basename(file),
+ "': no editor or not in interactive mode")
+ return(FALSE)
+ } else {
+ if (grepl("%s", editor)) {
+ cmd <- sprintf(editor, normalizePath(file))
+ } else {
+ cmd <- paste('"', editor, '" "', normalizePath(file), '"', sep = "")
+ }
+ wait <- isTRUE(as.logical(wait))
+ if (wait) {
+ message("Editing the file '", basename(file),
+ "'... Close the editor to continue!")
+ flush.console()
+ }
+ if (isWin()) {
+ res <- try(system(cmd, ignore.stdout = TRUE, ignore.stderr = TRUE,
+ wait = wait, minimized = FALSE, invisible = FALSE,
+ show.output.on.console = FALSE), silent = TRUE)
+ } else {
+ res <- try(system(cmd, ignore.stdout = TRUE, ignore.stderr = TRUE,
+ wait = wait), silent = TRUE)
+ }
+ return(invisible(!inherits(res, "try-error")))
+ }
+}
+
\ No newline at end of file
Modified: pkg/svMisc/R/svMisc-internal.R
===================================================================
--- pkg/svMisc/R/svMisc-internal.R 2012-07-02 13:45:55 UTC (rev 482)
+++ pkg/svMisc/R/svMisc-internal.R 2012-07-07 16:28:09 UTC (rev 483)
@@ -1,6 +1,40 @@
-.onLoad <- function (lib, pkg)
+.onLoad <- function (lib, pkg) {
.initialize()
+ ## Determine where to find the preferred file editor for fileEdit()
+ if (is.null(getOption("fileEditor"))) {
+ if (interactive()) {
+ if (isWin()) {
+ ## First check if Notepad++ is installed in default location...
+ pfdir <- Sys.getenv("ProgramFiles")
+ if (pfdir == "") pfdir <- "c:\\program files"
+ fEdit <- paste(pfdir, "\\Notepad++\\notepad++.exe", sep = "")
+ ## ... otherwise, fallback to notepad.exe
+ if (!file.exists(fEdit)) fEdit <- "notepad.exe"
+ } else if (isMac()) {
+ ## First check if 'edit' is there
+ ## (open files in TextWrangler or BBEdit)
+ if (length(system("which edit", intern = TRUE))) {
+ fEdit <- "edit --wait --resume \"%s\""
+ } else { # Fall back to the default text editor
+ ## Note: use "open -e -n -W \"%s\"" to force use of TextEdit
+ fEdit <- "open -t -n -W \"%s\""
+ }
+ } else { # This is unix
+ ## First check if gedit is there...
+ if (length(system("which gedit", intern = TRUE))) {
+ fEdit <- "gedit"
+ } else if (length(system("which kate", intern = TRUE))) {
+ fEdit <- "kate"
+ } else { # Fall back to something that is likely to be installed
+ fEdit <- "vi"
+ }
+ }
+ options(fileEditor = fEdit)
+ } else options(fileEditor = "") # Inactivate it!
+ }
+}
+
.initialize <- function (replace = TRUE)
{
## Create .svActions if it does not exists yet
Added: pkg/svMisc/man/fileEdit.Rd
===================================================================
--- pkg/svMisc/man/fileEdit.Rd (rev 0)
+++ pkg/svMisc/man/fileEdit.Rd 2012-07-07 16:28:09 UTC (rev 483)
@@ -0,0 +1,109 @@
+\name{fileEdit}
+\alias{fileEdit}
+
+\title{ Invoke an external text editor for a file }
+\description{
+ Edit a text file using an external editor. Possibly wait for the end of the
+ program and care about creating the file (from a template) if it does not
+ exists yet.
+}
+
+\usage{
+fileEdit(file, template = NULL, replace = FALSE, wait = FALSE,
+ editor = getOption("fileEditor"))
+}
+
+\arguments{
+ \item{file}{ path to the file to edit. }
+ \item{template}{ a file to use as template if \code{file} must be created.
+ If \code{NULL}, an empty file is created. }
+ \item{replace}{ force replacement of \code{file} if \code{template} is not null. }
+ \item{wait}{ wait for edition to complete. }
+ \item{editor}{ editor to use. Either the name of the program, or a string
+ containing the command to run, using \%s as replacement tag where to place
+ the filename in the command. }
+}
+
+\value{
+ The function returns \code{TRUE} if it was able to edit the file or
+ \code{FALSE} otherwise, invisibly.
+}
+
+\note{
+ The default editor program, or the command to run is in the \code{fileEditor}
+ option (use \code{getOption("fileEditor")} to retrieve it, and
+ \code{options(fileEditor = "<myowneditor>")} to change it). Default values are
+ determined automatically.
+
+ On Unixes, "gedit", "kate" and "vi" are looked for in that order. Note that
+ there is a gedit plugin to submit code directly to R:
+ \url{http://rgedit.sourceforge.net/}. Since, gedit natively supports a lot
+ of different syntax highlighting, including R, and is lightweight but feature
+ rich, it is recommended as default text editor for \code{fileEdit()} on Unixes.
+
+ On Mac OS X, if the "edit" program exists, it is used (it is the command line
+ program installed by TextWrangler or BBEdit, see
+ \url{http://www.barebones.com/products/}, much more capables text editors
+ than the default TextEdit program), otherwise, the default text editor used
+ by OS X is choosen (default usually to TextEdit). TextWrangler can be
+ installed freely. It can be configured to highlight and submit R code. see
+ \url{http://macsci.jelmerborst.nl/files/textwrangler_and_r.php} for
+ instructions. It features also several tools that makes it a much better
+ choice than TextEdit for \code{fileEdit()} on Mac OS X.
+
+ On Windows, if Notepad++ is installed in its default location, it is used,
+ otherwise, the default "notepad.exe" is used. Notepad++ is a free text editor
+ that is much better suited to edit code or text files that the default
+ Windows' notepad application, in particular because it can handle various line
+ end types (Unix, Mac or Windows) and encodings. It also supports syntax
+ highlighting, code completion and much more. So, it is strongly recommended to
+ install it (see \url{http://notepad-plus-plus.org/}) and use it with
+ \code{fileEdit()}. There is also a plugin to submit code to R directly from
+ Notepad++: \url{http://sourceforge.net/projects/npptor/}.
+
+ Of course, you can use your own text editor, just indicate it in the
+ \code{fileEditor} option. Note, however, that you should use only lighweight
+ and fast starting programs. Also, for the \code{wait = TRUE} argument of
+ \code{fileEdit()}, you must check that R waits for the editor to be closed
+ before furter processing code. In some cases, a little command line program is
+ used to start a larger application (like Komodo Edit/IDE), or the program
+ deleguates to an existing instances and exits immediatelly, even if the file
+ is still edited. These programs are not recommended at all for
+ \code{fileEdit()}.
+
+ If you want to use files that are compatibles between all platforms supported
+ by R itself, you should think about using ASCII encoding as much as possible
+ and the Windows style of line-ending. That way, you ensure that all the
+ default editors will handle those files correctly, including the broken
+ default editor on Windows, notepad.exe, which does not understand at all Mac
+ or Unix line ends!
+}
+
+\author{ Philippe Grosjean <phgrosjean at sciviews.org> }
+
+\seealso{ \code{\link{systemFile}}, \code{\link[base]{file.path}}, \code{\link[base]{file.exists}} }
+
+\examples{
+\dontrun{
+## Create a template file in the tempdir...
+tpl <- tempfile("template", fileext = ".txt")
+cat("Example template file\nto be used with fileEdit()\n", file = tpl)
+
+## ... and edit a new file, starting from that template:
+newf <- tempfile("test", fileext = ".txt")
+fileEdit(newf, template = tpl, wait = TRUE)
+
+## Make sure the content ends with \n, and read it
+cat("\n", file = newf, append = TRUE)
+cat("Your file contains:\n")
+readLines(newf)
+
+## Eliminate both the file and template
+unlink(newf)
+unlink(tpl)
+}
+}
+
+\keyword{ utilities }
+
+\concept{ file edition }
Modified: pkg/svMisc/man/svMisc-package.Rd
===================================================================
--- pkg/svMisc/man/svMisc-package.Rd 2012-07-02 13:45:55 UTC (rev 482)
+++ pkg/svMisc/man/svMisc-package.Rd 2012-07-07 16:28:09 UTC (rev 483)
@@ -12,8 +12,8 @@
\tabular{ll}{
Package: \tab svMisc\cr
Type: \tab Package\cr
- Version: \tab 0.9-65\cr
- Date: \tab 2012-03-31\cr
+ Version: \tab 0.9-67\cr
+ Date: \tab 2012-07-07\cr
License: \tab GPL 2 or above, at your convenience\cr
}
% TODO: add description of main functions here. Also add examples
Modified: pkg/svMisc/man/systemFile.Rd
===================================================================
--- pkg/svMisc/man/systemFile.Rd 2012-07-02 13:45:55 UTC (rev 482)
+++ pkg/svMisc/man/systemFile.Rd 2012-07-07 16:28:09 UTC (rev 483)
@@ -44,7 +44,7 @@
\author{ Philippe Grosjean <phgrosjean at sciviews.org> }
-\seealso{ \code{\link[base]{file.path}}, \code{\link[base]{file.exists}} }
+\seealso{ \code{\link{fileEdit}}, \code{\link[base]{file.path}}, \code{\link[base]{file.exists}} }
\examples{
systemFile("INDEX", package = "base")
More information about the Sciviews-commits
mailing list