Search code examples
rgoogle-colaboratory

Saving R output in Google Colab


How do I save R output into a file in google colab? It could be saved on google drive or my local drive, either would work.

For example, if I wanted to save a list of R objects in a RDS file, normally I would have used something like this on RStudio.

saveRDS(list(a, b, c, d), file = "C:\\sim1.rds")

I am looking to do something similar on Google colab.


Solution

  • Recently I found the answer so I wanted to write it here in case it is useful for others.

    To save an output on my google drive we need to mount it using the following.

    from google.colab import drive
    drive.mount('/content/drive')
    

    Then we can navigate to MyDrive using the following.

    cd /content/drive/MyDrive
    

    Now that we are in MyDrive, we can run the code and save outputs on MyDrive. Then we can download it to our laptop.