Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script for creating a new project in Rstudio

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?

like image 688
HBat Avatar asked Sep 30 '14 16:09

HBat


3 Answers

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).

like image 133
Tyler Rinker Avatar answered Sep 29 '22 15:09

Tyler Rinker


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

like image 20
John Tarr Avatar answered Sep 29 '22 13:09

John Tarr


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

like image 23
josep maria porrà Avatar answered Sep 29 '22 14:09

josep maria porrà