Dribble in R: how to duplicate all i/o into a file? - r

Is there something like the Common Lisp function DRIBBLE or Unix command tee in R?
Specifically, I want everything I type and everything R prints back to me to be appended to a file (tee only captures stdout; I want everyting: errors, warnings, print, cat, my input).
I found a 10 year old message on the subject which offers a weak version of that (it does not capture the output from cat/print).
The standard function sink only captures (not duplicates) the R's output; and it does not capture my input.
Is there a better way?

Look at the txtStart function (and related functions) in the TeachingDemos package. I think that it does everything you want except capturing errors (and the TaskCallback system in the R guts needs to be updated for that to happen).
The other option is to run R inside of another environment such as ESS (inside of Emacs) (there are others, but I am less familiar with them). Then everything is captured in the editor/buffer and can be saved to a file.
Note that the sink function does have a split argument that works like tee to show the output on screen as well as duplicate it to the file, but it still only does the output, not the input commands.

You probably want sink() -- see help(sink) for examples.

Related

Knitr - Define Latex commands that call R functions using Sexpr

I'm basically wondering how to define a new Latex command such that it allows the nesting of Sexpr and some other R function, where the Latex argument is an R object.
As fortunute happenstance, the idea somewhat is transmitted by the new command structure given below:
\newcommand{\SomeLatexCommand}[1]{\Sexpr{"#1"}}
Where fortunately the argument is indeed shown, albeit in string. With this in mind, I was hoping upon the following command:
\newcommand{\SweetLatexCommand}[1]{\Sexpr{SomeRFunction(get("#1"))}}
However, once inside nested inside an R function, #1 is not read as a placeholder for the Latex argument, but instead as an existing R variable.
Is there a way to make the last comand work? Or else, are there also other neat ways to define Latex commands which in turn can call on any R function through R objects?
Good day,
No, you can't do that. The problem is the way knitr works:
R runs the knit() function (or some other knitr function). That function looks through the source for code chunks and \Sexpr calls, executes them, and replaces them with the requested output, producing a .tex file.
Then LaTeX processes that .tex file. R is no longer involved.
Since \newcommand is a LaTeX command, it is only handled in the final stage, after all R evaluation is done.
There may be a way in knitr to specify another "macro" that works the way \Sexpr works, but I don't think there's a way to have several of them.
So what you should do is write multiple functions in R, and call those to do what you want, as \Sexpr{fn1(...)}, \Sexpr{fn2(...)}, etc.
I suppose if you were really determined, you could add an extra preprocessor stage at the beginning, that went through your Rnw file and replaced all strings that looked like \SweetLatexCommand{blah} with \Sexpr{SomeRFunction(get("blah"))} and then called knit(), but that seems like way too much work.

Lots of unnecessary and repititious output in clisp when I paste in new functions

I just started using clisp again (after about 20 years). Now, I get the following text after it displays EACH line when I paste in a new function:
You are in the top-level Read-Eval-Print loop.
Help (abbreviated :h) = this list.
Use the usual editing capabilities.
Generally, the function still works. This happens even when I start with a fresh copy of clisp. How can I get clisp to stop this seemingly unnecessary output? I thought that it should take in the entire function and then let me know if it found errors. It does this even when there are no errors.
The code you paste probably contains Tab characters.
PS. You now owe me 10 zorkmids.

Is there a fast way to copy and paste function arguments to R console

using R and debugging, I often might have a function with several arguments set by default.
e.g.
foo <- function(x=c(3,4,5), y= 'house', dateidx = '1990-01-01'){}
Often I just want to manually run through some lines in the function, while using the pre-set parameters. If the parameter list is long, I have to type or paste each argument to the console manually before stepping through the function.
x=c(3,4,5)
y= 'house'
dateidx = '1990-01-01'
It's ok if the list of arguments is small but if there is a long list of arguments, it gets tedious. Is there some way to just copy the whole set of arguments, paste to console, and do something like unlist, so that all the arguments are passed to the console as if I manually passed each one?
p.s. I'm weakly familiar with the debug tool, but sometimes I find it easier and faster to just troubleshoot lines quickly and manually as above.
There is no easy pre-existing way to do this--mainly because this a problem solved by the debugger.
One could imagine hacking something together that might parse these parameters with a regex and set them automatically--or something like that. However, the effort would be much better spent learning how to use the debugger.
It should be quite quick to test the part of the code you are interested in with the debugger if you learn how to use it. RStudio has a visual debugger. Using this, you can simply mark the command you are interested in testing with a breakpoint and run the script. The script will run until it reaches the breakpoint, then stop there so you can inspect what is happening.

