Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
*.ipynb_checkpoints
.Rproj.user
.RData
.Rhistory
*.pdf
5 changes: 5 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ Imports:
jsonlite (>= 0.9.6),
uuid,
digest
Collate:
'options.R'
'execution.r'
'help.r'
'kernel.r'
216 changes: 209 additions & 7 deletions Demo.ipynb

Large diffs are not rendered by default.

77 changes: 74 additions & 3 deletions Display.ipynb

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions IRkernel.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 4
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ export(set_plot_options)
import(digest)
import(evaluate)
import(methods)
import(repr)
import(rzmq)
import(uuid)
importFrom(IRdisplay,display)
importFrom(IRdisplay,display_alternatives)
importFrom(IRdisplay,display_png)
importFrom(base64enc,base64encode)
importFrom(jsonlite,fromJSON)
importFrom(jsonlite,toJSON)
44 changes: 14 additions & 30 deletions R/execution.r
Original file line number Diff line number Diff line change
@@ -1,28 +1,7 @@
displayenv = environment(display)

plot_options=new.env() # environment for storing plot-options

#'Set options for plotting
#'
#'IRkernel displays plots in the notebook with calls to png().
#'This function allows to set the variables that will be passed on to
#'png(), for example width or height, see help(png).
#' @param ... options that will be passed to png()
#' @export
set_plot_options <- function(...){
options <- list(...)
for(opt in names(options)){
assign( opt, options[[opt]], plot_options )
}
}
#' @include options.R
NULL

#'Get options for plotting
#'
#'Use set_plot_options() for modifying.
#' @export
get_plot_options <- function(){
return(as.list(plot_options))
}
displayenv = environment(display)

lappend <- function(lst, obj) {
# I hope this isn't the best way to do this.
Expand All @@ -43,6 +22,11 @@ plot_builds_upon <- function(prev, current) {
return((lcurrent >= lprev) && (identical(current[[1]][1:lprev], prev[[1]][1:lprev])))
}

# needed to easily encode reprs as json.
setOldClass('repr')
asJSON <- jsonlite:::asJSON
setMethod('asJSON', 'repr', function(x, ...) jsonlite:::asJSON(structure(x, class = NULL, repr.format = NULL), ...))

Executor = setRefClass("Executor",
fields=c("execution_count", "payload", "err", "interrupted", "kernel",
"last_recorded_plot"),
Expand Down Expand Up @@ -83,12 +67,12 @@ execute = function(request) {
payload <<- lappend(payload, list(source='page', text=paste(text, collapse="\n")))
})

send_plot = function(plotobj) {
tf = tempfile(fileext='.png')
do.call(png, c(list(filename=tf), get_plot_options()))
replayPlot(plotobj)
dev.off()
display_png(file=tf)
send_plot <- function(plotobj) {
params <- list()
for (mime in getOption('jupyter.plot_mimetypes')) {
params[[mime]] <- mime2repr[[mime]](plotobj)
}
display(params)
}

err <<- list()
Expand Down
28 changes: 22 additions & 6 deletions R/help.r
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
#'An R kernel for IPython.
#' An R kernel for IPython.
#'
#' IPython's modern interfaces, including the IPython Notebook, speak a JSON+ZMQ
#' protocol to a 'kernel' which is responsible for executing code. This protocol
#' is language agnostic, so other languages can take advantage of IPython's rich
#' UI by implementing a kernel. This package is a kernel for the R language.
#'
#' @section Options:
#'
#' The following can be set/read via \code{options(opt.name = ...)} / \code{getOption('opt.name')}
#'
#' \describe{
#' \item{\code{jupyter.plot_mimetypes}}{
#' The plot formats emitted to the frontend when a plot is displayed.
#' (default: image/png, application/pdf, and image/svg+xml)
#' }
#' }
#'
#'IPython's modern interfaces, including the IPython Notebook, speak a JSON+ZMQ
#'protocol to a 'kernel' which is responsible for executing code. This protocol
#'is language agnostic, so other languages can take advantage of IPython's rich
#'UI by implementing a kernel. This package is a kernel for the R language.
#' @export main
#'
#' @import repr
#' @import methods
#' @import rzmq
#' @import uuid
#' @import digest
#' @import evaluate
#' @importFrom jsonlite fromJSON toJSON
#' @importFrom IRdisplay display display_png
#' @importFrom base64enc base64encode
#' @importFrom IRdisplay display display_png display_alternatives
#'
#' @docType package
#' @name IRkernel
#' @aliases IRkernel IRkernel-package
Expand Down
28 changes: 28 additions & 0 deletions R/options.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#' @export
set_plot_options <- function(...) {
.Deprecated('options', msg = 'use the `repr.plot.*` options from the repr package instead')
opts <- list(...)
names(opts) <- paste0('repr.plot.', names(opts))
do.call(options, opts)
}

#' @export
get_plot_options <- function() {
.Deprecated('getOption', msg = 'use the `repr.plot.*` options from the repr package instead')
all.opts <- options()
plot.opt.idx <- grep('^repr.plot', names(all.opts))
plot.opts <- all.opts[plot.opt.idx]
names(plot.opts) <- gsub('^repr\\.plot\\.', '', names(plot.opts))
return(as.list(plot.opts))
}

# all plot mime types. default value for the jupyter.plot_mimetypes option
plot_mimetypes <- c(
'image/png',
'application/pdf',
'image/svg+xml')

.onLoad = function(libname = NULL, pkgname = NULL) {
if (is.null(getOption('jupyter.plot_mimetypes')))
options(jupyter.plot_mimetypes = plot_mimetypes)
}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ you'll lose all your variables if it crashes.
install.packages('RCurl')
library(devtools)
install_github('armstrtw/rzmq')
install_github("IRkernel/IRdisplay")
install_github("IRkernel/IRkernel")
install_github('IRkernel/repr')
install_github('IRkernel/IRdisplay')
install_github('IRkernel/IRkernel')

# Only if you have IPython 3 or above installed:
IRkernel::installspec()
Expand Down
12 changes: 12 additions & 0 deletions man/IRkernel.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,16 @@ protocol to a 'kernel' which is responsible for executing code. This protocol
is language agnostic, so other languages can take advantage of IPython's rich
UI by implementing a kernel. This package is a kernel for the R language.
}
\section{Options}{


The following can be set/read via \code{options(opt.name = ...)} / \code{getOption('opt.name')}

\describe{
\item{\code{jupyter.plot_mimetypes}}{
The plot formats emitted to the frontend when a plot is displayed.
(default: image/png, application/pdf, and image/svg+xml)
}
}
}

12 changes: 0 additions & 12 deletions man/get_plot_options.Rd

This file was deleted.

2 changes: 1 addition & 1 deletion man/installspec.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
\alias{installspec}
\title{Install the kernelspec to tell IPython (>= 3) about IRkernel}
\usage{
installspec(user = F)
installspec(user = T)
}
\arguments{
\item{user}{Install into user directory ~/.ipython or globally?}
Expand Down
17 changes: 0 additions & 17 deletions man/set_plot_options.Rd

This file was deleted.