Why can I not use dbWriteTable for SQL Server? - r

Does anyone know if something would have changed to break it? or am I missing something?
data <- data.frame(x = rnorm(10), y = rnorm(10))
cxn <- DBI::dbConnect(odbc::odbc(),
driver = 'ODBC Driver 17 for SQL Server',
server = dbServer,
database = dbName,
trusted_connection = 'yes',
port = port)
DBI::dbWriteTable(cxn, tblName, data, append = T)
I'm getting the following error:
Error in (function (classes, fdef, mtable) : unable to find an
inherited method for function ‘dbWriteTable’ for signature ‘"Microsoft
SQL Server", "character", "list"’
Here's my session Info:
R version 3.6.2 (2019-12-12)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] DBI_1.0.0 magrittr_1.5 lubridate_1.7.4 data.table_1.12.2
[5] sloop_1.0.1 forcats_0.4.0 stringr_1.4.0 dplyr_0.8.3
[9] purrr_0.3.2 readr_1.3.1 tidyr_0.8.3 tibble_2.1.3
[13] ggplot2_3.1.1 tidyverse_1.2.1 vctrs_0.2.0 devtools_2.0.2
[17] usethis_1.5.1 conflicted_1.0.4
loaded via a namespace (and not attached):
[1] Rcpp_1.0.1 lattice_0.20-38 prettyunits_1.0.2 ps_1.3.0
[5] assertthat_0.2.1 zeallot_0.1.0 rprojroot_1.3-2 digest_0.6.20
[9] R6_2.4.0 cellranger_1.1.0 plyr_1.8.4 odbc_1.1.6
[13] backports_1.1.4 httr_1.4.0 pillar_1.4.2 rlang_0.4.0
[17] lazyeval_0.2.2 readxl_1.3.1 rstudioapi_0.10 callr_3.2.0
[21] blob_1.1.1 desc_1.2.0 bit_1.1-14 munsell_0.5.0
[25] broom_0.5.2 compiler_3.6.2 modelr_0.1.4 pkgconfig_2.0.2
[29] pkgbuild_1.0.3 tidyselect_0.2.5 audio_0.1-6 crayon_1.3.4
[33] withr_2.1.2 grid_3.6.2 nlme_3.1-142 jsonlite_1.6
[37] gtable_0.3.0 scales_1.0.0 cli_1.1.0 stringi_1.4.3
[41] fs_1.3.1 remotes_2.0.4 testthat_2.1.1 xml2_1.2.0
[45] generics_0.0.2 tools_3.6.2 bit64_0.9-7 glue_1.3.1
[49] hms_0.4.2 processx_3.3.1 pkgload_1.0.2 colorspace_1.4-1
[53] sessioninfo_1.1.1 rvest_0.3.3 memoise_1.1.0 beepr_1.3
[57] haven_2.1.0

Recently I had similar issue.
Problem description: MS Server data base with scheme. The task is to save an R data.frame object to a predefined data base table without dropping it.
Problems I faced:
Some packages functions does not support schemes or require github development version installation
You can save data.frame only after drop (delete table) operation (I needed just "clear table" operation)
How I solved the issue
Using simple RODBC::sqlQuery, writing a data.frame row by row.
The solution (couple of functions) is available here or here

Related

Huxtable package for R: How to correctly reference huxtables in bookdown when outputting to Word

Using the example here, I am able to create a table & reference it using huxtable & bookdown as long as I output to pdf. The same does not seem to work with MS word.
Here is my sessionInfo in case it helps:
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] Rcpp_1.0.4.6 compiler_3.6.1 pillar_1.4.2 prettyunits_1.0.2
[5] tools_3.6.1 digest_0.6.25 packrat_0.5.0 pkgbuild_1.0.6
[9] lifecycle_0.2.0 evaluate_0.14 tibble_2.1.3 gtable_0.3.0
[13] pkgconfig_2.0.3 rlang_0.4.6 cli_2.0.2 rstudioapi_0.11
[17] parallel_3.6.1 yaml_2.2.0 xfun_0.10 loo_2.1.0
[21] gridExtra_2.3 dplyr_1.0.0 knitr_1.28 generics_0.0.2
[25] vctrs_0.3.1 tidyselect_1.1.0 stats4_3.6.1 grid_3.6.1
[29] glue_1.4.1 inline_0.3.15 R6_2.4.0 processx_3.4.1
[33] fansi_0.4.0 rmarkdown_2.3 bookdown_0.20 rstan_2.19.2
[37] purrr_0.3.3 callr_3.4.3 ggplot2_3.3.1 magrittr_1.5
[41] matrixStats_0.55.0 scales_1.0.0 ps_1.3.0 htmltools_0.4.0
[45] StanHeaders_2.19.0 assertthat_0.2.1 colorspace_1.4-1 munsell_0.5.0
[49] crayon_1.3.4
After additional searching, I found two solutions here and here. The easiest
solution is to use tab.cap as a chunk option:
```{r foo, echo=FALSE, tab.cap="Got it!"}
hux(a = 1:5, b = 1:5)
```

Shiny app not working when R & R packages gets updated

Please help.
I was able to run my shiny app when I had R version 3.5.1. The app was working fine. Now, as per the need of the server that we have , I needed to switch my local R version to 3.6.1 and had to reinstall all the packages.
Now, I am using bootsraplib library to get the app in Bootsrap4 version. But , now all those elements which were defined earlier in a conditional panel / they needed to be displayed under certain conditions , they are not getting displayed even when , in the server side the code that needs to run to get the condition to get those elements displayed gets executed correctly. but in UI side, those elements are not getting displayed now.
sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] reshape2_1.4.4 cowplot_1.0.0 bookdown_0.20.1 tinytex_0.24 shinysky_0.1.3 waiter_0.1.2
[7] rhandsontable_0.3.7 rio_0.5.16 shinyEffects_0.1.0 shinymaterial_1.1.0 plotly_4.9.2.1 expss_0.10.5
[13] scales_1.1.1 ggplot2_3.3.2 formattable_0.2.1 shinydashboard_0.7.1 dplyr_1.0.0 lubridate_1.7.9
[19] shinydashboardPlus_0.7.5 shinyWidgets_0.5.3 magrittr_1.5 shinyjs_1.1 shinyBS_0.61 devtools_2.3.0
[25] usethis_1.6.1 kableExtra_1.1.0 knitr_1.29 DT_0.14 shiny_1.5.0.9001
loaded via a namespace (and not attached):
[1] matrixStats_0.56.0 fs_1.4.2 webshot_0.5.2 httr_1.4.2 rprojroot_1.3-2 tools_3.6.1 backports_1.1.8
[8] R6_2.4.1 lazyeval_0.2.2 colorspace_1.4-1 withr_2.2.0 tidyselect_1.1.0 prettyunits_1.1.1 processx_3.4.3
[15] curl_4.3 compiler_3.6.1 cli_2.0.2 rvest_0.3.5 htmlTable_2.0.1 xml2_1.3.2 desc_1.2.0
[22] checkmate_2.0.0 readr_1.3.1 callr_3.4.3 stringr_1.4.0 digest_0.6.25 foreign_0.8-71 rmarkdown_2.3
[29] pkgconfig_2.0.3 htmltools_0.5.0 sessioninfo_1.1.1 fastmap_1.0.1 readxl_1.3.1 htmlwidgets_1.5.1.9001 rlang_0.4.7
[36] rstudioapi_0.11 generics_0.0.2 jsonlite_1.7.0 crosstalk_1.1.0.1 zip_2.0.4 Rcpp_1.0.5 munsell_0.5.0
[43] fansi_0.4.1 lifecycle_0.2.0 stringi_1.4.6 yaml_2.2.1 RJSONIO_1.3-1.4 plyr_1.8.6 pkgbuild_1.1.0
[50] grid_3.6.1 promises_1.1.1 forcats_0.5.0 crayon_1.3.4 haven_2.3.1 hms_0.5.3 ps_1.3.3
[57] pillar_1.4.6 pkgload_1.1.0 glue_1.4.1 evaluate_0.14 data.table_1.12.8 remotes_2.2.0 vctrs_0.3.2
[64] httpuv_1.5.4 cellranger_1.1.0 testthat_2.3.2 gtable_0.3.0 purrr_0.3.4 tidyr_1.1.0 assertthat_0.2.1
[71] xfun_0.15 openxlsx_4.1.5 mime_0.9 xtable_1.8-4 later_1.1.0.1 viridisLite_0.3.0 tibble_3.0.3
[78] memoise_1.1.0 ellipsis_0.3.1
What version of shiny you were using earlier?
Try this - options(shiny.jquery.version = 1)

How to run an lapply in parallel in R?

So I have tried a few different ways of doing this but each returns an a different error which is making me question if i'm even doing it correctly.
So without any parallel components, we have the following:
all_necks <- lapply(b_list, b_fun)
This works perfectly; b_list is a dataframe and b_fun is a ton of joins and functions which are to be done on the list.
Because each run takes about 5 minutes and there are 550 elements in b_list, I need this to be faster to be practical.
I try future.lapply but get the following error:
library(future.apply)
options(future.globals.maxSize= 178258920000)
plan(multiprocess, workers = 5) ## Parallelize using five cores
all_necks <- future_lapply(b_list, b_fun)
ERROR:
Error in serialize(data, node$con) : error writing to connection
Then I tried foreach and got the following:
library(doParallel)
cl <- makeCluster(detectCores())
registerDoParallel(cl)
all_necks <- foreach(i = 1:b_list %dopar% {b_fun})
ERROR:
There were 16 warnings (use warnings() to see them)
1: In environment() : closing unused connection 19 (<-DESKTOP-XXX)
2: In environment() : closing unused connection 18 (<-DESKTOP-XXX)
...
I must be doing this incorrectly but I really just want this long lapply to run faster via parallel processing.
I would prefer to do this on 5 cores.
EDIT: Session Info Added
R version 4.0.1 (2020-06-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] future.apply_1.5.0 future_1.17.0 formattable_0.2.0.1 lubridate_1.7.9 data.table_1.12.8 chron_2.3-55
[7] Nmisc_0.3.5 anytime_0.3.7 forcats_0.5.0 stringr_1.4.0 dplyr_1.0.0 purrr_0.3.4
[13] readr_1.3.1 tidyr_1.1.0 tibble_3.0.1 ggplot2_3.3.2 tidyverse_1.3.0 jsonlite_1.6.1
loaded via a namespace (and not attached):
[1] Rcpp_1.0.4.6 lattice_0.20-41 listenv_0.8.0 assertthat_0.2.1 digest_0.6.25 R6_2.4.1
[7] cellranger_1.1.0 backports_1.1.7 reprex_0.3.0 evaluate_0.14 httr_1.4.1 pillar_1.4.4
[13] rlang_0.4.6 readxl_1.3.1 rstudioapi_0.11 furrr_0.1.0 blob_1.2.1 rmarkdown_2.3
[19] htmlwidgets_1.5.1 munsell_0.5.0 tinytex_0.24 broom_0.5.6 compiler_4.0.1 modelr_0.1.8
[25] xfun_0.14 pkgconfig_2.0.3 globals_0.12.5 htmltools_0.5.0 tidyselect_1.1.0 codetools_0.2-16
[31] fansi_0.4.1 crayon_1.3.4 dbplyr_1.4.4 withr_2.2.0 rappdirs_0.3.1 grid_4.0.1
[37] nlme_3.1-148 gtable_0.3.0 lifecycle_0.2.0 DBI_1.1.0 magrittr_1.5 scales_1.1.1
[43] cli_2.0.2 stringi_1.4.6 fs_1.4.1 xml2_1.3.2 ellipsis_0.3.1 generics_0.0.2
[49] vctrs_0.3.1 tools_4.0.1 glue_1.4.1 hms_0.5.3 parallel_4.0.1 colorspace_1.4-1
[55] rvest_0.3.5 knitr_1.28 haven_2.3.1

Why doesn't 'Tidy' in Broom work as intended for an epi.2by2 object?

I am using the package epiR to calculate measures of association (rate ratio and attributable risk). Results are outputted to a list. I want to get these estimates into one overall dataframe - I thought Broom could do this - see here.
This is what I have attached:
R version 3.5.3 (2019-03-11)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] forcats_0.4.0 stringr_1.4.0 purrr_0.3.2 readr_1.3.1 tidyr_0.8.3 tibble_3.0.1 ggplot2_3.1.1
[8] tidyverse_1.2.1 dplyr_0.8.1 broom_0.5.6
loaded via a namespace (and not attached):
[1] Rcpp_1.0.1 cellranger_1.1.0 pillar_1.4.4 compiler_3.5.3 plyr_1.8.4 tools_3.5.3 jsonlite_1.6
[8] lubridate_1.7.4 lifecycle_0.2.0 nlme_3.1-137 gtable_0.3.0 lattice_0.20-38 pkgconfig_2.0.2 rlang_0.4.5
[15] Matrix_1.2-17 cli_1.1.0 rstudioapi_0.10 haven_2.1.0 withr_2.1.2 xml2_1.2.0 httr_1.4.0
[22] generics_0.0.2 vctrs_0.2.4 hms_0.4.2 grid_3.5.3 tidyselect_0.2.5 glue_1.3.1 R6_2.4.0
[29] readxl_1.3.1 modelr_0.1.4 magrittr_1.5 backports_1.1.4 scales_1.0.0 ellipsis_0.3.0 rvest_0.3.4
[36] assertthat_0.2.1 colorspace_1.4-1 stringi_1.4.3 lazyeval_0.2.2 munsell_0.5.0 crayon_1.3.4
>
This is the code I have run:
tabh7 <- table(ispa.h7$mat2,ispa.h7$cov,ispa.h7$Sex)
rh7 <- epi.2by2(dat = tabh7, method = "cohort.count",
conf.level = 0.95, units = 100, outcome = "as.columns")
tidy(rh7, parameters = "moa")
And this is the message:
Error: No tidy method for objects of class epi.2by2
I have tried deleting my .RHistory. I never save my workspaces. What could be the problem?

