R: read contents of text file as a query? - r

Using R, I just want to read the contents of a file into a variable like:
query <- read_file_contents('biglongquery.sql')
As to avoid putting, well, big long queries in the R script itself. I do not want to read in data like CSV (e.g. read.tables), etc- just the raw text.

Scan does the job, but the function for this purpose is actually readLines().
query <- readLines("biglongquery.sql")
This gives you a vector with the lines. To combine them to one single variable, you can use the paste function, e.g.
one.variable <- paste(query,collapse="\n")

x <- paste(scan("foo.sql",what="",sep="\n",blank.lines.skip=FALSE),collapse="\n")

Another way is to create a .R script with query definition
# content of biglongquery.R
query <- "
SELECT
very_long_list_of_fields
FROM ...
"
and then use it in the main script using
source("biglongquery.R")

Related

How to Optimize the URL in R

can anybody help me through the URL optimization in R.
Actually I need to get multiple organization related CSV file from URL.
I have to just change the one parameter in url to get that particular organization CSV table.
So mainly I was writing a function to extract that.
Example:
data<-function("bank"){table<-read.csv(url("*://.....=string.....")}
can you guide me on this.
Is that what you are looking for?
Data <- function(string){
table <- read.csv(url(paste("https://.....=", string, "...", sep="")))
# ...
}
# then you can change the parameter with function call
# (also dynamically using apply or a loop )
Data(string="bank")

How to create a new read.csv in R so it can read .csv file without typing the full name of .csv file

guys, thanks for read this. This is my first time writing a program so pardon me if I make stupid questions.
I have bunch of .csv files named like: 001-XXX.csv;002-XXX.csv...150-XXX.csv. Here XXX is a very long name tag. So it's a little annoying that every time I need to type read.csv("001-xxx.csv"). I want to make a function called "newread" that only ask me for the first three digits, the real id number, to read the .csv files. I thought "newread" should be like this:
newread <- function(id){
as.character(id)
a <- paste(id,"-XXX.csv",sep="")
read.csv(a)
}
BUt R shows Error: unexpected '}' in "}" What's going wrong? It looks logical.
I am running Rstudio on Windows 8.
as.character(id) will not change id into a character string. Change it to:
id = as.character(id)
Edit: According to comments, you should call newread() with a character paramter, and there is no difference between newread(001) and newread(1).
This is not specifically an answer to your question (others have covered that), but rather some advice that may be helpful for accomplishing your task in a different way.
First, some of the GUI's for R have file name completion. You can type the first part: read.csv("001- and then hit a key or combination of keys (In the windows GUI you press TAB) and the rest of the filename will be filled in for you (as long as it is unique).
You can use the file.choose or choose.files functions to open a dialog box to choose your file using the mouse: read.csv(file.choose()).
If you want to read in all the above files then you can do this in one step using lapply and either sprintf or list.files (or others):
mycsvlist <- lapply( 1:150, function(x) read.csv( sprintf("%03d-XXX.csv", x) ) )
or
mvcsvlist <- lapply( list.files(pattern="\\.csv$"), read.csv )
You could also use list.files to get a list of all the files matching a pattern and then pass one of the returned values to read.csv:
tmp <- list.files(pattern="001.*csv$")
read.csv(tmp[1])

Read SPecific lines of a CSV file in R-language

I am trying to write a code which manipulates data from a particular .csv and writes the data to another one.
I want to read each line one by one and perform the operation.
Also I am trying to read a particular line from the .csv but what I am getting is that line and the lines before it.
I am a beginner in R-Language, so I find the syntax a bit confusing.
testconn<=file("<path>")
num<-(length(readLines(testconn)))
for(i in 1:num){
num1=i-1
los<=read.table(file="<path>",sep=",",head=FALSE,skip=num1,nrows=1)[,c(col1,col2)]
write.table(los,"<path>",row.names=FALSE,quote=FALSE,sep=",",col.names=FALSE,append=TRUE)
}
This is the code I am currently using, thought it is giving the desiored output but it is extremely slow, my .csv data file has 43200 lines.
Your code doesn't work. You confuse the comparison operator <= and the assignment one <-
Your code is is extremly innefficient. You call both read.table and write.table 43200 times to read/write a single file.
You can simply do this:
los<- read.table(file="<path>",sep=",")[,c(col1,col2)]
res <- apply(los,1,function(x){## you treat your line here}
write.table(res,"<path_write>",row.names=FALSE,
quote=FALSE,sep=",",col.names=FALSE)

How to run shell script in R and get the output into table?

I know to run a shell script in R is using system command:
my.table <- system(command,intern=TRUE)
However, if the result of my "command" is to print out a table, and I want R to read the table directly into its own data structure. (something like data frame) Is there an easy way to do that? Because the current output in "table" is a character string table. What I want is the R object as read.table().
If the result 'table' has white-space separators and carriage-returns to mark lines, then you should pass the results to the 'text' argument of read.table:
inp.tbl <- read.table(text = system(command,intern=TRUE) )
I expect using pipe will be more efficient in memory and time than system with intern
inp.tbl <- read.table(pipe(command) )

problem creating dynamic file name in R

I'm working on a script in R that processes some data and writes an output file. I'd like that output file to be named in a way that reflects the input file, and I'd like something about the file to be unique so older files aren't overwritten.
So I thought to use a timestamp. But this isn't working the way I'd hoped, and I'd like to understand what's happening and how to do this correctly.
This is how I'm trying to name the file (file_base is the name of the input file):
now<-format(Sys.time(), "%b%d%H%M%S")
outputfile<-cat(file_base, "-",now,"-output.txt", sep="")
The output of this pair of functions looks great. But executing 'outputfile' subsequently results in 'NULL' as output.
What's happening here and how can I create an output filename with the properties that I'd like?
You're confusing cat and paste. You want:
outputfile <- paste(file_base, "-",now,"-output.txt", sep="")
You can also use the function sprintf(), it's a wrapper for the C function.
example:
filepath <- file.path(outdir, sprintf("abcdefg_%s.rda", name))
You could also use the separator argument of paste:
outputfile <- paste(file_base,now,"output.txt", sep="-")

Resources