Skip to content
Merged
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
6 changes: 4 additions & 2 deletions R/rmarkdown/knit.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ knit_command <- Sys.getenv("VSCR_KNIT_COMMAND")

# set the knitr chunk eval directory
# mainly affects source calls
knitr::opts_knit[["set"]](root.dir = knit_dir)
if (nzchar(knit_dir)) {
knitr::opts_knit[["set"]](root.dir = knit_dir)
}

# render and get file output location for use in extension
cat(
lim,
eval(parse(text = knit_command)),
lim,
sep = ""
)
)
6 changes: 4 additions & 2 deletions R/rmarkdown/preview.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ set_html <- tryCatch(

# set the knitr chunk eval directory
# mainly affects source calls
knitr::opts_knit[["set"]](root.dir = knit_dir)
if (nzchar(knit_dir)) {
knitr::opts_knit[["set"]](root.dir = knit_dir)
}

# render and get file output location for use in extension
cat(
Expand All @@ -42,4 +44,4 @@ cat(
),
lim,
sep = ""
)
)
4 changes: 2 additions & 2 deletions src/rmarkdown/knit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class RMarkdownKnitManager extends RMarkdownManager {
private async renderDocument(rDocumentPath: string, docPath: string, docName: string, yamlParams: IYamlFrontmatter, outputFormat?: string): Promise<DisposableProcess> {
const openOutfile: boolean = util.config().get<boolean>('rmarkdown.knit.openOutputFile') ?? false;
const knitWorkingDir = this.getKnitDir(knitDir, docPath);
const knitWorkingDirText = knitWorkingDir ? `${knitWorkingDir}` : `NULL`;
const knitWorkingDirText = knitWorkingDir ? `${knitWorkingDir}` : '';
const knitCommand = await this.getKnitCommand(yamlParams, rDocumentPath, outputFormat);
this.rPath = await util.getRpath();

Expand Down Expand Up @@ -150,8 +150,8 @@ export class RMarkdownKnitManager extends RMarkdownManager {
// the definition of what constitutes an R Markdown site differs
// depending on the type of R Markdown site (i.e., "simple" vs. blogdown sites)
private async findSiteParam(): Promise<string | undefined> {
const rootFolder = vscode.workspace.workspaceFolders[0].uri.fsPath;
const wad = vscode.window.activeTextEditor.document.uri.fsPath;
const rootFolder = vscode.workspace.workspaceFolders?.[0]?.uri?.fsPath ?? path.dirname(wad);
const indexFile = (await vscode.workspace.findFiles(new vscode.RelativePattern(rootFolder, 'index.{Rmd,rmd, md}'), null, 1))?.[0];
const siteRoot = path.join(path.dirname(wad), '_site.yml');

Expand Down
4 changes: 2 additions & 2 deletions src/rmarkdown/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ export abstract class RMarkdownManager {
switch (knitDir) {
// the directory containing the R Markdown document
case KnitWorkingDirectory.documentDirectory: {
return path.dirname(docPath).replace(/\\/g, '/').replace(/['"]/g, '\\"');
return path.dirname(docPath)?.replace(/\\/g, '/')?.replace(/['"]/g, '\\"') ?? undefined;
}
// the root of the current workspace
case KnitWorkingDirectory.workspaceRoot: {
const currentDocumentWorkspace = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(docPath) ?? vscode.window.activeTextEditor?.document?.uri)?.uri?.fsPath ?? undefined;
return currentDocumentWorkspace.replace(/\\/g, '/').replace(/['"]/g, '\\"');
return currentDocumentWorkspace?.replace(/\\/g, '/')?.replace(/['"]/g, '\\"') ?? undefined;
}
// the working directory of the attached terminal, NYI
// case 'current directory': {
Expand Down
2 changes: 1 addition & 1 deletion src/rmarkdown/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ export class RMarkdownPreviewManager extends RMarkdownManager {

private async previewDocument(filePath: string, fileName?: string, viewer?: vscode.ViewColumn, currentViewColumn?: vscode.ViewColumn): Promise<DisposableProcess> {
const knitWorkingDir = this.getKnitDir(knitDir, filePath);
const knitWorkingDirText = knitWorkingDir ? `${knitWorkingDir}` : `NULL`;
const knitWorkingDirText = knitWorkingDir ? `${knitWorkingDir}` : '';
this.rPath = await getRpath();

const lim = '<<<vsc>>>';
Expand Down