I want my R script to accept data from a .csv file. Is there a way to do this from the command prompt.
Just like if I write Rscript myscript.R 20, it passes a value 20 as input. I want to know if specifying the absolute address of the csv file will allow my script to use the data inside the csv file. If not what do I have to do to achieve what I want?
Have a look at ?commandArgs. Minimal example:
#!/usr/bin/Rscript
print(commandArgs(trailingOnly=TRUE))
Run it:
./myscript.R yourcsvfile.csv
[1] "yourcsvfile.csv"
Maybe you will be interested in the getopt package, too.
Related
Is there a way to do this? I want to write a bash script that can extract data from an object in an RData file and write it to a text file.
Without being too certain about the specifics of your request (please see about creating a minimal reproducible example), does something like this work:
Assuming your .Rdata is called 'mtcars.Rdata' and contains a data.frame called mtcars and you want to write it to mtcars.csv.
You may have to change the path to where your Rscript.exe file lives.
"C:\Program Files\R\R-3.5.3\bin\Rscript" -e "load('mtcars.Rdata', env <- new.env());write.csv(env$mtcars, 'mtcars.csv')"
I am trying to provide a way of running unattended R scripts through Rscript. The problem I am having is that the default output for graphics is a PDF file in the current directory. I would like to redirect this output to a separate folder but without having to change the script too much.
If my script is something simple as:
args = commandArgs(trailingOnly = TRUE)
mydata <- get some data frame somehow
plot(tayside)
And I execute the following commandline:
Rscript.exe --vanilla --default-packages=RODBC,graphics,grDevices sample.R > C:\temp\sample.Rout
I get a Rplots.pdf in the current folder and the sample.Rout file in the C:\temp\ folder.
Is there a way to specify an output folder and have Rscript put all output there? I have tried playing with the pdf.options(...) to pre-pend a default folder to the file parameter but no can do.
Ok, apparently it was easier than I thought, no need to use pdf.options() but simply pdf() at the top of the file (after getting the arguments):
pdf(paste0(args[1], "MyFile.pdf"))
or, for multiple files:
pdf(paste0(args[1], "MyFile%03d.pdf"), onefile=FALSE)
I'm trying to convert R documentation files (extension .Rd) into plain text. I am aware that RdUtils contains a tool called Rdconv, but as far as I know it can only be used from the command line. Is there a way to access Rdconv (or a similar conversion tool) from within an R session?
Try
tools::Rd2txt("path/to/file.Rd")
You may always invoke a system command e.g. with the system2 function:
input <- '~/Projekty/stringi/man/stri_length.Rd'
output <- '/tmp/out.txt'
system2('R', paste('CMD Rdconv -t txt', filename, '-o', output))
readLines(output)
## [1] "Count the Number of Characters"
## ...
Make sure that R is in your system's search path. If not, replace the first argument of system2() above with full path, e.g. C:\Program Files\R\3.1\bin\R.exe.
I'd like to be able to write the contents of a help file in R to a file from within R.
The following works from the command-line:
R --slave -e 'library(MASS); help(survey)' > survey.txt
This command writes the help file for the survey data file
--slave hides both the initial prompt and commands entered from the
resulting output
-e '...' sends the command to R
> survey.txt writes the output of R to the file survey.txt
However, this does not seem to work:
library(MASS)
sink("survey.txt")
help(survey)
sink()
How can I save the contents of a help file to a file from within R?
Looks like the two functions you would need are tools:::Rd2txt and utils:::.getHelpFile. This prints the help file to the console, but you may need to fiddle with the arguments to get it to write to a file in the way you want.
For example:
hs <- help(survey)
tools:::Rd2txt(utils:::.getHelpFile(as.character(hs)))
Since these functions aren't currently exported, I would not recommend you rely on them for any production code. It would be better to use them as a guide to create your own stable implementation.
While Joshua's instructions work perfectly, I stumbled upon another strategy for saving an R helpfile; So I thought I'd share it. It works on my computer (Ubuntu) where less is the R pager. It essentially just involves saving the file from within less.
help(survey)
Then follow these instructions to save less buffer to file
i.e., type g|$tee survey.txt
g goes to the top of the less buffer if you aren't already there
| pipes text between the range starting at current mark
and ending at $ which indicates the end of the buffer
to the shell command tee which allows standard out to be sent to a file
I have a R function which can generate the LaTeX code (the output is the LaTex code) by using cat(), while now I want to save these LaTeX code, but I don't know which function can save these LaTeX code...
I like to use the sink() function:
latex.code <- function(){
cat("\\begin{align}\n")
cat("[X'X]^{-1}X'y\n")
cat("\\end{align}\n")
}
sink(file='ols.txt')
latex.code()
sink()
Edit: Obviously, you can choose the file path where the file will be saved by changing the sink argument such as: sink(file='c:/Users/Eva/Desktop/ols.txt'), or sink(file='~/ols.txt')
Assuming your R function returns a character string of LaTeX code (your question would be much improved if you made it more concrete, with some specific examples), you can output something like that to a file using the cat() function, and specifying a file using the file= argument. You can read about it via ?cat.
If it happens that you have your output in a character vector (i.e. you are using something like cat(<something>) to have it written to the console), you can use writeLines function, like this:
writeLines(<something>,"filename.txt")
However the best way to make a LaTeX file in R is to use either Sweave or make a brew template.