I am trying to find a way to use R packages like dplyr, summarytools in R, but my data is the ODBC format. I am trying to convert the ODBC into dataframes, however facing some issues.
x <- as.data.frame(res)
Error in as.data.frame.default(res):
Cannot coerce class 'structure ("OdbCResult", package = "odbc")' to a data.frame
It is not clear from your post what the exact issues is, but you can start by reading these tips on using ODBC driver within R https://db.rstudio.com/r-packages/odbc/
If still stuck, please create a reprex. A reprex consists of the minimal code and data needed to recreate the issue/question you're having. You can find instructions how to build and share one here:
Hi, the reason is that the res variable contains not the results of the query, but a SQL statement. In order to pull down the actual data from the database, you would have to use the collect() function, that will convert the data transmitted by the DB to R into a tibble. So, you can try: res %>% collect(). Please keep in mind that that operation will bring back everything, so if the result of your query contains 1,000's of rows, that is what you will get. If you have exploring at this time, you can consider using res %>% head(100) %>% collect(), that will bring back first 100 results.
I understand that res is nothing but obtained from an SQL output from an ODBC database as follows:
res <- dbSendQuery(con, "select * from X.Y.Z")
dbFetch(res)
Is there a way to obtain a data frame from the odbc database X.Y.Z directly ?
Thank you for your suggestion to use res %>% head(100) %>% collect(), however it is giving me the following broad error:
"Error in x[seq_len(n)] : object of type 'S4' is not subsettable"
+1 to @mikecrobp 's response, for that you would use dbGetQuery() . In dbplyr, you would use tbl(), as in tbl(con, "xyz"), and that would become your pointer to that table that then you can use to pass more dplyr commands: tbl(con, "xyz") %>% group_by(field1) %>% summarise(x = sum(field2))