Rscript - Using same R-file, The R terminal behaves different compared to Rstudio











up vote
1
down vote

favorite












Problem: Using same R-file, The R terminal behaves different compared to Rstudio.



When running below R-file several times, in Rstudio I get the correct behaviour.




  • First run [count=20], rest of run [count=1].


When running same R-file several times from a terminal, using [Rscript]:




  • First run [count=20], rest of run [count=20].


Wanted behaviour:



I need the R terminal to behave same as R studio , creating counter with value [20] and for the rest of the times put it to value [1].



My environment:



Ubuntu Linux 18.04



R-studio: 1.1.453



Terminal (Bash 4.4.19, R v.3.4.4)



Content of R-file:



setwd ("/tmp-r") # Set working directory.

# Set [count] to 20 if [count] does not exists.
# Set [count] to 1 if [count] exists.
if (!exists('count')) {
count <- 20
} else {
count <- 1
}

save.image() # Save.









share|improve this question


















  • 2




    Somewhat obvious, but could it be that your global environment is contaminated after each consecutive run in RStudio? My point is, if you include rm(list=ls()); gc(reset=TRUE) at the very end of your R script, would it make the rest of run counter be equal to 20 in RStudio like it does in the terminal?
    – 12b345b6b78
    Nov 10 at 21:05






  • 2




    RStudio maintains the same session for each execution. Running Rscript loses the session between calls. You'd need to save the session (save.image() / load()) at the end and beginning of the script to keep the same state.
    – hrbrmstr
    Nov 10 at 21:05










  • I perfomed some tests adding, [load('.Rdata'] at the beginning of the script. It adjust to the correct behaviour in R terminal. However, having the [load] makes Rstudio always loading [value=1] which means I would have to "clean/reset" the count residing in [.RData] before running the script the first time. This for the script to capture that the variable [count] does not exist, thus creating the variable and set it to value [20].
    – Toolbox
    Nov 10 at 21:12












  • @12b345b6b78 You are correct in a way that if adding [rm(list=ls()] and [gc(reset=TRUE)], Rstudio will behave same as R terminal, meaning that in Rstudio the value with be kept to [20] for all the script runs. - However, adding [load('.RData'] below the [rm(list=ls()] and [gc(reset=TRUE)], will make Rstudio behave correctly.
    – Toolbox
    Nov 10 at 21:19










  • I discovered that even if the minified above code works, applying the solution to the expanded script does not work. When doing some tests, it seems that the local [.Rprofile] affect the total solution.
    – Toolbox
    Nov 11 at 13:53















up vote
1
down vote

favorite












Problem: Using same R-file, The R terminal behaves different compared to Rstudio.



When running below R-file several times, in Rstudio I get the correct behaviour.




  • First run [count=20], rest of run [count=1].


When running same R-file several times from a terminal, using [Rscript]:




  • First run [count=20], rest of run [count=20].


Wanted behaviour:



I need the R terminal to behave same as R studio , creating counter with value [20] and for the rest of the times put it to value [1].



My environment:



Ubuntu Linux 18.04



R-studio: 1.1.453



Terminal (Bash 4.4.19, R v.3.4.4)



Content of R-file:



setwd ("/tmp-r") # Set working directory.

# Set [count] to 20 if [count] does not exists.
# Set [count] to 1 if [count] exists.
if (!exists('count')) {
count <- 20
} else {
count <- 1
}

save.image() # Save.









share|improve this question


















  • 2




    Somewhat obvious, but could it be that your global environment is contaminated after each consecutive run in RStudio? My point is, if you include rm(list=ls()); gc(reset=TRUE) at the very end of your R script, would it make the rest of run counter be equal to 20 in RStudio like it does in the terminal?
    – 12b345b6b78
    Nov 10 at 21:05






  • 2




    RStudio maintains the same session for each execution. Running Rscript loses the session between calls. You'd need to save the session (save.image() / load()) at the end and beginning of the script to keep the same state.
    – hrbrmstr
    Nov 10 at 21:05










  • I perfomed some tests adding, [load('.Rdata'] at the beginning of the script. It adjust to the correct behaviour in R terminal. However, having the [load] makes Rstudio always loading [value=1] which means I would have to "clean/reset" the count residing in [.RData] before running the script the first time. This for the script to capture that the variable [count] does not exist, thus creating the variable and set it to value [20].
    – Toolbox
    Nov 10 at 21:12












  • @12b345b6b78 You are correct in a way that if adding [rm(list=ls()] and [gc(reset=TRUE)], Rstudio will behave same as R terminal, meaning that in Rstudio the value with be kept to [20] for all the script runs. - However, adding [load('.RData'] below the [rm(list=ls()] and [gc(reset=TRUE)], will make Rstudio behave correctly.
    – Toolbox
    Nov 10 at 21:19










  • I discovered that even if the minified above code works, applying the solution to the expanded script does not work. When doing some tests, it seems that the local [.Rprofile] affect the total solution.
    – Toolbox
    Nov 11 at 13:53













up vote
1
down vote

favorite









up vote
1
down vote

favorite











Problem: Using same R-file, The R terminal behaves different compared to Rstudio.



When running below R-file several times, in Rstudio I get the correct behaviour.




  • First run [count=20], rest of run [count=1].


When running same R-file several times from a terminal, using [Rscript]:




  • First run [count=20], rest of run [count=20].


Wanted behaviour:



I need the R terminal to behave same as R studio , creating counter with value [20] and for the rest of the times put it to value [1].



My environment:



Ubuntu Linux 18.04



R-studio: 1.1.453



Terminal (Bash 4.4.19, R v.3.4.4)



Content of R-file:



setwd ("/tmp-r") # Set working directory.

# Set [count] to 20 if [count] does not exists.
# Set [count] to 1 if [count] exists.
if (!exists('count')) {
count <- 20
} else {
count <- 1
}

save.image() # Save.









share|improve this question













Problem: Using same R-file, The R terminal behaves different compared to Rstudio.



When running below R-file several times, in Rstudio I get the correct behaviour.




  • First run [count=20], rest of run [count=1].


When running same R-file several times from a terminal, using [Rscript]:




  • First run [count=20], rest of run [count=20].


Wanted behaviour:



I need the R terminal to behave same as R studio , creating counter with value [20] and for the rest of the times put it to value [1].



My environment:



Ubuntu Linux 18.04



R-studio: 1.1.453



Terminal (Bash 4.4.19, R v.3.4.4)



Content of R-file:



setwd ("/tmp-r") # Set working directory.

# Set [count] to 20 if [count] does not exists.
# Set [count] to 1 if [count] exists.
if (!exists('count')) {
count <- 20
} else {
count <- 1
}

save.image() # Save.






r rstudio rscript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 21:01









Toolbox

514310




514310








  • 2




    Somewhat obvious, but could it be that your global environment is contaminated after each consecutive run in RStudio? My point is, if you include rm(list=ls()); gc(reset=TRUE) at the very end of your R script, would it make the rest of run counter be equal to 20 in RStudio like it does in the terminal?
    – 12b345b6b78
    Nov 10 at 21:05






  • 2




    RStudio maintains the same session for each execution. Running Rscript loses the session between calls. You'd need to save the session (save.image() / load()) at the end and beginning of the script to keep the same state.
    – hrbrmstr
    Nov 10 at 21:05










  • I perfomed some tests adding, [load('.Rdata'] at the beginning of the script. It adjust to the correct behaviour in R terminal. However, having the [load] makes Rstudio always loading [value=1] which means I would have to "clean/reset" the count residing in [.RData] before running the script the first time. This for the script to capture that the variable [count] does not exist, thus creating the variable and set it to value [20].
    – Toolbox
    Nov 10 at 21:12












  • @12b345b6b78 You are correct in a way that if adding [rm(list=ls()] and [gc(reset=TRUE)], Rstudio will behave same as R terminal, meaning that in Rstudio the value with be kept to [20] for all the script runs. - However, adding [load('.RData'] below the [rm(list=ls()] and [gc(reset=TRUE)], will make Rstudio behave correctly.
    – Toolbox
    Nov 10 at 21:19










  • I discovered that even if the minified above code works, applying the solution to the expanded script does not work. When doing some tests, it seems that the local [.Rprofile] affect the total solution.
    – Toolbox
    Nov 11 at 13:53














  • 2




    Somewhat obvious, but could it be that your global environment is contaminated after each consecutive run in RStudio? My point is, if you include rm(list=ls()); gc(reset=TRUE) at the very end of your R script, would it make the rest of run counter be equal to 20 in RStudio like it does in the terminal?
    – 12b345b6b78
    Nov 10 at 21:05






  • 2




    RStudio maintains the same session for each execution. Running Rscript loses the session between calls. You'd need to save the session (save.image() / load()) at the end and beginning of the script to keep the same state.
    – hrbrmstr
    Nov 10 at 21:05










  • I perfomed some tests adding, [load('.Rdata'] at the beginning of the script. It adjust to the correct behaviour in R terminal. However, having the [load] makes Rstudio always loading [value=1] which means I would have to "clean/reset" the count residing in [.RData] before running the script the first time. This for the script to capture that the variable [count] does not exist, thus creating the variable and set it to value [20].
    – Toolbox
    Nov 10 at 21:12












  • @12b345b6b78 You are correct in a way that if adding [rm(list=ls()] and [gc(reset=TRUE)], Rstudio will behave same as R terminal, meaning that in Rstudio the value with be kept to [20] for all the script runs. - However, adding [load('.RData'] below the [rm(list=ls()] and [gc(reset=TRUE)], will make Rstudio behave correctly.
    – Toolbox
    Nov 10 at 21:19










  • I discovered that even if the minified above code works, applying the solution to the expanded script does not work. When doing some tests, it seems that the local [.Rprofile] affect the total solution.
    – Toolbox
    Nov 11 at 13:53








2




2




Somewhat obvious, but could it be that your global environment is contaminated after each consecutive run in RStudio? My point is, if you include rm(list=ls()); gc(reset=TRUE) at the very end of your R script, would it make the rest of run counter be equal to 20 in RStudio like it does in the terminal?
– 12b345b6b78
Nov 10 at 21:05




Somewhat obvious, but could it be that your global environment is contaminated after each consecutive run in RStudio? My point is, if you include rm(list=ls()); gc(reset=TRUE) at the very end of your R script, would it make the rest of run counter be equal to 20 in RStudio like it does in the terminal?
– 12b345b6b78
Nov 10 at 21:05




2




2




RStudio maintains the same session for each execution. Running Rscript loses the session between calls. You'd need to save the session (save.image() / load()) at the end and beginning of the script to keep the same state.
– hrbrmstr
Nov 10 at 21:05




RStudio maintains the same session for each execution. Running Rscript loses the session between calls. You'd need to save the session (save.image() / load()) at the end and beginning of the script to keep the same state.
– hrbrmstr
Nov 10 at 21:05












I perfomed some tests adding, [load('.Rdata'] at the beginning of the script. It adjust to the correct behaviour in R terminal. However, having the [load] makes Rstudio always loading [value=1] which means I would have to "clean/reset" the count residing in [.RData] before running the script the first time. This for the script to capture that the variable [count] does not exist, thus creating the variable and set it to value [20].
– Toolbox
Nov 10 at 21:12






I perfomed some tests adding, [load('.Rdata'] at the beginning of the script. It adjust to the correct behaviour in R terminal. However, having the [load] makes Rstudio always loading [value=1] which means I would have to "clean/reset" the count residing in [.RData] before running the script the first time. This for the script to capture that the variable [count] does not exist, thus creating the variable and set it to value [20].
– Toolbox
Nov 10 at 21:12














@12b345b6b78 You are correct in a way that if adding [rm(list=ls()] and [gc(reset=TRUE)], Rstudio will behave same as R terminal, meaning that in Rstudio the value with be kept to [20] for all the script runs. - However, adding [load('.RData'] below the [rm(list=ls()] and [gc(reset=TRUE)], will make Rstudio behave correctly.
– Toolbox
Nov 10 at 21:19




@12b345b6b78 You are correct in a way that if adding [rm(list=ls()] and [gc(reset=TRUE)], Rstudio will behave same as R terminal, meaning that in Rstudio the value with be kept to [20] for all the script runs. - However, adding [load('.RData'] below the [rm(list=ls()] and [gc(reset=TRUE)], will make Rstudio behave correctly.
– Toolbox
Nov 10 at 21:19












I discovered that even if the minified above code works, applying the solution to the expanded script does not work. When doing some tests, it seems that the local [.Rprofile] affect the total solution.
– Toolbox
Nov 11 at 13:53




I discovered that even if the minified above code works, applying the solution to the expanded script does not work. When doing some tests, it seems that the local [.Rprofile] affect the total solution.
– Toolbox
Nov 11 at 13:53












1 Answer
1






active

oldest

votes

















up vote
0
down vote













With input from comments, I found 2 issues that solves the problem. Both for the minified test-script I published as a question, and my expanded script.



1) Add [load ('RData')], in the beginning since the load behaviour differs between Rstudio and R terminal. Rstudio dynamically update the global environment whenever you send in a change in the Rstudio console. R terminal loses the session between calls and therefor R terminal needs the R-file to start with [load ('RData')].



2) To solve my expanded script, I found out that the local [.Rprofile] has a [save.image('.RData)] at the end of the script. When removing that command in [.Rprofile] it solves the bigger script.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243375%2frscript-using-same-r-file-the-r-terminal-behaves-different-compared-to-rstudi%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    With input from comments, I found 2 issues that solves the problem. Both for the minified test-script I published as a question, and my expanded script.



    1) Add [load ('RData')], in the beginning since the load behaviour differs between Rstudio and R terminal. Rstudio dynamically update the global environment whenever you send in a change in the Rstudio console. R terminal loses the session between calls and therefor R terminal needs the R-file to start with [load ('RData')].



    2) To solve my expanded script, I found out that the local [.Rprofile] has a [save.image('.RData)] at the end of the script. When removing that command in [.Rprofile] it solves the bigger script.






    share|improve this answer

























      up vote
      0
      down vote













      With input from comments, I found 2 issues that solves the problem. Both for the minified test-script I published as a question, and my expanded script.



      1) Add [load ('RData')], in the beginning since the load behaviour differs between Rstudio and R terminal. Rstudio dynamically update the global environment whenever you send in a change in the Rstudio console. R terminal loses the session between calls and therefor R terminal needs the R-file to start with [load ('RData')].



      2) To solve my expanded script, I found out that the local [.Rprofile] has a [save.image('.RData)] at the end of the script. When removing that command in [.Rprofile] it solves the bigger script.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        With input from comments, I found 2 issues that solves the problem. Both for the minified test-script I published as a question, and my expanded script.



        1) Add [load ('RData')], in the beginning since the load behaviour differs between Rstudio and R terminal. Rstudio dynamically update the global environment whenever you send in a change in the Rstudio console. R terminal loses the session between calls and therefor R terminal needs the R-file to start with [load ('RData')].



        2) To solve my expanded script, I found out that the local [.Rprofile] has a [save.image('.RData)] at the end of the script. When removing that command in [.Rprofile] it solves the bigger script.






        share|improve this answer












        With input from comments, I found 2 issues that solves the problem. Both for the minified test-script I published as a question, and my expanded script.



        1) Add [load ('RData')], in the beginning since the load behaviour differs between Rstudio and R terminal. Rstudio dynamically update the global environment whenever you send in a change in the Rstudio console. R terminal loses the session between calls and therefor R terminal needs the R-file to start with [load ('RData')].



        2) To solve my expanded script, I found out that the local [.Rprofile] has a [save.image('.RData)] at the end of the script. When removing that command in [.Rprofile] it solves the bigger script.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 14:06









        Toolbox

        514310




        514310






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243375%2frscript-using-same-r-file-the-r-terminal-behaves-different-compared-to-rstudi%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma