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')"
Related
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 writing an R script whose contents can change from time to time. It would be really helpful if I could insert a command that would copy the current contents of the script to a file, so I can go back later and see exactly what commands I executed during that run of the code.
How can I do this?
You can do this with the teaching demos package:
install.packages("TeachingDemos")
library(TeachingDemos)
#Will write to a file in the working directory
txtStart("captureCode.txt")
#This comment will not appear in the file, all commands and output will
Sys.Date()
#This command ends writing to the file
txtStop()
Source
I got a question about reading a file into data frame using R.
I don't understand "getwd" and "setwd", do we must do these before reading the files?
and also i need to print some of the columns in the data frame, and only need to print 1 to 30,how to do this?
Kinds regards
getwd tells you what your current working directory is. setwd is used to change your working directory to a specified path. See the relevant documentation here or by typing ? getwd or ? setwd in your R console.
Using these allows you to shorten what you type into, e.g., read.csv by just specifying a filename without specifying its full path, like:
setwd('C:/Users/Me/Documents')
read.csv('myfile.csv')
instead of:
read.csv('C:/Users/Me/Documents/myfile.csv')
I'm having a little trouble here using the shell command in R. I have the a java JAR file that takes as input a file containing a character vector (1 tweet per line). I'm calling it from the shell function:
shell("java -Xmx500m -jar C:/Users/User/Documents/R/java/ark-tweet-nlp-0.3.2/ark-tweet-nlp-0.3.2.jar --input-format text C:/Users/User/Documents/R/java/ark-tweet-nlp-0.3.2/examples/test.txt",intern=T)
Rather than pull the character vector from a text file external to the R environment, I want to be able to pass a vector that I have preprocessed within R. For example, if the file "text.txt" is imported into R as a character vector called test, I thought I could do this:
shell(paste("java -Xmx500m -jar C:/Users/User/Documents/R/java/ark-tweet-nlp-0.3.2/ark-tweet-nlp-0.3.2.jar --input-format text",test,sep=" "),intern=T)
But the jar file that is being called needs to actually read the file name, not the file contents. My workaround is to write the preprocessed file to my drive and then reimport using the shell script, but that is clunky and will mess up later processing I plan on doing.
Use the system command set to create an environment variable, then read it from java. The shared location will be the environment variable table.
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.