Working in RStudio with default Git integration (Watson Studio)

You can create R scripts and R Shiny applications in projects with default Git integration.

R Shiny is an R package that makes it easy to develop interactive web applications straight from R. You can create, develope and refine Shiny apps in RStudio, whether to create a unique data visualization dashboard or publish applications into different places, for example to deployment spaces.

Creating R scripts and Shiny apps

The directory browser should be open when RStudio is launched, at the bottom right.

The Git extension is pre-installed, enabling access to the repository that you associated with your project at launch time and the Git tab is added to the RStudio toolbar.

The Git repository referenced in the project is cloned by the RStudio environment at launch time and can be viewed on the files browser at the bottom right of the IDE GUI in the folder called userfs. You must make all your changes to your R files in that folder or any subfolders to be able to sync with Git.

Important: The clone is pulled from the Git repository branch that you see next to the Git icon (Shows the Git icon.) on the project's action bar.

Note that if a folder or subfolder is thought to contain RShiny App files (i.e. files with the names app.R, ui.R or server.R), all files in that folder are considered to belong to the Shiny app (including .R files). Otherwise, all .R files are considered R script assets.

  1. Optional: Add collaborators to the project if you want to work on the same file with others. See Collaboration.
  2. Start working on R scipts:

    1. Select New File > R Script or uploading an R file from your local machine.
    2. Save your file changes. Don't save the file under userfs/assets as the assets directory is reserved for files related to project assets.
    3. You can test your scripts before you commit them to the Git repository by creating a job from your project's Jobs page and running it.

      1. From the project's Jobs page, select New job.
      2. Select your file, enter the job settings and run it.
      3. Validate the run results by clicking the job run.
  3. Or start working on Shiny apps:

    1. Click New File > Shiny Web App.... A new Shiny application creation window pops up.
    2. Enter a name for your Shiny application and leave userfs as the Create within directory setting. To enable synching with the Git repository, you must work in this directory or any subdirectories, however not in the /assets directory as this directory is reserved for files related to project assets.

      Both app.R and ui.R/server.R contain instructions needed to build your app and provides a sample app you can test run.

    3. You can choose to create a single file application (app.R) if your application is simple and can be contained within one file, for simplicity.
    4. Or, you can choose to create multiple files application (ui.R/server.R) if your application is more complex and needs to have its different facets edited separately.
    5. Once you are done with configuration, click Create.
    6. You can test run your app by clicking Run App in the top right corner of the editing panel. A pop-up window will be launched displaying your application.
  4. You can use data from a dataset in your scripts or apps. Supported formats of datasets include text, CSV, SPSS, SAS, and Stata. You can use data assets already imported into the project by clicking Import Dataset under the Environment tab, or clicking File and browsing for the file under userfs/assets/data-asset, or uploading them locally by clicking Upload in the Data panel on the bottom right. You can preview the data assets in the editing panel.

    Note: You can't preview datasets larger than 5 MB in RStudio.

  5. When your files are ready, push your changes to the Git respository:

    • By clicking the Git version control menu on the top menu bar of the main editing panel:

      1. Click Commit and select all the files that you have made changes to and would like to push to the Git repository.
      2. Add a change description and commit your staged changes to the local clone of your repository in your RStudio session.
      3. Click Push to push your your changes to the remote repository where your changes can be seen and accessed by other users.
      4. By clicking Pull in the Git actions panel, you can also pull file changes made by collaborators to your repository clone.
    • By selecting the Git icon from the project's action bar:

      1. Click Commit from the menu.
      2. Add a description, select the Git repository branch you chose for the project, select the files and commit the changes.
      3. Click Push from the Git menu to push your commits to the repository.

      The R files in the Git repository that are pushed are not added as assets to the project's Assets page. You can however select those files to run as jobs from the project's Jobs page.

Storing intermediate .rda files

You can store any intermediate files, for example .rda and .md files, log or text files in any storage volume installed with Cloud Pak for Data. This storage volume is automatically mounted at the time that an RStudio session is started and hence these files can be accessed by all project collaborators, and in R Shiny applications or jobs that run R scripts. For details on using a storage volume, see Managing storage volumes.

Working with data files

In RStudio, you can work with data files from different sources:

From the Files view in the RStudio UI, you can work with:

Adding data files as project assets

You should upload data files to use in RStudio from the find and add data sidebar on your project's Assets page because these files are automatically added as Data assets to your project.

However, if you uploaded or created data files in RStudio, you can add these files to your project as project data assets. These files must be in the assets/data_asset folder in RStudio. To add these files as data assets to the project:

  1. On the Assets page of the project, click the Find and Add Data icon (Shows the find data icon) and select the Files tab.
  2. Select the files you want to add to the project as assets.
  3. From the Actions list, select Add as data asset and apply your changes.

Running an R script in a job

You can run the script as a job in an RStudio environment in Watson Studio or on a remote Hadoop cluster. See:

Creating a Hadoop Yarn environment

  1. The Watson Studio adminstrator needs to add the Hadoop cluster configuration to your platform.
    1. Open the drop down menu from the sandwich button on Watson Studio's home page, and click on Configure Platform.
    2. Click on Add Registration to add the Hadoop cluster to the project's configuration.
  2. Now go to your project, click on the Environments tab. Click on New environment definition to create a custom environment.
  3. After you give the custom environment a name, select Hadoop as the environment type.
  4. Select the Hadoop configuration you would like to use.
  5. A Hadoop cluster set up for R scripts needs to be able to use Yarn, as certain R scripts require usage of Yarn. If the cluster is set up correctly, a field called Execution type appears, in which the user can select Yarn as the execution type. If you do not see an option for Execution type, it is likely your Hadoop admin has not set up the Hadoop cluster and configuration file to support the R environment. Once the set up is done on the hadoop side, your admin will need to refresh the Hadoop registration before Execution Type option would be available. You can select "Yarn" to run R script.
  6. Select the language, Yarn size and Yarn container memory. These fields are bounded by the admin's settings.
  7. Click Create to complete the creation of the environment.
  8. You can change the default settings of the custom environment later by clicking on the environment under the Environments tab, for example, increase or decrease the memory of the Yarn container.

Deploying scripts in a space

You can use CPDCTL to create a code package (ZIP archive file) that contains your script files, promote this code package to a deployment space where it is registered as a code package asset and then create a job that runs the files in the code package asset. See Code packages.

Learn more

Parent topic: RStudio