Skip to content

Commit

Permalink
Switch back to checking bitmap-level diff
Browse files Browse the repository at this point in the history
For the moment, checking for bitmap-level differences seems to be a
better indicator than recordPlot() for if a plot has changed.
  • Loading branch information
georgestagg committed Mar 21, 2024
1 parent a1748ed commit 686c8ba
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions src/webr_kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { KernelMessage } from '@jupyterlab/services';
import { IKernel } from '@jupyterlite/kernel';

import { Console, WebR, Shelter} from 'webr';
import { RObject, RList, RCharacter, RLogical } from 'webr';
import { RList, RCharacter, RLogical } from 'webr';

const webRVersion = "0.3.0-rc.0";
const baseRVersion = "4.3.3";
Expand All @@ -15,7 +15,7 @@ export class WebRKernel extends BaseKernel {
init: Promise<void>;
#webRConsole: Console;
#bitmapCanvas: HTMLCanvasElement;
#lastRecord: RObject | null = null;
#lastPlot: string | null = null;

constructor(options: IKernel.IOptions) {
super(options);
Expand Down Expand Up @@ -190,7 +190,6 @@ export class WebRKernel extends BaseKernel {
dev.set(${dev})
dev.copy(which = capture_dev)
dev.off(capture_dev)
recordPlot()
}, silent = TRUE)
`);
const image = capturePlot.images[0];
Expand All @@ -199,16 +198,8 @@ export class WebRKernel extends BaseKernel {
this.#bitmapCanvas.getContext('bitmaprenderer')?.transferFromImageBitmap(image);
const plotData = this.#bitmapCanvas.toDataURL('image/png');

// Send plot data to client if a new.plot() has been triggered or if
// the plot has changed since last time
const plotChanged = await this.webR.evalRBoolean('!identical(a, b)', {
env: {
a: this.#lastRecord,
b: capturePlot.result,
}
})
if (newPlot || plotChanged) {
this.#lastRecord = capturePlot.result;
if (newPlot || plotData !== this.#lastPlot) {
this.#lastPlot = plotData;
this.displayData({
data: {
'image/png': plotData.split(",")[1],
Expand Down

0 comments on commit 686c8ba

Please sign in to comment.