How to create a loop function that will create data frames by using different sql queries - r

I try to create a loop that will create data frames by using different sql queries that have the same name exept the day number.For example here is the name for query that is for day 1 :SF_2013_Day1_BaseLine and this is for day 6: SF_2013_Day6_BaseLine. I wrote this code (below) but I got an error : Error: unexpected ',' in "for(i in 3," .So any Idea how can i get this code to work ?
Thank you
for (i in 1,3,6,10,14,21,30) {
SF_FinalviewQ3_2013_Day[i]_BaseLine<-sqlQuery(DB,"select * from [SF_2013_Day[i]_BaseLine]");
dim(SF_Day[i]_BaseLine)}
After a change based on #Pgibas edvice to this code :
for (i in c(1,3,6,10,14,21,30)) {
SF_FinalviewQ3_2013_Day[i]_BaseLine<-sqlQuery(DB,"select * from [SF_2013_Day[i]_BaseLine]");
dim(SF_Day[i]_BaseLine)}
I got this error: Error: unexpected input in "for(i in c(3,6)){glm_d[i]_" . What can I do to resolve the problem?

You need to resolve i within the names first:
for (i in c(1,3,6,10,14,21,30)) {
set <- sqlQuery(DB, paste0("select * from [SF_2013_Day[", i, "]_BaseLine]"))
eval(parse(text = paste0("SF_FinalviewQ3_2013_Day", i, "_BaseLine <- set"))
dim(set)
}

Related

RODBC channel error in global environnement

I've set up a connection between R and SQL using the package RODBC and managed to connect and query the database from R.
I've created a small R function which objective is to delete some lines (as parameter) in a specific table.
Here it is (nameDB is the name of my database, and values_conversion another function I did to convert some data from R format to SQL format) :
delete_SQL = function(data, table){
ch = odbcConnect(nameDB,"postgres")
names = sqlColumns(channel=ch,table,schema="public",catalog = nameDB)$COLUMN_NAME
for(i in 1:nrow(data)){
sqlQuery(channel=ch,query=paste0("DELETE FROM public.\"",table,"\" WHERE ",
paste0(names," = ",values_conversion(data[i,]),collapse = " and "),";"),errors = TRUE)
}
odbcCloseAll()
}
Query exemple : "DELETE FROM public.\"lieu_protection\" WHERE lieu_id = 3 and protection_id = 1430;"
The code inside this function works fine when I execute everything directly, but when I call the function it throws an
Error in sqlQuery(channel = ch, query = paste0("DELETE FROM public.\"", :
first argument is not an open RODBC channel
I have a similar function that's getting and returning data from SQL and which is working fine, so I guess it has something to do with the delete, but the error is about the channel, so I'm quite confused.
Thanks to anyone that can help !

Need to Print Value of a Variable using Paste in R

I am trying to create a data frame of various error messages based on Data to be cross checked between two dataframes and storing the message in a vector in an iterative manner . I am using the following snippet for this purpose :
> for(j in 1:nrow(MySQL_Data)){ date_mysql=
> paste("MySQL_Data[",j,",1]") date_red= paste("RED_Data[",j,",1]")
> body= c() if(!date_mysql == date_red) {
> body<- append(body,paste("'There is data missing for date",date_mysql,"in",table2)) }else {
> NULL }}
My table2 variable prints as MYSQL_Data[2,1] instead of the actual value of the variable which is a date
Following is the Output :
"'There is data missing for date MySQL_Data[ 2 ,1] in Dream11_UserRegistration"
Can someone help me with the error that I am committing here..
Thanks in Advance !
Your use of paste in the definitions of data_mysql and data_red makes no sense. I’m assuming that what you actually want is this:
data_mysql = MySQL_Data[j, 1]
data_red = RED_Data[j, i]
Furthermore, you’re resetting body in every loop iteration so it will only ever hold a single element.

Function within function in R does not work

I have defined the following functions:
where_condition_SQL<-
function(table,selected_columns,where_conditions) {
new_table<-sqldf(sprintf("select %s from %s where %s",
as.character((selected_columns)),
deparse(substitute(table)),
as.character((where_conditions))))
return(new_table) }
select_category_amounts <- function(
input_table,selected_columns,category,category_column){
new_table<-where_condition_SQL(input_table,
selected_columns=as.character(selected_columns),
where_conditions=sprintf( "%s='%s'",
as.character(category_column),
as.character(category)) ) return(new_table) }
When I try to run the second function by:
select_category_amounts(second_table,"*","Reservas","categoria2")
then it does not recognize the second_table and give me the following error:
Error in rsqlite_send_query(conn#ptr, statement) :
no such table: input_table
I suppose that this is some issue about enviroments but I do not get the point. Thank you very much in advance for any help.
I would use eval instead of deparse in your first function, and pass the name of the table as a string. This way, you can get the content of the variable table in your first function:
where_condition_SQL<-
function(table,selected_columns,where_conditions) {
new_table<-sqldf(sprintf("select %s from %s where %s",
as.character((selected_columns)),
eval(table),
as.character((where_conditions))))
return(new_table) }
select_category_amounts("second_table","*","Reservas","categoria2")
Show Traceback
Rerun with Debug
Error in rsqlite_send_query(conn#ptr, statement) :
no such table: second_table
With eval, you can also pass the name of the table as an object:
second_table <- "mytable"
select_category_amounts(second_table,"*","Reservas","categoria2")
Error in rsqlite_send_query(conn#ptr, statement) : no such table: mytable

Unable to resolve an Argument is of length zero error error

I get an Argument is of length zero error when I run the below code
The code is from this blog -http://giventhedata.blogspot.in/2012/08/r-and-web-for-beginners-part-iii.html.
library(XML)
url<- "http://news.bbc.co.uk/2/hi/uk_politics/8044207.stm"
first<-"Abbott, Ms Diane"
url.tab <- readHTMLTable(url)
for (i in 1:length(url.tab)){
if (as.character(url.tab[[i]][1,1]) == first ) {print(first)}
}
I know that the url.tab[[5]][1,1]) does contain the string "Abbott, Ms Diane", and when I run IF statement in isolation replacing the i with 5, it runs fine. Any help would be appreciated. I also tried declaring i<-1 upfront. DInt change anything.
Some of your tables are in fact NULL.
So you have to test for is.null before trying to subset the table:
for (i in 1:length(url.tab)){
this.tab <- url.tab[[i]]
if(!is.null(this.tab)) if(as.character(this.tab[1,1]) == first ) {print(first)}
}
[1] "Abbott, Ms Diane"

db operation exception handling in R

I am using the following function to select values from a table.Table name is given as the parameter to that function.If the table does not exist an error generated and the execution is stoped.I want to do something if the table is not found.Is it possible in R? something like try-catch exception ?
library('RSQLite')
getData <- function(portfolio){
lite <- dbDriver("SQLite", max.con = 25)
db <- dbConnect(lite, dbname = "portfolioInfo.db")
res <- dbSendQuery(db, paste("SELECT * from ",portfolio," ",sep=""))
data <- fetch(res)
return (data)
}
getData("table1")
One way to do this would be to check the class of the data that is returned.
I am not familiar with RSQLite but I guess it will return a dataframe if the table is found and a text error when not?
So a possibility would be to check whether or not an error is raised:
checkQueryResult<-function(data){
if(class(data)=='data.frame') cat('SQL execution worked!')
else cat('Something went wrong, table does not exist?')
}
checkQueryResult(getData("table1"))
But maybe the RSQLite package already has error handling stuff built in?

Resources