What I'm trying to do:
- I have a long script I wrote in RStudio which links several SQL databases to pull information and ends up putting it all in to a table in one of these databases.
- I want to make this automated, so I have a computer that is on all day everyday that I have created a task scheduler daily task for to automatically open R.exe and run this script.
- The script works perfectly in RStudio, but has errors when run in R.exe.
- The location I am using to automatically run the script via R.exe is C:\Program Files\R\R-4.0.4\bin\x64\R.exe
- Here is a list of packages and initial setup I am using:
rm(list = ls())
install.packages("RDCOMClient", repos = "http://www.omegahat.net/R")
install.packages('xlsx', repos = "https://mirror.las.iastate.edu/CRAN/")
install.packages('RODBC', repos = "https://mirror.las.iastate.edu/CRAN/")
install.packages('dplyr', repos = "https://mirror.las.iastate.edu/CRAN/")
install.packages('devtools', repos = "https://mirror.las.iastate.edu/CRAN/")
install.packages('usethis', repos = "https://mirror.las.iastate.edu/CRAN/")
library(xlsx)
library(RODBC)
library(RDCOMClient)
library(dplyr)
library(devtools)
connClarity <-odbcConnect("Clarity")
connMCIF <-odbcConnect("MCIF")
Following this is a long code that I'll only post if needed, but the code works just fine in RStudio.
Here is the first and main error that I'm getting:
SaveToMCIF <- sqlSave(connMCIF, CovidTestOrders, tablename = "Import_tblCOVI$
Error in sqlSave(connMCIF, CovidTestOrders, tablename = "Import_tblCOVID19_Events_Information", :
should be a data frame
For some reason, it thinks the dataframe CovidTestOrders isn't a dataframe. I used is.data.frame to check, and in RStudio it says TRUE, but in R.exe it says FALSE.
Here is the start of the code used to make CovidTestOrders:
CovidTestOrders <- sqlQuery(connClarity,"
WITH COVIDORDER AS (
SELECT CASE
WHEN CLOC1.LOC_NAME IS NOT NULL AND CBED.BED_LABEL IS NULL THEN ' '
WHEN CLOC1.LOC_NAME IS NULL THEN ' '
ELSE CLOC1.LOC_NAME
END AS HOSPITAL
,CASE
WHEN CDEP1.DEPARTMENT_NAME IS NULL THEN ' '
ELSE CDEP1.DEPARTMENT_NAME
I tried forcing CovidTestOrders to be a dataframe by starting with data.frame(), which indeed made it a dataframe however it put all 28 columns in to 1 column. The version of R.exe I'm using is 4.0.4, but I have older versions that I've tried as well (also tried bin i386 vs x64) and they either didn't work or had different errors.