REditorSupport / vscode-R

R Extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=REditorSupport.r
MIT License
1.07k stars 128 forks source link

Provide R package-related tasks #59

Open jacob-long opened 6 years ago

jacob-long commented 6 years ago

There are some things added in one of the recent releases that we should consider including as VS Code tasks as well.

In particular, being able to run tests and R CMD check is ideally-suited to tasks.

I wasn't sure whether an extension can provide tasks in this way, but it appears to be possible. I don't know TypeScript well enough to implement, but here's an example of an extension that provides users with tasks: https://github.com/Microsoft/vscode-extension-samples/tree/master/task-provider-sample

I can help in terms of putting together the task definition JSONs since I'm using some in my own projects already.

Ikuyadeu commented 6 years ago

@jacob-long Sorry my late reply. Good idea! if you have a task JSON files, could you share it?

andycraig commented 4 years ago

Hi @jacob-long, I’m following up on some old issues. Are tasks still something you’re keen to see added?

jacob-long commented 4 years ago

I think these would be valuable additions to support R package developer workflows.

andycraig commented 4 years ago

@jacob-long Great! It sounds like you might already have some task definition JSON files that you can share? If so, that would be very useful.

I structure most of my projects as packages and use a workflow based around devtools::load_all() and devtools::test(), but I’ve never submitted a package to CRAN, used R CMD, or used VSCode tasks. Would you be able to provide a list of desired tasks and what they should do? I’m happy to help out on the implementation side of things.

Thank you!

crsh commented 4 years ago

Sorry, to barge in. I'm only just getting started using VS Code, but in case it helps, my set up looks as follows:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build package",
            "type": "shell",
            "command": "RScript -e 'devtools::install(upgrade = \"never\")'",
            "presentation": {
                "reveal": "always"
            },
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "Run unit tests",
            "type": "shell",
            "command": "RScript -e 'devtools::test()'",
            "presentation": {
                "reveal": "always"
            },
            "problemMatcher": [],
            "group": {
                "kind": "test",
                "isDefault": true
            }
        },
        {
            "label": "Run R CMD checks",
            "type": "shell",
            "command": "RScript -e 'devtools::check()'",
            "presentation": {
                "reveal": "always"
            },
            "problemMatcher": [],
            "group": {
                "kind": "test",
                "isDefault": false
            }
        }
    ]
}
maxheld83 commented 3 years ago

based on @crsh's example, I've factored out/expanded this a bit to replicate as much as possible the RStudio build pane. (Corresponding keymaps are in #551).

I understand there's a way to make an extension a proper Task provider.

I think this might also warrant deprecating the R: Document shortcuts in the command picker now, since a task is arguably the proper way to do this. In particular, the tasks run in their own R session, which makes sense for devtools::document().

I'm not knowledgeable about TypeScript (?), let alone VSCode extensions, otherwise I'd be happy to make PR, but I can help curate the tasks (rmarkdown, shiny, etc).

Here's what I got:

{
    "version": "2.0.0",
    "options": {
        "shell": {
            "executable": "Rscript",
            "args": [
                "-e"
            ]
        }
    },
    "presentation": {
        "reveal": "always",
        "panel": "shared",
        "showReuseMessage": false,
        "group": "build"
    },
    "type": "shell",
    "problemMatcher": [],
    "tasks": [
        {
            "label": "Document",
            "command": "devtools::document(roclets = c('rd', 'collate', 'namespace'))",
            "problemMatcher": []
        },
        {
            "label": "Install",
            "command": "devtools::install(quick = TRUE, upgrade = 'never')",
            "group": "build",
            "dependsOn": "Document"
        },
        {
            "label": "Test",
            "command": "devtools::test()",
            "group": "test",
            "problemMatcher": []
        },
        {
            "label": "Check",
            "command": "devtools::check(error_on = 'warning')",
            "group": {
                "kind": "test",
                "isDefault": true
            },
            "dependsOn": "Document"
        },
        {
            "label": "Pkgdown",
            "command": "muggle::build_site2()",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "dependsOn": "Document",
            "problemMatcher": []
        },
        {
            "label": "Build and Test",
            "dependsOn": [
                "Pkgdown",
                "Check"
            ]
        }
    ]
}
andycraig commented 3 years ago

@crsh Thank you for posting that info, and apologies for not responding! I completely missed the notification.

@maxheld83 Thank you for the followup on this issue. This isn't something I'm going to pursue any time soon, but contributions are very welcome!

jolars commented 3 years ago

Good idea, and thanks for the task configs. To really emulate the functionality from R Studio, it would be nice if it was possible to restart the R Session (and run library(<package>), just like the Install and Restart function does in R Studio.

I've fiddled a bit with the tasks, but I'm not sure how to do this properly. It's of course easy to start up R as part of the task, but then the R extension does not recognize the new console as the "main" R terminal, and sends commands to either a new terminal or the old one (if it was already opened).

What I'd like to achieve is to have the task reuse the standard R terminal. Does anyone know how to achieve this?

maxheld83 commented 3 years ago

I thought about that too @jolas. Maybe just put devtools::load_all() in a project-specific .rprofile? Though I forget ... I think project-specific project/.rprofile overrides ~/.rprofile and the whole thing is the same load_all() call so maybe just put it in the personal ~/.rprofile.

Might need some logic to test whether R is being launched inside an r package dir, but I think devtools (or usethis?) have some functions to test this.


Update: I was confused in the above, sorry @jolars. You were talking about "Install and Restart", I was talking about devtools::load_all(), so that's a separate issue. I always use load_all() had forgotten about "Install and Restart".

I would guess that there is a way to programmatically close/open the R terminal, since you can already open an r terminal from the command pallette ... but I don't know enough about the internals.

Anyway, I'm going to mark my own reply here off-topic, since this is a separate issue.

krlmlr commented 3 years ago

@jolars: I have mapped workbench.action.terminal.relaunch to Ctrl + Shift + F10 to restart the active terminal if the active language is R. Works for me.

andycraig commented 3 years ago

I've added tasks for Check, Document, Install and Test.

It should be fairly easy to add new tasks by copy-pasting the code for these tasks, which is here: https://github.com/Ikuyadeu/vscode-R/blob/17a7d69423d3a4377211e83f39ebc3bceba6ab69/src/extension.ts#L147-L154

krlmlr commented 3 years ago

Nice! I wonder if we can use "problemMatchers" to implement tasks that work on the active document, e.g. "knit".

andycraig commented 3 years ago

@krlmlr I think problem matchers work on the output of tasks. For tasks that use the active file, we might be able to use variable substitution: https://code.visualstudio.com/docs/editor/variables-reference

gowerc commented 2 years ago

This is quite a meaty issue now I'm wondering if its worth splitting it into sub issues namely: