Include query with R package - r

I have a SQL query that I would like to ship with an R package I have built, but when I try to include it in the inst or extdata or data folders inside my R package I don't know how to get the function to reference it. An example might be this: query file is myQuery.sql
runDbQuery = function(){
queryfile = 'folder/myQuery.sql'
query = readChar(queryfile, file.info(queryfile)$size)
require(RODBC)
channel <- odbcConnect("mydb", uid = "uid",
pwd = "pwd")
dbResults = sqlQuery(channel = channel, query = query, as.is = T)
close(channel)
return(dbResults)
}

I put .sql files I use in packages in /inst/sql and then get the path to them in functions via:
system.file("sql/myquery.sql",package = "mypackage")

Related

How to reference Rmd files which should be rendered (within a package function)

I write a package, which will be used to create automated reports.
There is one function createPdfReport which basically looks as follows (I use RStudio):
createPdfReport <- function(dataset, save_path) {
rmdName <- strsplit(x = basename(dataset, split = ".", fixed = TRUE)[[1]][1]
# some code here which uses "dataset"
relPath <- dirname(rstudioapi::getSourceEditorContext()$path)
rmarkdown::render(input = paste0(relPath, "/myRMDfile.Rmd"),
output_dir = save_path,
output_file = paste0(rmdName , ".html"),
encoding = "UTF-8", quiet = TRUE)
}
Most likely, R will finally run on a server and it is not clear, which operating system or editor is used there.
Therefore, I would like to get rid of rstudioapi::getSourceEditorContext().
But how? I could not find anything.
createPdfReport is part of a typical package with the following structure:
DESCRIPTION
NAMESPACE
/man
/R
createPdfReport.R --> Contains the function createPdfReport() above
myRMDfile.Rmd
/tests
You could store myRMDfile.Rmd in inst/extdata, see package raw data.
This allows to get the file path and use it after package installation with:
system.file("extdata", "myRMDfile.Rmd", package = "myPackage")

R Package passing function through to new function

I'm creating a new package as a learning exercise. I've selected a few functions that serve the purpose I need and want to bundle them together into a single, new, package that I can then apply gWidgets to to make them GUI driven.
Finding documentation on how to pass existing functions has been pretty sparse and I am a novice at this. Any assistance would be appreciated.
I've added the necessary imports in my Description using usethis:use_package() updated the NAMESPACE (using Roxygen)
and created the .R files using a Stackoverflow as a framework. a sample .R looks like this:
#'ODBC list drivers
#'
#'#export
odbcListDrivers <- function() {
odbc::odbcListDrivers()
}
This Works.
But when it comes to the point I need to pass information with a more advanced function:
#'
#' u/export
#'
DBconnect <- function() {
DBI::dbConnect()
}
I get an unused argument error when I try to run the test code.
con <- DBconnect(RMySQL::MySQL(),
+ dbname = "sakila",
+ host = "192.168.50.71",
+ port = 49153,
+ user = "root",
+ password = "Camplovers01")
Error in DBconnect(RMySQL::MySQL(), dbname = "sakila", host = "192.168.50.71", :
unused arguments (dbname = "sakila", host = "192.168.50.71", port = 49153, user = "root", password = "Camplovers01")
The question then is when I pass a function like above how can I make sure I pass the correct arguments? Function I am trying to pass
Ok, asked and then answered by myself. The answer is in the error, unused arguments. I need to place the arguments from the source function in the function I create. These will pass through to the original function and return the correct response. The odbcListDrivers worked as it had no arguments to pass or expect.
Example
Old .R file
#'
#' u/export
#'
DBconnect <- function() {
DBI::dbConnect()
}
This of course fails as was the reason for my question.
New .R file
#'
#' #export
#'
DBconnect <- function(dsn = NULL,
...,
timezone = "UTC",
timezone_out = "UTC",
encoding = "",
bigint = c("integer64", "integer", "numeric", "character"),
timeout = Inf,
driver = NULL,
server = NULL,
database = NULL,
uid = NULL,
pwd = NULL,
dbms.name = NULL,
.connection_string = NULL) {
DBI::dbConnect()
}
Finding the arguments of the function was a simple matter of reviewing the .R file in the GitHub repo for the Package in question.

Decompress .gz file from Azure blob in r

I want to read a .csv.gz from a Azure blob container, but I am struggling with the .gz format. When I download the file local and then read it in R with readr it works fine. But when I try to read it from Azure the file isn't read propperly. It seems that the file is not decompressed. This is the code I used to read the file local (also read_csv2 works fine):
df<-read_delim("filename.csv.gz", delim=";",col_names=c('epoch','SegmentID','TT','Speed','LoS','Coverage'),
col_types=cols(epoch = col_integer(),SegmentID = col_integer(),TT = col_integer(),Speed = col_integer(),LoS = col_integer(),Coverage = col_integer()))
And this is what I try to do to reach the file from Azure:
blob_urls_with_sas<-paste("https://name.blob.core.windows.net","/container/filename.csv.gz",
sas_token, sep="")
dfAzure<-read_delim(blob_urls_with_sas,delim=";",
col_names=c('epoch','SegmentID','TT','Speed','LoS','Coverage'),
col_types=cols(epoch = col_integer(),SegmentID = col_integer(),TT = col_integer(),
Speed = col_integer(),LoS = col_integer(),Coverage =col_integer()))
or from the AzureStor package
test <- storage_read_delim(cont, "filename.csv.gz",delim=";",
col_names=c('epoch','SegmentID','TT','Speed','LoS','Coverage'), col_types=cols(epoch = col_integer(),SegmentID = col_integer(),TT = col_integer(),
Speed = col_integer(),LoS = col_integer(),Coverage = col_integer()))
One option would be to use fread() from data.table which naturally handles .gz files. Make sure you install R.utils first.

RSQLite only pretends to write to a database table with a bracketed name

I want to write data to a table called [foo/bar] inside of a SQLite database created through RSQLite. I wrote the following function to save answers to disk.
save_to_disk = function(data) {
con = dbConnect(SQLite(), "responses.db")
path = "[foo/bar]"
message("Writing the data now!")
message("The database file size was: ", file.size(response_db))
dbWriteTable(con, path, data, append = TRUE)
message("Table append complete!")
message("The database file size is now: ", file.size(response_db))
dbDisconnect(con)
}
However, when I try to pass this function data I see:
Writing the data now!
The database file size was: 8192
Table append complete!
The database file size is now: 8192
If I change the table name to dummy and repeat the process, then instead I see:
Writing the data now!
The database file size was: 8192
Table append complete!
The database file size is now: 12288
It seems as though RSQLite doesn't like my table name for some reason. My understanding was that this was a valid table name since it was wrapped in [...]. In fact, outside of that function I am perfectly able to write to such a table. In other words, this works from the REPL:
test = data.frame(submitted = as.integer(Sys.time()),
respondent = "Alice",
subject = "Alice",
question = "name",
part = "first",
order = 1L,
answer = "Alice")
dbWriteTable(con, "[foo/bar]", test, append = TRUE)
After that I can use dbReadTable to see what I had just entered. Why doesn't it work the same way in a function?

What is the standard way to set default paths for function arguments in R packages?

Setting default values for R functions is straightforward, e.g.
myfunction = function(x, k=42, c=1){
result = x*x + k - c
return(result)
}
Here, by default k=42, c=1, and x is a required argument.
I'm creating an R package whereby I would like the arguments to be default files. (In this case, these could either be variables loaded via .rda files, or actual text or csv files.)
To provide the path to a file in inst/extdata, the documentation recommends using the following:
http://r-pkgs.had.co.nz/inst.html
For example, to find inst/extdata/mydata.csv, you’d call
system.file("extdata", "mydata.csv", package = "mypackage")
What is the recommended way to create function arguments which default to a certain file?
I think directly linking to the files would be not the best approach, e.g.
do_something_with_data = function(file=system.file("extdata", "mydata.csv", package = "mypackage")){
data.table::fread(file)
...
}
Another approach could be to set all such arguments to NULL, and then use the default arguments if nothing else is used:
do_something_with_data2 = function(file=NULL){
if (is.null(file)){
file = system.file("extdata", "mydata.csv", package = "mypackage")
}
...
}

Resources