Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions R/session/vsc.R
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,67 @@ if (show_view) {
stop("data must be a data.frame or a matrix")
}
}
##############view S4 object################
traverse_S4 <- function(obj, max_depth = 5, current_depth = 0, length_threshold = 20, head_length = 5) {
if (current_depth > max_depth) {
return("Max depth reached")
}

if (isS4(obj)) {
slots <- slotNames(obj)
result <- list()
for (slot in slots) {
content <- slot(obj, slot)
result[[slot]] <- traverse_S4(content, max_depth, current_depth + 1, length_threshold, head_length)
}
return(result)
} else if (is.list(obj)) {
result <- lapply(obj, function(x) {
traverse_S4(x, max_depth, current_depth + 1, length_threshold, head_length)
})
return(result)
} else if (is.vector(obj)) {
len <- length(obj)
if (len > length_threshold) {
obj_subset <- obj[1:head_length]
obj_subset <- c(obj_subset, paste("Length:", len, "- show", head_length, "of them"))
return(obj_subset)
} else {
return(obj)
}
} else if (is.vector(obj) || is.double(obj) || is.character(obj)) {
len <- length(obj)
if (len > length_threshold) {
obj_subset <- obj[1:head_length]
obj_subset <- c(obj_subset, paste("Length:", len, "- show", head_length, "of them"))
return(obj_subset)
} else {
return(obj)
}
} else if (is.factor(obj)) {
levels_len <- length(levels(obj))
if (levels_len > length_threshold) {
levels_subset <- levels(obj)[1:head_length]
levels_subset <- c(levels_subset, paste("Length of level:", levels_len, "- show", head_length, "of them"))
return(list(levels = levels_subset))
} else {
return(list(levels = levels(obj)))
}
} else if (is.matrix(obj) || is.data.frame(obj)) {
nrows <- nrow(obj)
if (nrows > length_threshold) {
obj_subset <- head(obj, head_length)
obj_subset$info <- paste("nrows:", nrows, "- show", head_length, "of them")
return(obj_subset)
} else {
return(obj)
}
} else {
return("Unknown type")
}
}



show_dataview <- function(x, title, uuid = NULL,
viewer = getOption("vsc.view", "Two"),
Expand Down Expand Up @@ -619,6 +680,24 @@ if (show_view) {
title = title, file = file, viewer = viewer, uuid = uuid
)
})
} else if (isS4(x)) {
tryCatch({
file <- tempfile(tmpdir = tempdir(), fileext = ".json")
print(file)
structure_overview <- traverse_S4(x)
jsonlite::write_json(structure_overview, file, na = "string", null = "null", auto_unbox = TRUE, force = TRUE)
request("dataview", source = "S4", type = "json",
title = title, file = file, viewer = viewer, uuid = uuid
)
}, error = function(e) {
file <- file.path(tempdir, paste0(make.names(title), ".txt"))
text <- utils::capture.output(print(x))
writeLines(text, file)
request("dataview", source = "object", type = "txt",
title = title, file = file, viewer = viewer, uuid = uuid
)
})

} else {
file <- file.path(tempdir, paste0(make.names(title), ".R"))
if (is.primitive(x)) {
Expand Down
94 changes: 93 additions & 1 deletion src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { IRequest } from './liveShare/shareSession';
import { homeExtDir, rWorkspace, globalRHelp, globalHttpgdManager, extensionContext, sessionStatusBarItem } from './extension';
import { UUID, rHostService, rGuestService, isLiveShare, isHost, isGuestSession, closeBrowser, guestResDir, shareBrowser, openVirtualDoc, shareWorkspace } from './liveShare';


export interface GlobalEnv {
[key: string]: {
class: string[];
Expand Down Expand Up @@ -371,6 +372,21 @@ export async function showDataView(source: string, type: string, title: string,
const content = await getListHtml(panel.webview, file);
panel.iconPath = new UriIcon('open-preview');
panel.webview.html = content;
} else if (source === 'S4') {
const panel = window.createWebviewPanel('dataview', title,
{
preserveFocus: true,
viewColumn: ViewColumn[viewer as keyof typeof ViewColumn],
},
{
enableScripts: true,
enableFindWidget: true,
retainContextWhenHidden: true,
localResourceRoots: [Uri.file(resDir)],
});
const content = await getObjectHtml(panel.webview, file);
panel.iconPath = new UriIcon('open-preview');
panel.webview.html = content;
} else {
if (isGuestSession) {
const fileContent = await rGuestService?.requestFileContent(file, 'utf8');
Expand Down Expand Up @@ -632,7 +648,7 @@ export async function getListHtml(webview: Webview, file: string): Promise<strin
var data = ${String(content)};
$(document).ready(function() {
var options = {
collapsed: false,
collapsed: true,
rootCollapsable: false,
withQuotes: false,
withLinks: true
Expand All @@ -648,6 +664,82 @@ export async function getListHtml(webview: Webview, file: string): Promise<strin
`;
}


export async function getObjectHtml(webview: Webview, file: string): Promise<string> {
resDir = isGuestSession ? guestResDir : resDir;
const content = await readContent(file, 'utf8');

return `
<!doctype HTML>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="${String(webview.asWebviewUri(Uri.file(path.join(resDir, 'jquery.min.js'))))}"></script>
<script src="${String(webview.asWebviewUri(Uri.file(path.join(resDir, 'jquery.json-viewer.js'))))}"></script>
<link href="${String(webview.asWebviewUri(Uri.file(path.join(resDir, 'jquery.json-viewer.css'))))}" rel="stylesheet">
<style type="text/css">
body {
color: var(--vscode-editor-foreground);
background-color: var(--vscode-editor-background);
}

.json-document {
padding: 0 0;
}

pre#json-renderer {
font-family: var(--vscode-editor-font-family);
border: 0;
}

ul.json-dict, ol.json-array {
color: var(--vscode-symbolIcon-fieldForeground);
border-left: 1px dotted var(--vscode-editorLineNumber-foreground);
}

.json-literal {
color: var(--vscode-symbolIcon-variableForeground);
}

.json-string {
color: var(--vscode-symbolIcon-stringForeground);
}

a.json-toggle:before {
color: var(--vscode-button-secondaryBackground);
}

a.json-toggle:hover:before {
color: var(--vscode-button-secondaryHoverBackground);
}

a.json-placeholder {
color: var(--vscode-input-placeholderForeground);
}
</style>
<script>
var data = ${String(content)};
$(document).ready(function() {
var options = {
collapsed: true,
rootCollapsable: true,
withQuotes: false,
withLinks: true
};
$("#json-renderer").jsonViewer(data, options);
});
</script>
</head>
<body>
<pre id="json-renderer"></pre>
</body>
</html>
`;
}



export async function getWebviewHtml(webview: Webview, file: string, title: string, dir: string, webviewDir: string): Promise<string> {
const observerPath = Uri.file(path.join(webviewDir, 'observer.js'));
const body = (await readContent(file, 'utf8') || '').toString()
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
".github"
]
}