Hide function definitions from the history for easier debug

Just suppose we are debugging a function foo(), and we want to modify it again and again and run it with some arguments - foo(bar="Hello", baz="How are you?") - to be sure the problem is solved.
After a modification of the foo() body, we run the lines of the function definition - to make the function modified - and now, we have to search over the history for the line containing foo(bar="Hello", baz="How are you?") to see if the modified foo() works properly.
Searching the history can also be replaced by continued pressing the "Up" key until it reaches before the function definition, when the last time we run foo(bar="Hello", baz="How are you?").
The other possibility is to keep foo(bar="Hello", baz="How are you?") in the clipboard and every time we modify the foo() body, we just paste foo(bar="Hello", baz="How are you?") from the clipboard and run it.
But all of these solutions are quite hard if we are modifying several functions with long bodies at the same time. The best possibility I have taught is to hide the function definitions from the history - when we are working with native R environment or with IDEs like RStudio. Is there any possibility to do this? Is there any better solution?
You can source() the function definition from file rather than "copy-paste" (or otherwise run) the function code block from the IDE/editor. Sourcing won't show in the R console if you do this (by default anyway). Most reasonable editors have a keyboard shortcut to source/load the function buffer/file/window into R via source() rather that by "pasting" - on Emacs+ESS it is C-c C-l for example.
You could use a sensible editor like Emacs with ESS which doesn't interleave code sent from code buffers into the R buffer, so you don't have to up-key back from the function definition, only back through the history.
At least on Linux you can use the common Ctrl+r and then start typing the first few characters of the function call you want, which will do a reverse search for the thing you are typing and then upon Enter will run that command/line.

Switch R script from non-interactive to interactive

I've an R script, that takes commandline arguments, where the top line is:
#!/usr/bin/Rscript --slave
I wanted to interrupt execution in a function (so I can interactively use the data variables that have been loaded by that point to work out the next bit of code I need to write). I added this inside the function in question:
browser()
but it gets ignored. A bit of searching suggests it might be because the program is running in non-interactive mode. But even more searching has not tracked down how I switch the script out non-interactive mode so that browser() will work. Something like a browser_yes_I_really_mean_it() function.
P.S. I want to avoid altering the rest of the script if at all possible. My current approach is to copy and paste the code chunks, needed to prepare the data, into an interactive session; but as the script gets more and more complex this is getting more and more unreasonable.
UPDATE: for anyone else with the same question, it appears the answer to the actual question is that it is impossible. Once you start R in a non-interactive mode the die is cast. The given answers are therefore workarounds: either you hack your code (remembering to unhack it afterwards), or you refactor to make debugging easier. (This comment is not intended as a criticism of the answers; the suggested refactoring makes the code cleaner anyway.)
Can you just fire up R and source the file instead?
R
source("script.R")
Following mdsumner's answer, I edited my script like this:
if(!exists("argv")){
argv=commandArgs(TRUE)
if(length(argv)!=4)usage_and_exit()
}else{
if(length(argv)!=4){
stop("Must set argv as a 4 element vector. E.g. argv=c(...)")
}
}
Then no other change was needed, and I was able to do:
R
> argv=c('a','b','c','d')
> source("script.R")
In addition to the previous answer, I'd create a toplevel function (e.g. doStuff) which performs the analysis you want to perform in batch. The function takes the cmd line options as input. In the batch script you source the script that contains this function and call it. In this way you can easily run the function in interactive mode and use e.g. browser().
In some cases, the suggested solution (workaround) may not work - for example, when the R code needs to be run as a part of an existing bash script. For those cases, I suggest to write in your R script into the bash script using here document:
#!/bin/bash
R --interactive << EOT
# R code starts here
argv=c('a','b','c','d')
print(interactive())
# Rest of script contents
quit("no")
# R code ends here
EOT
This way, print(interactive()) above will yield TRUE.
Sidenote: Make sure to avoid the $ character in your R code, as this would not be processed correctly - for example, retrieve a column from a data.frame() by using df[["X1"]] instead of df$X1.

Resources