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).
Related
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?
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
I am using R Studio and I want to save my script (i.e., the upper left panel). However, the only ways that I can find to do it are by either clicking the blue floppy disk icon to save or using the drop down menu File > Save > name.R
Is there any way besides using these shortcuts to save the script to a .R file or is the shortcut the only way?
Thanks.
You can use rstudioapi::documentSave() to save the currently open script file to disk.
From the source documentation, one can see that it can be used in conjunction with the id returned with getActiveDocumentContext()$id to make sure the document saved is the one running the script.
For your intended use, try:
rstudioapi::documentSave(rstudioapi::getActiveDocumentContext()$id)
For future reference, here is the reference manual of rstudioapi:
https://cran.rstudio.com/web/packages/rstudioapi/rstudioapi.pdf
I'm not yet allowed to comment, but this refers to the comment above that this does not work with .rmd files:
rstudioapi::documentSave(rstudioapi::getActiveDocumentContext()$id)
I tried and in Rstudio Version 1.2.5042 it does seem to work.
Every new tab in R(created by ctrl+shift+n) can be independently saved by using ctrl+s in the respective tab. If you intend to rename the file though, you may do it as you would rename any file in windows(goto the file location and single click on the filename). Hope my answer was of some help!
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.
My setup: Eclipse, R and Statet
I am using R from inside Eclipse using Statet.
I have made a graph (this is the plot of a time series, in case this is relevant information).
I now wish to save this graph as a JPG image (or BMP, or the like ...), or to copy this image in the clipboard. I need my graph in a PowerPoint presentation.
Does anyone know how to do that?
Thank you
jpeg('foo.png') more help is available by typing ?jpeg at the statet prompt. You may also be able to write to the clipboard, using a destination of clipboard instead of the filename, depending on your platform -- see ?connections for further details.