jamzsabb
jamzsabb

Reputation: 1154

Copying code into R?

This is a very basic and stupid question, but I just haven't been able to phrase it correctly into Google to get an answer.

I'm going through a book to learn R, and there are often code samples that I'd like to copy and run, but the only way I know how to do this is to copy it line by line, or put it in a text file and then use source("text_file.txt"). Simply copy and pasting multiple lines into the terminal produces errors.

Is there a better way to do this?

Upvotes: 0

Views: 3492

Answers (2)

Jan Luba
Jan Luba

Reputation: 86

Which R are you using?

You're probably working only off the command line and don't have a script open. In RStudio, click File -> New File -> R Script (or Ctrl+Shift+N) to create a script, which you can save like a text file. Copy-paste all your code into that and click Ctrl+A (to select all lines) and Ctrl+R to run all at once.

Best, Jan

Upvotes: 1

dixhom
dixhom

Reputation: 3035

What I'd do is, in R GUI, select File > New script. Paste the lines of code, select them and Ctrl + R.

Or you can select any chunk of code and hit Ctrl + R to run them. This is good for debugging.

Upvotes: 1

Related Questions