How to delete part of R studio console? - r

I don't want to erase the entire console, but just a few lines (as I have to submit my Rcode as an assignment and if I make an error, I want to be able to edit/clean up my coding). Thanks!

use R editor from File option.
type script in the R editor and run each line by selecting it and pressing Ctrl+R.
if you have typed it in console directly then you can still copy your typed script form the console and paste it in the editor

Related

Understanding R console vs writing R code in text file

What is the difference between using R console vs writing R code in a text file? I wrote this question on Kaggle but there were no previous questions on this matter.
When you supply code via text file (.R file) you "run the file with R" without visualizing it and it can stop somewhere due to error i.e. (which can be handled, etc.). Also running an .R file with R (for example via .bat file) generates a .Rout file, which is basically a print out of the console and some aditional info like runtime, etc.
If you feed the code in the console, each line is treated independently: even if there is an error you can process an aditional line (if it depends on the failed comand then it will fail also though) and you get to see each result as soon as the comand is run. In comparision to the .R file you will have no copy of the code other than that stored in the session - meaning you will end up needing to save to disk the code you have written if you want it to persist between session. Now you can choose to use whatever text format you like for this task from simple .txt to .docx BUT if you use .R format you can manipulate with notepad++ or the notepad editor and still run/complipe the file with R (via .bat file for example). In case of opting against .R file to store the written code, you will have to feed it to the console again to run.
In R Studio you can open .R files and manage (extend, correct) your code and feed it comand per comand or as a block to the console. So one could say you use .R files to manage you code, having the possiblity to compile/run these .R files directly with R to execute on a button click or repeatedly for example.
Not sure if that is what you are looking for?

Save the output of an r script including its commands

I want to save a part of my r script output including the commands into a text file. I know sink() but it does not include the commands or I could not find a specific option to do that.
Is there any possibility to capture the commands and its ouput within an r session. Simply write an Rmd or capture the output within the console is not the solution at the moment.
You are probably looking for the TeachingDemos package. Documentation can be found here.
Example:
library(TeachingDemos)
txtStart("test.txt")
# Your code
txtStop()
This should write both your command input and output to a file called test.txt.
If you're working interactively, here's one idea. It was this specific problem for which I created the sinkstart() function in the rite package. Basically, this creates a pop-up tcl/tk widget that you can write commands and output to. Here's a screenshot to give you a feel:
There are just two relevant functions: sinkstart() starts the sink; sinkstop() turns it off. You can toggle back and forth to selectively write to the widget. Then you can just save the contents with a right-click or a key shortcut.

R Studio/R Accessing Blocks of Previously Run Code

Is there a way to display past commands in R/R Studio?? I know in R Studio there is a shortcut (CTRL+UP Arrow) that allows you to see past lines you have ran. But this shortcut only allows you to access only a single line, not a block of previously run code. Is there a package, or some way in R to display and select blocks of past code in R/R Studio?
Here you can find a description of the panes in R Studio, including the "history" pane.
There you can select several lines and paste them into the code.
Also, you can use the command savehistory() to save your history in a file you can the modify. If you want to choose the name of the file to be saved, use
savehistory(file = filename)
The same option is available in the basic R GUI (MS Windows), with "Save history" in the "File" menu (as a .RHistory file). Then, you can open it with any text editor and modify your history to make a script.
To see a specific number of lines, you can use history(25) (for 25 previous lines).

editing a dataframe in R invoking vi

I am trying to edit a dataframe, content and titles from R.
there is a command edit(), but you can also invoke a vi editor by using vi([data.frame]).
You can view it and edit, it, but it saves the file to a file that I don't seem able to access and turn into a new edited data.frame.
example:
data(Orange)
test <- vi(Orange)
you should bring up a vi editor, and can change things here. if you save it, it creates a separate file in some temp directory. When you go back to R, and look at test, you'll see that none of your changes are in there.
Anyone know how to invoke a vi editor on the data.frame, such that the changes will be saved to a new data.frame?
I am running the same setup: OSX and R 3.0.1 and don't have an issue -- perhaps you're missing the saving step?
data(Orange)
test <- vi(Orange)
Then i edit the first data point, and hit the red button -- which opens a dialog box to save. You can also select save by hitting Command-S or selecting it from the menu.
This will not alter Orange, but it will pass the altered Orange to test.

Create script from console input in R

Instead of first creating a script in R and then executing it afterwards, is it possible to do it the other way around?
I.e. I would type some user input in the console, and afterwards have R compile all the user inputs from the console (without the resulting outputs), to a script for later user.
Commands that you type into the console are stored in the history file (see ?savehistory). You can edit that to get your script.

Resources