An R project (an
.rxproj file) identifies all the source and content files associated with your project. It also contains build information for each file, maintains the information to integrate with source-control systems, and helps you organize your application into logical components. Workspace-related information such as the list of installed packages, however, is maintained separately in the workspace itself.
Projects are always managed within a Visual Studio solution, which can contain any number of projects that can reference one another. See Use multiple project types in Visual Studio.
Creating a new R project
- Start Visual Studio.
- Choose File > New > Project... (Ctrl+Shift+N)
Select "R Project" from under Templates > R, give the project a name and location, and select OK:
This command creates a project with an empty
script.R file open in the editor. Notice also in Solution Explorer there are two other files in the project:
.Rhistory records whatever commands you enter into the R Interactive window. You can open a dedicated history window with the R Tools > Windows > History command. That window has a toolbar button and context menu items to clear history contents.
rproject.rproj file maintains certain R-specific project settings that aren't otherwise managed by Visual Studio:
|Version||1.0||The version of R Tools for Visual Studio used to create the project.|
|RestoreWorkspace||Default||Automatically load previous Workspace variables from the
|SaveWorkspace||Default||Save current workspace variables to the
|AlwaysSaveHistory||Default||Save current Interactive Window history to the
|EnableCodeIndexing||Yes||Determines whether to run a background indexing task to speed code searches.|
|UseSpacesForTab||Yes||Determines whether to insert spaces (Yes) or a Tab character (No) when the Tab key is pressed in the editor.|
|NumSpacesForTab||2||The number of spaces to insert if UseSpacesForTab is Yes.|
|Encoding||UTF-8||The default encoding for
|RnwWeave||Sweave||Package to use when weaving a Rnw file.|
|LaTeX||pdfLaTeX||Library to use when converting RMarkdwon to PDF.|
Converting a folder of files to an R project
If you have an existing folder of
.R files that you want to manage in a project, do the following steps:
- Create a new project in Visual Studio as in the previous section.
- Copy your files into the project folder.
- In the Visual Studio Solution Explorer, right-click the project, select Add > Exiting Item, and browse to the files you want to add. Those files appear in your project tree after selecting OK.
- To organize code into subfolders, right-click the project, select Add > New Folder first, then copy your files into that folder and add those existing items in step 3.
To open the project property pages, right-click the project in Solution Explorer and select Properties, or select the *Project > (project name) properties... menu item. The window that opens displays project properties:
|Run||Startup file||The name of the file that is run with Source startup file command, F5, Debug > Start debugging, or Debug > Start without debugging. Right-clicking the file in the project and selecting Set as startup R script also sets it as the startup file.|
|Reset R Interactive on Run||Clears all variables from the interactive window's workspace when running the project. Doing so guarantees that there's no residual workspace contents from pervious runs.|
|Remote Project Path||Path to a remote workspace.|
|Transfer files on run||Indicates whether the project files, subject to the filter in Files to transfer, are to be copied to a remote workspace with each run.|
|Files to transfer||Filenames and wildcards indicating the specific files to copy to a remote workspace if Transfer files on run is selected.|
|Settings||(Settings.R file)||R project settings come from
Settings are stored as R code and the file can be sourced before running other modules, thus pre-populating environment with the predefined settings.
R-specific project commands
Visual Studio projects support a number of general commands through both the right-click menu and the Project menu. For details on these general capabilities, see Solutions and Projects in Visual Studio. Keep in mind, however, that
R Tools for Visual Studio (RTVS) adds a number of its own commands to the right-click menu for an R project and also files and folders within the project.
|Set Working Directory Here||Sets the R Interactive window's working directory to the project folder, which can also be used on any subfolder within a project.|
|Open Containing Folder||Opens Windows Explorer at the location of the selected file.|
|Add R Script||Creates and opens a new
|Add R Markdown||Creates and opens new
|Publish Stored Procedures||Starts a process to publish any stored procedures contained in R scripts. See Working with SQL Server stored procedures.|
R-specific item templates
RTVS includes a number of templates for specific file types. You access templates by right-clicking an R project and selecting Add > New Item..., by selecting Project > Add New Item..., or by using File > New > File... and selecting the R tab. The best way to explore a template is to create a new project and insert files of each type.
The Add > New Item... commands also display general file types that aren't listed in the table; with File > New > File... those types are contained instead on the General tab.
|R Script||A text file containing the same commands that can be entered on the R command line.|
|R Markdown||A file containing an R Markdown document.|
|R Settings||A file that holds R application settings.|
|R Documentation||A generic R documentation file containing only name, alias, and title fields.|
|R Documentation (Function)||An R documentation file containing many fields with comments for describing a function.|
|R Documentation (Dataset)||An R documentation file containing many fields with comments for describing a dataset.|
|SQL Query||And empty
|Stored Procedure with R||An R file with child SQL Query and child stored procedure template file. See SQL Server integration.|
Use multiple project types in Visual Studio
Visual Studio Solutions provide a convenient place to gather and manage related projects in one logical place. Solutions help keep your code organized and facilitates collaboration within teams.
In the example below, the solution contains an R project with a model built using R and Azure Machine Learning, a Python/scikit-learn project, a C++ project containing modules for intensive computational work, a SQL project for data management, and a Python/Bottle project for the web site that publishes the result:
The project highlighted with boldface is the "startup" project for the solution; to change it, right-click a different project and select Set as startup project.
At present, there isn't any explicit R to C#/C++ language integration in place (as there is for Python, see Creating a C++ extension for Python). However there are libraries available that provide C# and C++ bridges for R.
For more information on managing projects and solutions in general, see Solutions and Projects in Visual Studio.