How to write an R dataframe to an Access .mdb if supports.transactions=FALSE

I would like to save an R dataframe as a new table in an existing .mdb available here. I am getting an error message (below) and I suspect it has to do with .. ..$ supports.transactions: logi FALSE. I know I can save the dataframe as a .csv and import it into Access by hand. However, I would like my code to work for people who do not have Access software on their computer.
My connection is working and I'm able to dbReadTable from the .mdb. The .mdb I'm working with is I've tried
DBI::dbWriteTable(con,
"SSURGO_Soils_new",
value=newsoilexp)
as explained here, which results in
Error in new_result(connection#ptr, statement) :
nanodbc/nanodbc.cpp:1344: 42000: [Microsoft][ODBC Microsoft Access Driver] Syntax error in CREATE TABLE statement.
I've tried
DBI::dbWriteTable(con,
SQL("SSURGO_Soils_new"),
value=newsoilexp)
as suggested here, which results in
Error in connection_sql_tables(conn#ptr, catalog_name = if ("catalog" %in% :
nanodbc/nanodbc.cpp:4266: HYC00: [Microsoft][ODBC Microsoft Access Driver]Optional feature not implemented
Here is the DBI::dbGetInfo(con) output:
$dbname
[1] "C:\\SWAT\\ARCSWAT\\DATABASES\\SWAT_US_SSURGO_Soils_20190419.mdb"
$dbms.name
[1] "ACCESS"
$db.version
[1] "04.00.0000"
$username
[1] "admin"
$host
[1] ""
$port
[1] ""
$sourcename
[1] "mySSURGO"
$servername
[1] "ACCESS"
$drivername
[1] "ACEODBC.DLL"
$odbc.version
[1] "03.80.0000"
$driver.version
[1] "Microsoft Access database engine"
$odbcdriver.version
[1] "03.51"
$supports.transactions
[1] FALSE
attr(,"class")
[1] "ACCESS" "driver_info" "list"
And my sessionInfo()
R version 3.5.3 (2019-03-11)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] dbplyr_1.4.2 DBI_1.0.0 odbc_1.1.6 car_3.0-3 carData_3.0-2 dplyr_0.8.3
[7] ggsci_2.9 cowplot_1.0.0 ggpubr_0.2.2.999 magrittr_1.5 ggthemes_4.2.0 ggExtra_0.8
[13] devtools_2.1.0 usethis_1.5.1 ggplot2_3.1.0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.2 prettyunits_1.0.2 ps_1.2.1 zeallot_0.1.0 assertthat_0.2.0
[6] rprojroot_1.3-2 digest_0.6.18 mime_0.6 cellranger_1.1.0 R6_2.3.0
[11] plyr_1.8.4 backports_1.1.4 httr_1.4.0 pillar_1.3.1 rlang_0.4.0
[16] readxl_1.3.1 lazyeval_0.2.1 curl_3.2 rstudioapi_0.8 data.table_1.12.2
[21] miniUI_0.1.1.1 blob_1.2.0 callr_3.3.1 desc_1.2.0 labeling_0.3
[26] stringr_1.3.1 foreign_0.8-72 bit_1.1-14 munsell_0.5.0 shiny_1.2.0
[31] compiler_3.5.3 httpuv_1.4.5 pkgconfig_2.0.2 pkgbuild_1.0.4 htmltools_0.3.6
[36] tidyselect_0.2.5 tibble_2.1.3 rio_0.5.16 crayon_1.3.4 withr_2.1.2
[41] later_0.7.5 grid_3.5.3 xtable_1.8-3 gtable_0.2.0 scales_1.0.0
[46] zip_2.0.3 cli_1.1.0 stringi_1.2.4 ggsignif_0.6.0 fs_1.3.1
[51] promises_1.0.1 remotes_2.1.0 testthat_2.2.1 vctrs_0.2.0 openxlsx_4.1.0.1
[56] tools_3.5.3 forcats_0.3.0 bit64_0.9-7 glue_1.3.0 purrr_0.2.5
[61] hms_0.4.2 abind_1.4-5 processx_3.4.1 pkgload_1.0.2 yaml_2.2.0
[66] colorspace_1.3-2 sessioninfo_1.1.1 memoise_1.1.0 haven_2.1.0
Will dbWriteTable() work properly if supports.transactions: TRUE? If so, how do I change it to TRUE? Or maybe something else is the problem. Neither the DBI or ODBC package manuals mention supports.transactions, so I suspect my problem is rather basic. Thanks in advance.
EDIT: This post does not address my question. That post uses the older pkg RODBC and deals with appending existing tables, not writing new ones.

Resources