Hi,
I am implementing the db_query_fields method in dbplyr package in rstudio in the function below to delete a selected row from MySQL database in a shiny web table:
row <- if (input$row %>% db_query_fields(pool,sql,input$responses))
I however, get the error message:
Warning: Error in UseMethod: no applicable method for 'db_query_fields' applied to an object of class "NULL"
81: db_query_fields
80: function_list[[k]]
78: freduce
77: _fseq
76: eval
75: eval
73: %>%
I have included the reproducible example below:
library(shiny)
library(session)
library(pool)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(dbplyr)
#>
#> Attaching package: 'dbplyr'
#> The following objects are masked from 'package:dplyr':
#>
#> ident, sql
library(RMySQL)
#> Loading required package: DBI
#### When delete button is clicked####
observeEvent(input$deleterow, {
pool = dbPool(RMySQL::MySQL(),user='root',password='',dbname='Rueben',host='localhost')
conn <- poolCheckout(pool)
#input$responses - refers to the shiny table
row <- if (input$row %>% db_query_fields(pool,sql,input$responses)) {
input$row
} else {
showModal(modalDialog(
title = "Invalid Row name",
"The selected row must be a row of the DB table",
easyClose = TRUE, footer = NULL
))
return()
}
df <- as_data_frame(pool %>% tbl(input$responses) %>% select(row))
allUniqueVals <- unique(df[[row]])
results <- lapply(as_list(input$vals), `%in%`, allUniqueVals)
vals <- if (all(results)) {
if (is(df[["description"]], "string")) input$vals
else lapply(input$vals, sql_escape_string, con = pool)
} else {
showModal(modalDialog(
title = "Invalid Row values",
"The selected values do not exist in the selected table row",
easyClose = TRUE, footer = NULL
))
return()
}
sql <- paste0("DELETE FROM ?tasks WHERE ", row, " IN (",
paste0(vals, collapse = ", "), ");")
query <- sqlInterpolate(pool, sql, table = input$responses)
dbExecute(pool, query)
#session$sendCustomMessage("messageBox", paste("Delete process completed" ))
})
Created on 2019-06-20 by the reprex package (v0.3.0)
Session info
sessionInfo()
#> R version 3.6.0 (2019-04-26)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 15063)
#>
#> Matrix products: default
#>
#> locale:
#> [1] LC_COLLATE=Swedish_Sweden.1252 LC_CTYPE=Swedish_Sweden.1252
#> [3] LC_MONETARY=Swedish_Sweden.1252 LC_NUMERIC=C
#> [5] LC_TIME=Swedish_Sweden.1252
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] RMySQL_0.10.17 DBI_1.0.0 dbplyr_1.4.2 dplyr_0.8.1
#> [5] pool_0.1.4.2 session_1.0.3 shiny_1.3.2
#>
#> loaded via a namespace (and not attached):
#> [1] Rcpp_1.0.1 knitr_1.23 magrittr_1.5 tidyselect_0.2.5
#> [5] xtable_1.8-4 R6_2.4.0 rlang_0.3.4 stringr_1.4.0
#> [9] highr_0.8 tools_3.6.0 xfun_0.7 htmltools_0.3.6
#> [13] yaml_2.2.0 digest_0.6.19 assertthat_0.2.1 tibble_2.1.3
#> [17] crayon_1.3.4 purrr_0.3.2 later_0.8.0 promises_1.0.1
#> [21] glue_1.3.1 evaluate_0.14 mime_0.7 rmarkdown_1.13
#> [25] stringi_1.4.3 pillar_1.4.1 compiler_3.6.0 httpuv_1.5.1
#> [29] pkgconfig_2.0.2
Please, has anybody got the solution to this problem? I have attached a picture from my code in rstudio.
Thanks.