r

How to hide code when I run program in console in R studio?


I use R studio. when I run my script I see in console the outputs together with the code. I dont want the console will print also the code. How I can hide it?

enter image description here


Solution

  • There is a way to do this, which is to source selected text from inside the RStudio document pane. You can do this via the Rstudio API. First define the following function:

    run <- function() {
      eval(parse(text = rstudioapi::primary_selection(
      rstudioapi::getSourceEditorContext())$text))
    }
    

    Now in your RStudio, you can select text in your document pane like this:

    enter image description here

    And in the R console, you can do:

    run()
    #> [1] "I only want to see this message"
    

    And you can see the variables have changed as expected:

    x
    #> [1] 10
    

    If you wanted to, you could bind this function to a keyboard shortcut in R Studio.