Create script from console input in R - 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.

Related

R Markdown script and R

I am calling one R Markdown script from another R script.Below you can see command
rmarkdown::render((file=paste(path1,"/Dashboard.Rmd",sep="")),params=list(args = myarg))
The script is executed without any problem but is not open automatically.
So can anybody help me how to solve this problem and open this script automatically after running of this command ?
First, your syntax probably isn't doing what you intended. Writing
rmarkdown::render((file=paste(path1,"/Dashboard.Rmd",sep="")),params=list(args = myarg))
will create a new variable named file and use it as the first parameter to rmarkdown::render. A more likely way to get what you want is to write it as
outfile <- rmarkdown::render(paste(path1,"/Dashboard.Rmd",sep=""),
params=list(args = myarg))
This removes the assignment from the first argument, and saves the
result (which is the name of the file that was produced).
Now, on to your question: You need to follow that line with
rstudioapi::viewer(outfile)
to view it in RStudio, or
browseURL(outfile)
elsewhere, because rmarkdown::render doesn't automatically call a previewer.

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?

How to delete part of R studio console?

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

in R , how to get the output to a file as well as on console at the same time?

I want only the output not the commands, and I want to see the output on the console at the same time.
I tried sink and capture.output , but tried to work through the examples but I can accomplish only one of the task i.e. either to console or to a file.
I am new to R, and was thinking whether there is a function that can help me see the output on the console and save it to a text file as well?
Do sink(file="file.txt", split=TRUE).

How to save a function as new R script?

Given a function, how to save it to an R script (.R)?
Save works well with data, but apparently can not create .R data.
Copy pasting from the console to a new script file appears to introduce characters that cause errors.
Take a look at the dump function. That writes files that are R code that can be read back in with source or used in some other way.
I have to ask: why are you writing your functions in the console in the first place? Any number of editors support a "source" call, so you can update the function as you edit. Copy/pasting from the console will carry prompt characters along , if nothing else, so it's a bad idea to begin with.

Resources