Skip to content
Advertisement

Error in as.character(query) : cannot coerce type ‘closure’ to vector of type ‘character’

I was trying to load some data from the oracle database, During the proceeding every line of code was fine until I try to run the last line: df<- sqlQuery(sql_connection,sql_statement,stringsAsFactors=0), an error in the title occurred.

So here is the basic structure of my code:

since I’m new to R, I don’t feel I can tell where is the cause of the error. If someone can help me out with this I’d be much appreciated it.

Advertisement

Answer

In this line, you assign the base function sprintf to sql_statement:

You then pass it to sqlQuery() as the second argument (query):

The function expects query to be a character, so you get the error that R can’t coerce a ‘closure’ (essentially another name for a function) to a character.

I assume you were trying to imitate this commented-out line:

But it’s not necessary if all you want to do is run the query you just wrote. So if we just tidy up your code so sql_string is passed straight to sqlQuery(), it should work as intended, though I can’t actually test it because we don’t have access to your database:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement