Script to create a new project in Rstudio

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

  • Create a new Rstudio project in the "MyNewProject" folder
  • Create a new project with the help of the package ProjectTemplate

    in this folder: create.project('MyNewProject')

    .
  • Make some changes to this folder.

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

+3


source to share


3 answers


Nothing special about the file .Rproj

, just a text file with (or default):

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 will do what you need:

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

      



To hit

open a folder and use:

qdapTools::repo2github()

      

in the console (you need to install of course qdapTools

).

+4


source


Was looking for exactly this and noticed that RStudio recently did something for this.

Thought I would say it would help someone else.



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

+3


source


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

If you look at a function devtools::create

, a function appears use_rstudio

. Looking at the contents of this 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 template_path

? This is the code you can use to create the file .Rproj

. So the final code in the script will be:

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

      

Now you can generate code .Rproj

from code! :)

+2


source







All Articles