Skip to content

Commit

Permalink
Merge pull request #670 from renkun-ken/session-watcher-on
Browse files Browse the repository at this point in the history
Enable r.sessionWatcher by default
  • Loading branch information
renkun-ken committed Jun 20, 2021
2 parents e980d40 + 9aac899 commit 9230fb0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 37 deletions.
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ Yes / No
// Use bracketed paste mode
"r.bracketedPaste": false,

// Enable R session watcher (experimental)
"r.sessionWatcher": false,
// Enable R session watcher
"r.sessionWatcher": true,

// Delay in milliseconds before sending each line to rterm (only applies if r.bracketedPaste is false)
"r.rtermSendDelay": 8,
Expand Down
6 changes: 3 additions & 3 deletions R/vsc.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ request <- function(command, ...) {
cat(get_timestamp(), file = request_lock_file)
}

capture_str <- function(object) {
capture_str <- function(object, max.level = getOption("vsc.str.max.level", 0)) {
paste0(
utils::capture.output(
utils::str(object,
max.level = getOption("vsc.str.max.level", 0),
max.level = max.level,
give.attr = FALSE,
vec.len = 1
)
Expand Down Expand Up @@ -361,7 +361,7 @@ if (show_view) {
type = typeof(obj),
length = length(obj),
size = as.integer(object.size(obj)),
value = trimws(capture_str(obj)),
value = trimws(capture_str(obj, 0)),
stringsAsFactors = FALSE,
check.names = FALSE
)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1167,8 +1167,8 @@
},
"r.sessionWatcher": {
"type": "boolean",
"default": false,
"description": "Enable R session watcher (experimental). Required for workspace viewer. Restart required to take effect."
"default": true,
"description": "Enable R session watcher. Required for workspace viewer and most features to work with an R session. Restart required to take effect."
},
"r.rtermSendDelay": {
"type": "integer",
Expand Down
69 changes: 39 additions & 30 deletions src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,56 @@ import { commands, extensions, window, workspace } from 'vscode';

import { runTextInTerm } from './rTerminal';
import { getWordOrSelection } from './selection';
import { checkForSpecialCharacters, checkIfFileExists, delay } from './util';
import { config, checkForSpecialCharacters, checkIfFileExists, delay } from './util';

export async function previewEnvironment(): Promise<void> {
if (!checkcsv()) {
return;
if (config().get('sessionWatcher')) {
await runTextInTerm('View(globalenv())');
} else {
if (!checkcsv()) {
return;
}
const tmpDir = makeTmpDir();
const pathToTmpCsv = `${tmpDir}/environment.csv`;
const envName = 'name=ls()';
const envClass = 'class=sapply(ls(), function(x) {class(get(x, envir = parent.env(environment())))[1]})';
const envOut = 'out=sapply(ls(), function(x) {capture.output(str(get(x, envir = parent.env(environment()))), silent = T)[1]})';
const rWriteCsvCommand = 'write.csv(data.frame('
+ `${envName},`
+ `${envClass},`
+ `${envOut}), '`
+ `${pathToTmpCsv}', row.names=FALSE, quote = TRUE)`;
await runTextInTerm(rWriteCsvCommand);
await openTmpCSV(pathToTmpCsv, tmpDir);
}
const tmpDir = makeTmpDir();
const pathToTmpCsv = `${tmpDir}/environment.csv`;
const envName = 'name=ls()';
const envClass = 'class=sapply(ls(), function(x) {class(get(x, envir = parent.env(environment())))[1]})';
const envOut = 'out=sapply(ls(), function(x) {capture.output(str(get(x, envir = parent.env(environment()))), silent = T)[1]})';
const rWriteCsvCommand = 'write.csv(data.frame('
+ `${envName},`
+ `${envClass},`
+ `${envOut}), '`
+ `${pathToTmpCsv}', row.names=FALSE, quote = TRUE)`;
await runTextInTerm(rWriteCsvCommand);
await openTmpCSV(pathToTmpCsv, tmpDir);
}

export async function previewDataframe(): Promise<boolean> {
if (!checkcsv()) {
return undefined;
}
if (config().get('sessionWatcher')) {
const symbol = getWordOrSelection();
await runTextInTerm(`View(${symbol})`);
} else {
if (!checkcsv()) {
return undefined;
}

const dataframeName = getWordOrSelection();
const dataframeName = getWordOrSelection();

if (!checkForSpecialCharacters(dataframeName)) {
void window.showInformationMessage('This does not appear to be a dataframe.');
if (!checkForSpecialCharacters(dataframeName)) {
void window.showInformationMessage('This does not appear to be a dataframe.');

return false;
}
return false;
}

const tmpDir = makeTmpDir();
const tmpDir = makeTmpDir();

// Create R write CSV command. Turn off row names and quotes, they mess with Excel Viewer.
const pathToTmpCsv = `${tmpDir}/${dataframeName}.csv`;
const rWriteCsvCommand = `write.csv(${dataframeName}, `
+ `'${pathToTmpCsv}', row.names = FALSE, quote = FALSE)`;
await runTextInTerm(rWriteCsvCommand);
await openTmpCSV(pathToTmpCsv, tmpDir);
// Create R write CSV command. Turn off row names and quotes, they mess with Excel Viewer.
const pathToTmpCsv = `${tmpDir}/${dataframeName}.csv`;
const rWriteCsvCommand = `write.csv(${dataframeName}, `
+ `'${pathToTmpCsv}', row.names = FALSE, quote = FALSE)`;
await runTextInTerm(rWriteCsvCommand);
await openTmpCSV(pathToTmpCsv, tmpDir);
}
}

async function openTmpCSV(pathToTmpCsv: string, tmpDir: string) {
Expand Down

0 comments on commit 9230fb0

Please sign in to comment.