Skip to content

Commit da483c5

Browse files
committed
Update notebook
1 parent ea97df6 commit da483c5

5 files changed

Lines changed: 27 additions & 26 deletions

File tree

R/client.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
con <- socketConnection(host = "127.0.0.1", port = 8781, blocking = TRUE,
2-
server = FALSE, open = "w", timeout = 86400)
3-
on.exit(close(con))
1+
con <- socketConnection(host = "127.0.0.1", port = 8708,
2+
blocking = TRUE, server = FALSE, open = "r+")
43
request <- list(time = format(Sys.time()), expr = "1+1")
54
json <- jsonlite::toJSON(request, auto_unbox = TRUE)
65
writeLines(json, con)
7-
response <- readLines(con)
6+
response <- readLines(con, n = 1)
87
response
8+
close(con)

R/notebook.R

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,21 @@ local({
99

1010
print(ls.str(env))
1111
while (TRUE) {
12-
con <- try(socketConnection(host = "127.0.0.1", port = env$port,
12+
con <- socketConnection(host = "127.0.0.1", port = env$port,
1313
blocking = TRUE, server = TRUE,
14-
open = "r+", timeout = 86400L), silent = TRUE)
15-
if (inherits(con, "try-error")) {
16-
message(con, "\n")
17-
} else {
18-
tryCatch({
19-
request <- jsonlite::fromJSON(readLines(con, n = 1))
20-
cat(sprintf("[%s] %s\n",
21-
request$time, request$expr))
22-
expr <- parse(text = request$expr)
23-
res <- try(eval(expr, globalenv()), silent = TRUE)
24-
str <- list(
25-
type = if (inherits(res, "try-error")) "error" else "output",
26-
result = utils::capture.output(print(res))
27-
)
28-
writeLines(jsonlite::toJSON(str), con)
29-
}, error = function(e) message(e, "\n"),
30-
warning = function(w) message(w, "\n"),
31-
finally = close(con))
32-
}
14+
open = "r+")
15+
line <- readLines(con, n = 1)
16+
request <- jsonlite::fromJSON(line)
17+
cat(sprintf("[%s] %s\n",
18+
request$time, request$expr))
19+
expr <- parse(text = request$expr)
20+
res <- try(eval(expr, globalenv()), silent = TRUE)
21+
str <- list(
22+
type = if (inherits(res, "try-error")) "error" else "output",
23+
result = utils::capture.output(print(res))
24+
)
25+
result <- jsonlite::toJSON(str, force = TRUE)
26+
writeLines(result, con)
27+
close(con)
3328
}
3429
})

package-lock.json

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@
566566
"datatables.net-fixedheader-jqui": "^3.1.7",
567567
"fotorama": "^4.6.4",
568568
"fs-extra": "^9.0.1",
569+
"get-port": "^5.1.1",
569570
"jquery": "^3.5.1",
570571
"jquery.json-viewer": "^1.4.0",
571572
"popper.js": "^1.16.1",

src/notebook.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class RNotebook implements vscode.Disposable {
9999
}
100100

101101
public async eval(cell: vscode.NotebookCell): Promise<string> {
102-
this.kernel.start();
102+
await this.kernel.start();
103103
return this.kernel.eval(cell);
104104
}
105105
}

0 commit comments

Comments
 (0)