Skip to content

R markdown templates#984

Merged
renkun-ken merged 15 commits intoREditorSupport:masterfrom
renkun-ken:rmd-templates
Feb 13, 2022
Merged

R markdown templates#984
renkun-ken merged 15 commits intoREditorSupport:masterfrom
renkun-ken:rmd-templates

Conversation

@renkun-ken
Copy link
Copy Markdown
Member

@renkun-ken renkun-ken commented Feb 9, 2022

What problem did you solve?

Closes #839

This PR implements choosing template and create draft rmd from template via rmarkdown::draft().

The templates are obtained via scanning the R package folders so that the information of all templates are written to a temp JSON file. When user picks a template, saveFileDialog will show up and user needs to find a location to save the file.

Some templates have multiple files. If user specify e.g. draft.Rmd, multi-file templates will create a new folder draft and then put files in it.

(If you have)Screenshot

Kapture.2022-02-09.at.20.15.37.mp4

(If you do not have screenshot) How can I check this pull request?

  1. "R Markdown: New Draft" will let user choose template and choose a location to save file.
  2. Then a rmd file or a folder will be created.
  3. The rmd file will be open in editor.

@renkun-ken renkun-ken marked this pull request as draft February 9, 2022 09:42
@renkun-ken renkun-ken marked this pull request as ready for review February 9, 2022 12:13
@ManuelHentschel
Copy link
Copy Markdown
Member

I'm not very familiar with rmd templates, but the ones that were found in my installed packages (rmarkdown, reprex) were only single-file documents. In these instances it might feel more natural to just create an unsaved editor with the content of the file, instead of having to save it somewhere.

Would it be feasible to do this for the packages that have create_dir = false?

@renkun-ken
Copy link
Copy Markdown
Member Author

Yes. In my initial implementation, I create untitled documents in all cases, but switched to save before creation when I test with create_dir = true. Using untitled documents for create_dir = false certainly makes good sense, and it is consistent with the behavior of RStudio. I'll implement this soon.

Comment thread src/rmarkdown/draft.ts Outdated
Comment thread src/rmarkdown/draft.ts
}

if (template.info.create_dir) {
const uri = await window.showSaveDialog({
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .Rmd extension might be a bit confusing here. I did not find a really clean solution to configure the SaveDialog to specify a folder, but the following could be used:

        let defaultPath = path.join(cwd, 'draft');
        let i = 1;
        while(fs.existsSync(defaultPath)){
            defaultPath = path.join(cwd, `draft_${++i}`);
        }

        const uri = await window.showSaveDialog({
            defaultUri: Uri.file(defaultPath),
            filters: {
                'Folder': ['']
            },
            saveLabel: 'Create Folder',
            title: 'R Markdown: New Draft'
        });

The open dialogue seems to have an option to select only folders, but does not allow to select non-existing folders

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is something I'm also considering. I tried showOpenDialog with folder-only options, but it didn't work smoothly for saving purpose.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, rmarkdown::draft() will throw error if the target file or folder exists, which I have not handled yet.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rmarkdown::draft() will create a folder with last file extension removed. For example, draft.Rmd will become draft; draft.test.Rmd will become draft.test.

I update the code to check if this folder already exists and let user confirm whether to remove it before creating the draft folder.

Comment thread src/rmarkdown/draft.ts
@renkun-ken
Copy link
Copy Markdown
Member Author

@ManuelHentschel Any more suggestions?

Copy link
Copy Markdown
Member

@ManuelHentschel ManuelHentschel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works nicely, thanks!

@renkun-ken renkun-ken merged commit f5d857e into REditorSupport:master Feb 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature request] R Markdown: Choose templates

2 participants