{"id":436,"date":"2015-04-12T15:15:40","date_gmt":"2015-04-12T07:15:40","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=436"},"modified":"2018-09-04T22:47:17","modified_gmt":"2018-09-04T14:47:17","slug":"how-to-save-and-load-environment-objects-in-r","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-save-and-load-environment-objects-in-r\/","title":{"rendered":"How to save and load environment objects in R"},"content":{"rendered":"<p>There was a need for me to build a prediction model in R and a Shiny app to allow users to get predictions out of my model. As the building of a prediction model take quite a while, it is not feasible for my shiny app to build the prediction model on demand. <\/p>\n<p>There must be a way for my Shiny app to load my prediction model only once for fulfilling prediction requests from the users. As with many other programming languages, there are mechanisms in R that allow me to save my environment objects in one session and load them back in another session. This post documents how I can save and load environment objects in R.<\/p>\n<h2>Getting some objects into the environment<\/h2>\n<p>To demonstrate the ability of R in saving and loading objects, I first create some objects into the environment. <\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nx = c(1,2,3)\r\ny = c('Hello', 'World')\r\naList &lt;- list()\r\naList&#x5B;'a'] &lt;- 'a'\r\naList&#x5B;'b'] &lt;- 'b'\r\n<\/pre>\n<p>Here I had created a Integer vector, a Character vector and a list of Character vectors. I then ran the following function to see if my objects had been saved to the current environment:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nls()\r\n<\/pre>\n<p>which gave me the following output:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;1] &quot;aList&quot; &quot;x&quot;     &quot;y&quot;\r\n<\/pre>\n<h2>Saving the entire list of environment objects<\/h2>\n<p>After I had created some objects into the R environment, I used the <code>save.image<\/code> function to help me save the entire list of environment objects to a file. To do so, I ran the following function:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsave.image(file='myEnvironment.RData')\r\n<\/pre>\n<p>This function ran the <code>ls<\/code> function to get the entire list of objects in the environment and save the objects as a file named 'myEnvironment.RData' in my current working directory. <\/p>\n<p>To check if I had 'myEnvironment.RData', I ran the following function to get the files available in my current working directory:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ndir()\r\n<\/pre>\n<p>and was able to see the newly created file that should contain the objects that I had created earlier:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;1] &quot;myEnvironment.RData&quot; \r\n<\/pre>\n<h2>Loading back the entire list environment of objects<\/h2>\n<p>To demonstrate that I can load back the objects that I had created, I first terminate my current R session by running the following function:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nquit(save='no')\r\n<\/pre>\n<p>This function tells R to quit the current session without saving the environment data.<\/p>\n<p>I then fire up a new R session and run the following function to load back the objects from 'myEnvironment.RData':<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nload('myEnvironment.RData')\r\n<\/pre>\n<p>This function looked up 'myEnvironment.RData' in my current working directory in order to load back the objects that are saved within the 'myEnvironment.RData' file.<\/p>\n<p>I ran the <code>ls<\/code> function again to check whether my objects are loaded into the environment:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nls()\r\n<\/pre>\n<p>which produced the following output:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;1] &quot;aList&quot; &quot;x&quot;     &quot;y&quot;\r\n<\/pre>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-72\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-72&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-72&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F436&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>There was a need for me to build a prediction model in R and a Shiny app to allow users to get predictions out of my model. As the building of a prediction model take quite a while, it is not feasible for my shiny app to build the prediction model on demand. <\/p>\n<p>There must be a way for my Shiny app to load my prediction model only once for fulfilling prediction requests from the users. As with many other programming languages, there are mechanisms in R that allow me to save my environment objects in one session and load them back in another session. This post documents how I can save and load environment objects in R.<\/p>\n","protected":false},"author":1,"featured_media":1231,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[25,218],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/R-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-72","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/436"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=436"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/436\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1231"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}