I'm coding in R.
I thought I was making pretty good progress in R. I had created a script that read the command line arguments (there was 1 single argument, it was the name of a data file), read in data from a .csv, performed a regression and printed some data.
Somewhere along the way, I modified something and everything went out the window. Subsequently, I have torn down my entire script and I now have the most basic script and it is behaving strangely. Here is what I've got now:
#NOTE: To run the script use the following command in terminal R CMD
#BATCH scriptname arguments #store the current directory
initial.dir<-getwd()
#set the output
sink("PROGOUT.out")
#Get commandline arguments
args<-commandArgs(trailingOnly=TRUE)
print(args)
"HELLO WORLD"
I expected this program to create a file, PROGOUT.out, that contains the commandline arguments and the phrase HELLO WORLD.
At the command prompt I ran,
$ R CMD BATCH R_ScriptV3.R test1 test2 test3 test4
The program creates two files, 'test1' and 'PROGOUT.out'
This is the contents of PROGOUT.out:
character(0)
[1] "HELLO WORLD"
proc.time() user system elapsed 0.201 0.026 0.219
This is the contents of test1:
R version 3.1.2 (2014-10-31) -- "Pumpkin Helmet" Copyright (C) 2014 The R Foundation for Statistical Computing Platform: x86_64-apple-darwin10.8.0 (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. [Previously saved workspace restored] >
NOTE: To run the script use the following command in terminal
R CMD BATCH scriptname arguments > > #store the current directory
initial.dir<-getwd()
set the output
sink("PROGOUT.out")
Get commandline arguments
args<-commandArgs(trailingOnly=TRUE)
print(args)
"HELLO WORLD" >
So... 1) why did the program create the file 'test1'? This wasn't happening when I started scripting but it appears the first argument in the command prompt becomes a file with some header information and a copy of the program?, 2) PROGOUT.out does contain the 'HELLO WORLD' statement but it does NOT contain any other arguments .... why not and how do I read those arguments into my script.
I could have SWORN this was all working just fine before. Any ideas?
You're looking for Rscript:
Rscript R_ScriptV3.R test1 test2 test3 test4
The params of R CMD BATCH work differently:
R CMD BATCH '--args test1 test2 test3 test4' R_ScriptV3.R output.Rout
Related
I recently upgraded to R version 4.0.2 and RStudio 1.3.1056 and believe that may be at the root of this problem. After copying a section of my Shiny code to create a minimal reproducible example (mprex), I get an error for a section of code that's not in the mprex. I cleared my RStudio workspace and rebooted both my computer and RStudio, but continue to get the same error.
Here's my code:
library(shiny)
# Define UI
ui <- (fluidPage
(
fileInput("sessionFileName", "Session File Name")
)
)
# Define server logic
server <- (function(input, output, session)
{
inputParameters <- reactive ({
if (is.null(input$sessionFileName)) return
inFile <- input$sessionFileName
if (!is.null(inFile)) {
inputData <- readRDS(file=inFile$name)
}
inputIDs <- names(inputData)
inputvalues <- unlist(inputData)
for (i in 1:length(inputData)) {
print (paste(inputIDs[i],inputvalues[[i]],sep=' '))
session$sendInputMessage(inputIDs[i], list(value=inputvalues[[i]]) )
}
})
})
# Start the shiny app
shinyApp(ui = ui, server = server)
And here's the error I get when I try to run my code:
Error in reqHistoricalData(tws, twsEquity(equity), yesterday4PM, backtestFrequency, :
could not find function "reqHistoricalData"
It's pretty clear the error has nothing to do with the code I'm trying to run. I realize my code may have other errors. The error appears when I launch my code. My guess is that someone running this code on a different machine would not get the same error, but I don't know yet if that's true.
Here's the startup banner when I start an RStudio session.
R version 4.0.2 (2020-06-22) -- "Taking Off Again"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin17.0 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
[Workspace loaded from ~/Library/MobileDocuments/com~apple~CloudDocs/r Dev/.RData]
For this last line of the startup banner, "[Workspace loaded from ~...]", I don't understand if there's a way for me to delete and/or reset the workspace that's being loaded. Perhaps that's the solution to this problem.
I do have many packages in my environment, including IBrokers, to which this error refers. I've been running other Shiny scripts that access the function reqHistoricalData().
Is this a bug in either Shiny and/or R? Any ideas about how to get this code to run?
I have the R software installed on my MacBook, but the problem is that the default language is set on "russian", I really don't know why. When I call R from my terminal I see the message below
Giulios-MBP:~ opisthofulax$ R
R version 3.2.4 (2016-03-10) -- "Very Secure Dishes"
Copyright (C) 2016 The R Foundation for Statistical Computing
R version 3.2.4 (2016-03-10) -- "Very Secure Dishes"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin13.4.0 (64-bit)
R -- это свободное ПО, и оно поставляется безо всяких гарантий.
Вы вольны распространять его при соблюдении некоторых условий.
Введите 'license()' для получения более подробной информации.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Введите 'demo()' для запуска демонстрационных программ, 'help()' -- для
получения справки, 'help.start()' -- для доступа к справке через браузер.
Введите 'q()', чтобы выйти из R.
>
As you can notice the software is half in russian half in english, and, obviously, I would like to change the default language to ENG.
I've found a temporary solution here on StackOverflow
but it doesn't solve my problem, because every time I call R from my terminal it's again in RUS.
I was wondering if there's a definitive solution/command to change and set the default language to ENG. Thanks all.
=
I didn't get the problem solved, since, even if I took a look here, on the other SO link.
I wasn't able to locate the file Rconsole, which seems to be the key to change the language.
I looked for it on my MacBook (and maybe the problem is because of the new Sierra OS) with the search tool, but the file seems not to be on my computer.
So the problem is actually still there. Any suggestion?
There are several questions dealing with that problem:
- How to change language settings in R
- The R console is in my native language, how can I set R to English?
Type locale in the terminal and check your language settings. If you want everything to be in US english, it should look like this
locale
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL="en_US.UTF-8"
In your case, I assume that it is a mix of en_US.UTF-8 and your local russian language setting.
These variables are set system wide by your language & region settings. However, you can also set them in your ~/.bash_profile. For example, I use the following settings:
# language settings
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
Restart R/RStudio and everything should work.
Also, this worked for me, OS X Big Sur with R console from Anaconda.
which R showed /opt/anaconda3/envs/rm_resolutions/lib/R/bin/R
Good, it's using Anaconda
(rm_resolutions is my active virtual environment)
cd /opt/anaconda3/envs/rm_resolutions/lib/R/etc
Do at least one of these:
Add LANGUAGE=en to the Renviron file there.
Create Rconsole with language = en
For example, echo language = en > Rconsole
I did both of step 3, not sure which is used / preferred.
I also have the R.app installed, which is totally separate.
I am new to programming and mainly I am able to do some scripts within R, but for my work I need to call an external program. For this program to work on the ubuntu's terminal I have to first use setenv and then execute the program. Googling I've found the system () and Sys.setenv() functions, but unfortunately I can make it function.
This is the code that does work in the ubuntu terminal:
$ export PATH=/home/meme/bin:$PATH
$ mast "/home/meme/meme.txt" "/home/meme/seqs.txt" -o "/home/meme/output" -comp
Where the first two arguments are input files, the -o argument is the output directory and the -comp is another parameter for the program to run.
The reason that I need to do it in R despite it already works in the terminal is because I need to run the program 1000 times with 1000 different files so I want to make a for loop where the input name changes in every loop and then analyze every output in R.
I have already tried to use:
Sys.setenv(PATH="/home/meme/bin"); system(mast "/home/meme/meme.txt" "/home/meme/seqs.txt" -o "/home/meme/output" -comp )
and
system(Sys.setenv(PATH="/home/meme/bin") && mast "/home/meme/meme.txt" "/home/meme/seqs.txt" -o "/home/meme/output" -comp )
but always received:
Error: unexpected constant string in "system(mast "/home/meme/meme.txt""
or
Error: unexpected symbol in "system(Sys.setenv(PATH="/home/meme/bin") && mast "/home/meme/meme.txt""
At this point I have run out of ideas to make this work. If this has already been answered, then my googling have just been poor and I would appreciate any links to its response.
Thank you very much for your time.
Carlos
Additional details:
I use Ubuntu 12.04 64-bits version, RStudio version 0.97.551, R version 3.0.2 (2013-09-25) -- "Frisbee Sailing" Platform: x86_64-pc-linux-gnu (64-bit).
The program I use (MAST) finds a sequence pattern in a list of letters and is part of the MEME SUIT version 4.9.1 found in http://meme.nbcr.net/meme/doc/meme-install.html and run through command line. The command-line usage for mast is:
mast <motif file> <sequence file> [options]
Construct the string you want to execute with paste and feed that to system:
for(i in 1:10){
cmd=paste("export FOO=",i," ; echo \"$FOO\" ",sep='')
system(cmd)
}
Note the use of sep='' to stop paste putting spaces in, and back-quoting quote marks in the string to preserve them.
Test before running by using print(cmd) instead of system(cmd) to make sure you are getting the right command built. Maybe do:
if(TESTING){print(cmd)}else{system(cmd)}
and set TESTING=TRUE or FALSE in R before running.
If you are going to be running more than one shell command per system call, it might be better to put them all in one shell script file and call that instead, passing parameters from R. Something like:
cmd = paste("/home/me/bin/dojob.sh ",i,i+1)
system(cmd)
and then dojob.sh is a shell script that parses the args. You'll need to learn a bit more shell scripting.
I would like to produce a log-file which keeps track of all commands (stdin), results (stdout) and errors/warnings/messages (stderr) in the R console.
I am aware that there are a lot of logging-packages and I tried several like TeachingDemos (seems to ignore stderr completely) or R2HTML (seems to ignore messages), however, none of them seems to include everything from stderr.
Only knitr and markdown seem to be able to include everything into a single file. But using this workaround, I have to write R-scripts and I cannot freely write commands in the console. Furthermore, I cannot include the knitr or markdown command in the same R-script (which is of course a minor problem).
Here is an example:
library(TeachingDemos)
library(R2HTML)
library(TraMineR)
logdir <- "mylog.dir"
txtStart(file=paste(logdir,"test.txt", sep=""), commands=TRUE,
results=TRUE, append=FALSE)
HTMLStart(outdir = logdir, file = "test", echo=TRUE, HTMLframe=FALSE)
## Messages, warnings and errors
message("Print this message.")
warning("Beware.")
"a" + 1
geterrmessage()
## Some example application with the TraMiner package
## which uses messages frequently
data(mvad)
mvad.seq <- seqdef(mvad[, 17:86])
mvad.ham <- seqdist(mvad.seq, method="HAM")
txtStop()
HTMLStop()
If you are running R from a Unix/Linux/Mac/etc. terminal, you can do:
R | tee mydir/mylog.txt
On windows, you can run the script in
R CMD BATCH yourscript.R
and your result will appear in the same folder as yourscript.out
On unices, I've often used the following code idiom with bash:
Some Command 2>&1 | tee NameOfOutputFile.txt
The "2>&1" says to take stderr and redirect it to stdout, which then gets piped to "tee". I will be experimenting with this and other ways of logging an R session.
Another unix trick is the "script" command, which starts a subshell whose I/O (basically everything you type and see in return) is logged to the specified file. And then exit the shell to end the script. Again, subject to experimentation. Since "sink" is native to R, I'll be trying that first.
By the way, I picked these tricks up using solaris, but they work the same running Cygwin, the unix emulator on Windows. Long time ago, I found that my Cygwin images were more up-to-date than the institutional installations of Solaris because the administrators had much more responsibility than just keeping the Solaris up-to-date. Mind you, the institutional machines were more powerful, so even though Cygwin was way more convenient, my personal machine simply didn't fill the need.
AFTERNOTE:
I took example code from page 99 of Shumway's Time Series Analysis and Its Applications With R examples. Here are the contents of a test file palette.R:
r
plot(gnp)
acf2(gnp, 50)
gnpgr = diff(log(gnp)) # growth rate
plot(gnpgr)
acf2(gnpgr, 24)
sarima(gnpgr, 1, 0, 0) # AR(1)
sarima(gnpgr, 0, 0, 2) # MA(2)
ARMAtoMA(ar=.35, ma=0, 10) # prints psi-weights
quit("no")
exit
I invoked it using:
script < palette.R
It captures the commands from palette.R and the corresponding output. So, seems usable for batch mode. For interactive mode, I'm going to go with my original plan and use sink.
I had a similar problem and in my case, I was unable to redirect truly all the output into my log file, as it was dependent on R running in interactive mode.
Specifically, in my log file, I wanted to be able to keep track of the progress bar generated by the rjags::update() function which, however, requires interactive mode:
The progress bar is suppressed if progress.bar is "none" or NULL, if
the update is less than 100 iterations, or if R is not running
interactively.
Therefore, I needed to trick R into thinking that it was running interactively, while it was in fact run from a bash script (interactive_R.sh, below) using here document:
interactive_R.sh
#!/bin/bash
R --interactive << EOT
# R code starts here
print(interactive())
quit("no")
# R code ends here
EOT
(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.)
Then you can simply run this script and send its contents into a log file using the bash command below:
$ ./interactive_R.sh > outputFile.log 2>&1
Your log file will then look as follows:
outputFile.log
R version 4.0.2 (2020-06-22) -- "Taking Off Again"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-conda_cos6-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
>
> # R code starts here
> print(interactive())
[1] TRUE
> quit("no")
After seven hours of googling and rereading through somewhat similar questions, and then lots of trial and error, I'm now comfortable asking for some guidance.
To simplify my actual task, I created a very basic R script (named test_script):
x <- c(1,2,3,4,5)
avg <- mean(x)
write.csv(avg, file = "output.csv")
This works as expected.
I'm new to python and I'm just trying to figure out how to execute the R script so that the same .csv file is created.
Notable results come from:
subprocess.call(["C:/Program Files/R/R-2.15.2/bin/R", 'C:/Users/matt/Desktop/test_script.R'])
This opens a cmd window with the typical R start-up verbiage, except there is a message which reads, "ARGUMENT 'C:/Users/matt/Desktop/test_script.R' __ ignored __"
And:
subprocess.call(['C:/Program Files/R/R-2.15.2/bin/Rscript', 'C:/Users/matt/Desktop/test_script.r'])
This flashes a cmd window and returns a 0, but no .csv file is created.
Otherwise, I've tried every suggestion I could identify on this site or any other. Any insight will be greatly appreciated. Thanks in advance for your time and efforts.
Running R --help at the command prompt prints:
Usage: R [options] [< infile] [> outfile]
or: R CMD command [arguments]
Start R, a system for statistical computation and graphics, with the
specified options, or invoke an R tool via the 'R CMD' interface.
Options:
-h, --help Print short help message and exit
--version Print version info and exit
...
-f FILE, --file=FILE Take input from 'FILE'
-e EXPR Execute 'EXPR' and exit
FILE may contain spaces but not shell metacharacers.
Commands:
BATCH Run R in batch mode
COMPILE Compile files for use with R
...
Try
call(["C:/Program Files/R/R-2.15.2/bin/R", '-f', 'C:/Users/matt/Desktop/test_script.R'])
There are also some other command-line arguments you can pass to R that may be helpful. Run R --help to see the full list.
It might be too late, but hope it helps for others:
Just add --vanilla in the call list.
subprocess.call(['C:/Program Files/R/R-2.15.2/bin/Rscript', '--vanilla', 'C:/Users/matt/Desktop/test_script.r'])