@@ -3,7 +3,7 @@ import { IKernel } from '@jupyterlite/kernel';
3
3
import { ISignal , Signal } from '@lumino/signaling' ;
4
4
import { v4 as uuid } from 'uuid' ;
5
5
import { WebR } from '@georgestagg/webr' ;
6
- import { RObjData } from '@georgestagg/webr/dist/webR/robj' ;
6
+ import { RObjData , RInteger } from '@georgestagg/webr/dist/webR/robj' ;
7
7
8
8
export namespace WebRKernel {
9
9
export interface IOptions extends IKernel . IOptions { }
@@ -37,6 +37,7 @@ export class WebRKernel implements IKernel {
37
37
this . #webR. init ( ) ,
38
38
this . #webR. installPackages ( [ 'svglite' ] ) ,
39
39
this . #webR. evalRCode ( 'library(svglite)' ) ,
40
+ this . #webR. evalRCode ( 'options(device=function(...){ pdf(...); dev.control("enable") })' ) ,
40
41
] . reduce ( ( p , x ) => p . then ( ( ) => x ) , Promise . resolve ( ) ) ;
41
42
}
42
43
@@ -121,6 +122,33 @@ export class WebRKernel implements IKernel {
121
122
}
122
123
}
123
124
} ) ;
125
+
126
+ const dev = ( await this . #webR. evalRCode ( 'dev.cur()' ) ) . result as RInteger ;
127
+ const devNumber = await dev . toNumber ( ) ;
128
+ if ( devNumber && devNumber > 1 ) {
129
+ // A non-null graphics device is currently being used, let's copy it
130
+ await this . #webR. evalRCode ( `
131
+ try({
132
+ dev.copy(function(...) {
133
+ svglite(width = 6.25, height = 5, ...)
134
+ }, "/tmp/_webRplots.svg")
135
+ dev.off()
136
+ }, silent=TRUE)
137
+ ` ) ;
138
+ const plotData = await this . #webR. getFileData ( '/tmp/_webRplots.svg' ) ;
139
+ console . log ( plotData ) ;
140
+ this . sendExecuteResult ( msg , {
141
+ execution_count : this . #executionCounter,
142
+ data : {
143
+ 'image/svg+xml' : new TextDecoder ( ) . decode ( plotData ) ,
144
+ } ,
145
+ metadata : {
146
+ 'image/svg+xml' : {
147
+ isolated : true ,
148
+ } ,
149
+ } ,
150
+ } ) ;
151
+ }
124
152
if ( status === 'ok' ) {
125
153
this . sendExecuteReply ( msg , {
126
154
status : status ,
0 commit comments