Learn R Programming

svKomodo (version 1.0.0)

koCmd: Connect to the SciViews-K (Komodo Edit/IDE) socket server and run JavaScript code in Komodo

Description

If Komodo Edit/IDE with the SciViews-K extension is running on your machine, you can connect to its socket server and run javascript code in it with this function.

Usage

koCmd(
  cmd,
  data = NULL,
  async = FALSE,
  host = getOption("ko.host"),
  port = getOption("ko.port"),
  kotype = getOption("ko.kotype"),
  timeout = 2,
  type = c("js", "rjsonp", "output"),
  pad = NULL,
  ...
)

Arguments

cmd

the JavaScript you want to execute in Komodo Edit, in a character

data

if a named list, replace <<<name>>> in cmd for each name of the list by its component first. If a character string, replace <<<data>>> in cmd. If NULL (by default), do nothing to cmd before submitting it. See the last examples for using data.

async

not used yet!

host

the host where Komodo is located. Currently, only localhost is accepted. Can be changed by setting options(ko.host = ....).

port

the socket port where the SciViews-K server is listening, by default, it is port 7052. Can be changed by setting options(ko.port = ....).

kotype

the type of Komodo server in use. Currently (SciViews-K >= 0.9-25), it can be either "socket" or "file".

timeout

number of seconds to wait for a response.

type

which type of Komodo command do we send? If type = "js" (by default), cmd is considered to be JavaScript code to be evaluated in Komodo. If type = "rjsonp", cmd is parsed as RJson object with padding (included in a JavaScript function call) and will be evaluated as such in Komodo. if type = "output", the string in cmd is considered to be some R output and will be displayed in the Komodo local R console (no evaluation).

pad

a string naming the JavaScript function to run in Komodo, with the constructed RJson object as argument. If NULL (by default), the RJson object is evaluated directly without padding.

...

further arguments to pass to toRjson().

Value

Returns the results of the evaluation of the javascript code in Komodo Edit/IDE if async = FALSE. Note that async = TRUE is not supported yet.

If there is an error, or cmd is an invalid JavaScript code, a character string containing javascript error message is returned (this is changed from version 0.9-47, previously a 'try-error' was returned).

Details

Komodo Edit (https://www.activestate.com/products/komodo-ide/) is an Open Source (MPL, GPL & LGPL) editor based on the excellent Mozilla platform and the powerful Scintilla text editor widget. It runs on many Linux distributions, on Windows and on MacOS. Komodo IDE was a commercial equivalent, but with many tools for developers, especially targeting languages like Perl, Tcl, Python, Ruby, etc. This product is now freely distributed as well and Komodo Edit was deprecated. However, the project does not appear to be actively maintained any more and it may not work on more recent MacOS or Windows versions.

koCmd() can only talk to Komodo if the SciViews-K socket server is installed. This server is contained in the SciViews-K extension that you can download from https://github.com/SciViews/sciviewsk. See Komodo documentation to know how to install this extension (drag and drop of the extension on the Komodo window works in most platforms).

See Also

svSocket::startSocketServer(), svSocket::processSocket()

Examples

Run this code
# NOT RUN {
# Make sure you have started Komodo Edit or IDE with the SciViews-K extension
# installed on the same machine, and the socket server started and then...

# Send JavaScript commands to Komodo
# Alert box in Komodo, and then reply to R
koCmd(c('alert("Hello from R!");',
  'sv.socket.serverWrite("Hello from OpenKomodo (" + ko.interpolate.currentFilePath() + ")");'))

# Open a web page wih Komodo configuration
koCmd("ko.open.URI('about:config','browser');")

# Get info from Komodo
koCmd("sv.socket.serverWrite(ko.logging.getStack());")

# Passing a large amount of data to Komodo, and then, back to R
koCmd(paste0('sv.socket.serverWrite("', rep(paste(iris, collapse = "\\\\n"), 10), '");'))

# It is easier to use 'data =' instead of paste() for constructing the JS command
koCmd('alert("<<<data>>>");', data = search())

# Using a named list for data to replace in the cmd
koCmd('alert("This is R version <<<major>>>.<<<minor>>>");', R.version)

# Sending incorrect JavaScript instruction
koCmd('nonexistingJSfunction();')
# Should return something like:
# "ReferenceError: nonexistingJSfunction is not defined"

# Sending RJsonP (RJson with padding) instruction to Komodo
koCmd("Hello with RJsonP!", type = "rjsonp", pad = "alert")

# This is more useful to pass complex R objects to Komodo
koCmd(head(iris), type = "rjsonp", pad = "sv.socket.serverWrite")

# Send simple text (no evaluation) to the Komodo R console
koCmd("Hello again from R!", type = "output")
# }

Run the code above in your browser using DataLab