12

I want to write a template that creates new projects in RStudio. What I want to do is:

  1. Create a new Rstudio project in a folder called "MyNewProject"
  2. Create a new project using ProjectTemplate package in this folder by: create.project('MyNewProject').
  3. Make some modifications in this folder.

I believe I can code steps 2 and 3. But I don't know how to create a new project in RStudio by a script. If it is possible, how can I do that?

HBat
  • 4,873
  • 4
  • 39
  • 56
  • Have you taken a look at `devtools`, particularly the `create` function? (Note that that will create an *R package* in the folder, not just the R project) – David Robinson Sep 30 '14 at 16:55
  • @DavidRobinson No, I did not. I simply want a code that creates a folder that includes "MyNewProject.Rproj" file and a git in that folder to accompany that in Step 1. – HBat Sep 30 '14 at 17:06
  • You could write one and put it in your `.Rprofile` – Rich Scriven Sep 30 '14 at 18:44

4 Answers4

8

Nothing special about a .Rproj file, just a text file with (or what ever defaults):

Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 4
Encoding: UTF-8

RnwWeave: knitr
LaTeX: pdfLaTeX

So this function would do what you're after:

myProject <- function(proj, ...) {

    require(ProjectTemplate)
    create.project(proj, ...)

    x <- c("Version: 1.0", "", "RestoreWorkspace: Default", "SaveWorkspace: Default", 
        "AlwaysSaveHistory: Default", "", "EnableCodeIndexing: Yes", 
        "UseSpacesForTab: Yes", "NumSpacesForTab: 4", "Encoding: UTF-8", 
        "", "RnwWeave: knitr", "LaTeX: pdfLaTeX")

    cat(paste(x, collapse="\n"), file=file.path(proj, paste0(basename(proj), ".Rproj")))

    message(paste(basename(proj), "has been created"))
}

myProject("MyNewProject.Rproj")

For the git requirement, open the folder and use:

qdapTools::repo2github()

in the console (of course you'll need to install qdapTools).

hisspott
  • 416
  • 6
  • 18
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • Thank you very much for your answer. I believe there is no method to create a new project from RStudio's interface by R code. That's fine. I can manage to create a project template using the methods you offered. – HBat Oct 17 '14 at 17:34
7

With the new package usethis, the simpler answer to your question 1 reads:

library(usethis)
create_project(path = "MyNewProject", open = TRUE, rstudio = TRUE)

This code makes a folder "MyNewProject", creates "MyNewProject.Rproj" file and opens a new RStudio session with working directory "MyNewProject".

In the new session, now in "MyNewProject" folder, you can run the following code to initialize a local git repo

library(usethis)
use_git()

You can even create a remote repo in github, if you have all git configured properly, with

use_github()

Two useful references are:

  1. usethis

  2. Happy Git and Github for the UseR

josep maria porrà
  • 1,198
  • 10
  • 18
5

Was looking for this very thing, and noticed that RStudio has recently put out something for this.

Thought I'd put out an answer in case it helps anyone else.

https://rstudio.github.io/rstudio-extensions/rstudio_project_templates.html

John Tarr
  • 717
  • 1
  • 9
  • 21
2

I know this is a bit of an older question, but for others out there, there is a way to code the .Rproj in a script.

If you look at the devtools::create function, there is a function called use_rstudio. Looking at the contents of that function you get:

> devtools::use_rstudio
function (pkg = ".") 
{
    pkg <- as.package(pkg)
    path <- file.path(pkg$path, paste0(pkg$package, ".Rproj"))
    if (file.exists(path)) {
        stop(pkg$package, ".Rproj already exists", call. = FALSE)
    }
    message("Adding RStudio project file to ", pkg$package)
    template_path <- system.file("templates/template.Rproj", 
        package = "devtools")
    file.copy(template_path, path)
    add_git_ignore(pkg, c(".Rproj.user", ".Rhistory", ".RData"))
    add_build_ignore(pkg, c("^.*\\.Rproj$", "^\\.Rproj\\.user$"), 
        escape = FALSE)
    invisible(TRUE)
}
<environment: namespace:devtools>

See the section for template_path? That is the code you can use to create the .Rproj file. So the end code in the script would be:

path <- file.path('path/to/folder', paste0('foldername', ".Rproj"))
template_path <- system.file("templates/template.Rproj", 
    package = "devtools")
file.copy(template_path, path)

You can now create an .Rproj from code! :)

Luke W. Johnston
  • 954
  • 9
  • 17
  • I have been using you script using system.file for some times now but it does not work anymore. No idea on what is going wrong ? – marc Mar 31 '20 at 17:11
  • 1
    @marc Function `use_rstudio` is now part of the package `usethis` that was uncoupled from `devtools` package in release 2.0.0 oct 30, 2018 [devtool 2.0.0](https://www.tidyverse.org/blog/2018/10/devtools-2-0-0/). See [usethis](https://usethis.r-lib.org/articles/articles/usethis-setup.html). – josep maria porrà Apr 07 '20 at 22:01