Skip to content

Commit 9f9e8cd

Browse files
committed
prettier output tables
1 parent 84f57df commit 9f9e8cd

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

R/notebook.R

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,27 @@ r$run(function() {
8080
)
8181
} else if (out$visible) {
8282
if (inherits(out$value, "data.frame")) {
83-
text <- utils::capture.output(knitr::kable(out$value, format = "html"))
83+
table <- head(out$value, 10)
84+
list(
85+
id = id,
86+
type = "table",
87+
result = list(
88+
html = knitr::kable(table, format = "html"),
89+
markdown = paste0(knitr::kable(table, format = "markdown"), collapse = "\n")
90+
)
91+
)
92+
} else {
93+
list(
94+
id = id,
95+
type = "text",
96+
result = paste0(text, collapse = "\n")
97+
)
8498
}
85-
86-
list(
87-
id = id,
88-
type = "text",
89-
result = paste0(text, collapse = "\n"),
90-
tempdir = plot_dir
91-
)
9299
} else {
93100
list(
94101
id = id,
95102
type = "text",
96-
result = "",
97-
tempdir = plot_dir
103+
result = ""
98104
)
99105
}
100106
}, error = function(e) {

src/notebook.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { spawn, ChildProcess } from 'child_process';
44
import { dirname } from 'path';
55
import { inlineAll } from './inlineScripts';
66

7-
87
interface RKernelRequest {
98
id: number;
109
type: 'eval' | 'cancel';
@@ -339,7 +338,6 @@ export class RNotebookProvider implements vscode.NotebookContentProvider, vscode
339338
}
340339

341340
async renderHtmlOutput(response) {
342-
343341
const html = (await vscode.workspace.fs.readFile(vscode.Uri.parse(response.result))).toString();
344342
const htmlDir = dirname(response.result)
345343
const htmlInline = await inlineAll(html, htmlDir)
@@ -358,6 +356,26 @@ export class RNotebookProvider implements vscode.NotebookContentProvider, vscode
358356
}
359357
}
360358

359+
async renderTableOutput(response) {
360+
const html = `
361+
<div id="chunk-table-${response.id}"></div>
362+
<script>
363+
$('#example').DataTable({
364+
data: JSON.parse('${JSON.stringify(response.result)}')
365+
})
366+
</script>
367+
`
368+
369+
return {
370+
outputKind: vscode.CellOutputKind.Rich,
371+
data: {
372+
'text/html': response.result.html,
373+
'text/markdown': response.result.markdown,
374+
'text/plain': response.result.markdown
375+
},
376+
}
377+
}
378+
361379
async renderOutput(cell, response) {
362380

363381
switch (response.type) {
@@ -382,6 +400,10 @@ export class RNotebookProvider implements vscode.NotebookContentProvider, vscode
382400
}];
383401
break;
384402
}
403+
case 'table': {
404+
cell.outputs = [await this.renderTableOutput(response)];
405+
break;
406+
}
385407
case 'error': {
386408
cell.metadata.runState = vscode.NotebookCellRunState.Error;
387409
cell.outputs = [{

0 commit comments

Comments
 (0)