Skip to content

Commit 625ae35

Browse files
authored
Merge pull request #184 from renkun-ken/update-readme
Improve session watcher initialization
2 parents b6597b5 + 75a5edd commit 625ae35

5 files changed

Lines changed: 80 additions & 16 deletions

File tree

R/.Rprofile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
if (nzchar(Sys.getenv("R_PROFILE_USER_OLD"))) {
2+
source(Sys.getenv("R_PROFILE_USER_OLD"))
3+
} else if (file.exists(".Rprofile")) {
4+
source(".Rprofile")
5+
} else if (file.exists("~/.Rprofile")) {
6+
source("~/.Rprofile")
7+
}
8+
9+
if (is.null(getOption("vscodeR"))) {
10+
source(file.path(Sys.getenv(if (.Platform$OS.type == "windows") "USERPROFILE" else "HOME"), ".vscode-R", "init.R"))
11+
}

R/init.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
if (interactive() && !identical(Sys.getenv("RSTUDIO"), "1")) {
1+
if (interactive() && Sys.getenv("TERM_PROGRAM") == "vscode") {
22
if (requireNamespace("jsonlite", quietly = TRUE)) {
33
local({
44
pid <- Sys.getpid()

README.md

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,53 @@ This extension contributes the following settings:
6969
An opt-in experimental R session watcher is implemented to support the following features:
7070

7171
* Watch any R session
72-
* Show value of session symbol on hover
73-
* Provide completion for session symbol
74-
* `View()` data frames and list objects
75-
* Show plot output on update
76-
* Show htmlwidgets and shiny apps
72+
* Show value of session symbols on hover
73+
* Provide completion for session symbols
74+
* `View()` any objects including data frames and list objects
75+
* Show plot output on update and plot history
76+
* Show htmlwidgets, documentation and shiny apps in WebView
7777

78-
To enable this feature, turn on `r.sessionWatcher` and append the following code to your `.Rprofile` (in your home directory):
78+
### Basic usage
7979

80-
```r
81-
source(file.path(if (.Platform$OS.type == "windows") file.path(Sys.getenv("HOMEDRIVE"), Sys.getenv("HOMEPATH")) else Sys.getenv("HOME"), ".vscode-R", "init.R"))
82-
```
80+
To enable this feature, turn on `r.sessionWatcher` in VSCode settings, reload or restart VSCode, and the session watcher will be activated automatically
81+
on R sessions launched by vscode-R via `R: Create R Terminal` command.
82+
83+
*If you previously appended the `source(...)` line to `~/.Rprofile`, you may safely remove it since the configuration for basic usage is automated. It is
84+
now only necessary for advanced usage described below.*
85+
86+
### Advanced usage (for self-managed R sessions)
87+
88+
For advanced users to work with self-managed R sessions (e.g. manually launched R terminal or started in `tmux` or `screen` window), some extra
89+
configuration is needed. Follow the steps below to make R session watcher work with any external R session:
90+
91+
1. Turn on `r.sessionWatcher` in VSCode settings.
92+
2. Edit `.Rprofile` in your home directory by running the following code in R:
93+
94+
```r
95+
file.edit("~/.Rprofile")
96+
```
97+
98+
3. Append the following code to the file:
99+
100+
```r
101+
source(file.path(Sys.getenv(if (.Platform$OS.type == "windows") "USERPROFILE" else "HOME"), ".vscode-R", "init.R"))
102+
```
103+
104+
4. Restart or Reload Window in VSCode
105+
106+
If the workspace folder you open in VSCode already has a `.Rprofile`, you need to append the code above in this file too because `~/.Rprofile` will not
107+
be executed when a local `.Rprofile` is found.
108+
109+
The script only works with environment variable `TERM_PROGRAM=vscode`. the script will not take effect with R sessions started in a `tmux` or `screen` window that does not have it, unless this environment variable is manually set before sourcing `init.R`, for example, you may insert a line `Sys.setenv(TERM_PROGRAM="vscode")` before it.
110+
111+
### How to disable it
112+
113+
For the case of basic usage, turning off `r.sessionWatcher` in VSCode settings is sufficient
114+
to disable R session watcher.
115+
116+
For the case of advanced usage, user should, in addition, comment out or remove the `source(...)` line appended to `~/.Rprofile`.
117+
118+
### How it works
83119

84120
This script writes the metadata of symbols in the global environment and plot file to `${workspaceFolder}/.vscode/vscode-R/PID` where `PID` is the R process ID. It also captures user input and append command lines to `${workspaceFolder}/.vscode/vscode-R/response.log`, which enables the communication between vscode-R and a live R sesson.
85121

@@ -91,7 +127,7 @@ R sessions started from the workspace root folder will be automatically attached
91127
* R session started by vscode-R or user
92128
* R session in a `tmux` or `screen` window
93129
* Switch between multiple running R sessions
94-
* [Remote Development](https://code.visualstudio.com/docs/remote/remote-overview)
130+
* [Remote Development](https://code.visualstudio.com/docs/remote/remote-overview) via SSH, WSL and Docker
95131

96132
The status bar item shows the process id of the attached R session. Click the status bar item and it will
97133
attach to currently active session.
@@ -100,6 +136,9 @@ attach to currently active session.
100136

101137
![R session watcher](https://user-images.githubusercontent.com/4662568/70815935-65391480-1e09-11ea-9ad6-7ebbebf9a9c8.gif)
102138

139+
*The R terminal used in the screenshot is [radian](https://github.com/randy3k/radian) which is cross-platform and
140+
supports syntax highlighting, auto-completion and many other features.*
141+
103142
## TODO
104143

105144
* Debug

src/rTerminal.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
"use strict";
22

3+
import os = require("os");
4+
import path = require("path");
5+
36
import { pathExists } from "fs-extra";
47
import { isDeepStrictEqual } from "util";
5-
import { commands, Terminal, window } from "vscode";
8+
import { commands, Terminal, window, TerminalOptions } from "vscode";
69

710
import { getSelection } from "./selection";
811
import { removeSessionFiles } from "./session";
@@ -18,7 +21,18 @@ export function createRTerm(preserveshow?: boolean): boolean {
1821
const termOpt: string[] = config.get("rterm.option");
1922
pathExists(termPath, (err, exists) => {
2023
if (exists) {
21-
rTerm = window.createTerminal(termName, termPath, termOpt);
24+
let termOptions: TerminalOptions = {
25+
name: termName,
26+
shellPath: termPath,
27+
shellArgs: termOpt
28+
};
29+
if (config.get("sessionWatcher")) {
30+
termOptions.env = {
31+
R_PROFILE_USER_OLD: process.env.R_PROFILE_USER,
32+
R_PROFILE_USER: path.join(os.homedir(), ".vscode-R", ".Rprofile")
33+
};
34+
}
35+
rTerm = window.createTerminal(termOptions);
2236
rTerm.show(preserveshow);
2337
return true;
2438
}

src/session.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ const sessionDir = path.join(".vscode", "vscode-R");
2020

2121
export function deploySessionWatcher(extensionPath: string) {
2222
resDir = path.join(extensionPath, "dist", "resources");
23-
const srcPath = path.join(extensionPath, "R", "init.R");
2423
const targetDir = path.join(os.homedir(), ".vscode-R");
2524
if (!fs.existsSync(targetDir)) {
2625
fs.mkdirSync(targetDir);
2726
}
28-
const targetPath = path.join(targetDir, "init.R");
29-
fs.copySync(srcPath, targetPath);
27+
28+
fs.copySync(path.join(extensionPath, "R", "init.R"), path.join(targetDir, "init.R"));
29+
fs.copySync(path.join(extensionPath, "R", ".Rprofile"), path.join(targetDir, ".Rprofile"));
3030
}
3131

3232
export function startResponseWatcher(sessionStatusBarItem: StatusBarItem) {

0 commit comments

Comments
 (0)