The question might be unclear -sorry about that- but is pretty simple.
I'm currently writing a Rscript for which I'd like to send all the terminal's outputs to a log file, using the logr package, without using any redirection with >.
However, I have issues to redirect to the log file all the various statements that print to the console by the R commands of an other package (which is dada2).
For example, when I use a command of dada2, it prints some various informations to the console while computing its stuff, but these informations are not in the final log file.
Is there a way to correct this or should I use an other package or a simple redirection with >?
Thanks.
Related
I have been asked to implement a shell of my own that works similar to that in the UNIX system.I am having confusion regarding how to implement I/O redirection.
I am able to run some basic commands like "ls" and "cat" using my program.
It would be of great help, if someone can guide me with the "how-to's" of the required problem.I have no clue where to start from.
I have no clue where to start from.
Start from reading about dup2(). For just redirecting output or input, there's not much about it.
To redirect standard output to file descriptor ofd, call dup2(ofd, 1), to redirect standard input from file descriptor ifd, call dup2(ifd, 0), each just before you run some basic command.
I'm trying to spawn a sub-process in R using the subprocess library, as presented in this tutorial. The Problem is that the program I'm trying to launch requires an additional command after the executable.
Example:
I would launch the command from the shell like this:
monetdbd create mydb
where 'create' is the additional command and 'mydb' a parameter.
I tried giving 'create mydb' as parameters in R like this:
handle <- spawn_process('/usr/local/bin/monetdb', c('create mydb'))
However from the output I got with
process_read(handle, PIPE_STDOUT, timeout = 3000)
I conclude that the parameters don't work as I'm getting the info message from monetdb on how to call it, just as if I call only 'monetdb' without the create command from the shell:
Usage: monetdb [options] command [command-options-and-arguments]
The second thing I tried is to include the create command into the path, but this leads to a "No such file and directory" error.
Any hints are appreciated.
MonetDB is the daemon process for MonetDB and has little to do with the (now old) version of MonetDBlite used in R. The latter one is decommissioned from CRAN and a newer version of MonetDBlite is expected to arrive early next year.
Without knowing anything about the package you’re using, and going purely by the documentation, I think you need to separate the command line arguments you pass to the functions:
handle <- spawn_process('/usr/local/bin/monetdb', c('create', 'mydb'))
This also follows the “conventional” API of spawn/fork/exec functions.
In addition, using c(…) is (almost) only necessary when creating a vector of multiple elements. In your code (and in the tutorial) it’s unnecessary around a single character string.
Furthermore, contrary to what the tutorial claims, this functionality is actually already built into R via the system2 and pipe functions (although I don’t doubt that the subprocess package is more feature-complete, and likely easier to use).
But if your ultimate goal is to use MonetDB in R then you’re probably better advised following the other answer, and using dedicated MonetDB R bindings rather than interacting with the daemon binary via subprocess communication.
I am creating a function in R and in one step of the function I run a batch file, this batch file in turn runs a different program which creates files that I then want to read in my function.
I am using shell.exec to run the batch file and it runs fine, the problem is that the next line of my code that wants to read in the output from the program ran by the batch file crashes because it hasn't been created yet.
So I get an error the first time I call my function but if I simply call it again it runs fine. Example of code below: Basically what happens is I get an error message when calling the function saying that .../bat_output.txt does not exist, because the batch file hasn't been run yet, but then when I call the function again, it works fine.
shell.exec("run.bat")
readout<-read.table("bat_output.txt")
Any suggestions?
shell.exec returns immediately while the script is running in the background. The reason bat_output.txt is not found the first time is likely that the script has not finished yet. shell.exec does not give you the ability to wait or any information to determine if the process is still running, so it might not be the best tool for this.
Alternatives:
system("cmd /c run.bat")
system2("cmd", c("/c", "run.bat"))
Realize that if you reference a different path, you might want/need to normalizePath and/or dQuote it going into those commands. (R's system* commands are bad at argument forming.)
I am a professor at a small college, and I am working with a blind student in a statistics course. We have found that R is by far the most accessible system to use, and for the screen reader to work (i.e. for it to read the output), we need to run R out of the Command Prompt (the student uses a Windows machine, specifically one running Windows 7). Though using the R console would probably be easier to operate, for some reason the screen reader being used by the student (JAWS) doesn't read the output in the console.
We have gotten to the point where we can use R out of the Command Prompt just fine, but the issue is that we cannot save the command history and the output from the Command Prompt to an external file (e.g. a .txt). I understand that I can use the sink() function to get the output in a .txt file, and I can also use the savehistory() function to get the command history, but I need something that captures both. We have also tried to use CMD outside of the R environment to try and print the full session of the Command Prompt, but that doesn't seem to capture the work that we might do during an R session.
On a Mac, this wouldn't be a problem, but I am not the best with using the Windows Command Prompt.
Any help would be greatly appreciated, and if more detail is required, I am more than happy to provide it.
I'm trying to replicate the toy example found here:
https://www.r-bloggers.com/jug-easily-create-r-apis/
When I load the jug package, and run the script, my R console just seems to run continuously, thus I not able to attempt the second part of the code where I submit the curl request. I basically want to create a few functions that accept HTTP input, and host the functions locally. What am I missing?
See the screenshot image of my R console
Did you try going to http://127.0.0.1:8080? Do it while the script is running (as per your screenshot). It should prompt you the response of the function. In any case, I had similar issues with jug, so I recommend using plumber instead.