Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
- The `sql` engine now produces an output for update-like SQL queries, indicating the number of rows affected (as returned by DBI::dbExecute). So far, `knitr` produces a nicely formatted output for queries returning a result set (typically SELECT...), but gives no feedback at all for queries making changes to the DB (e.g. INSERT|UPDATE|DELETE|CREATE|DROP; see not exported function knitr:::is_sql_update_query). For such queries, `knitr` now shows the number of affected rows. You can also set the chunk option `output.var` to assign the number of affected rows to a variable.



# CHANGES IN knitr VERSION 1.35

## BUG FIXES
Expand Down
9 changes: 7 additions & 2 deletions R/engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,8 @@ eng_sql = function(options) {

data = tryCatch({
if (is_sql_update_query(query)) {
DBI::dbExecute(conn, query)
NULL
data = DBI::dbExecute(conn, query)
data
} else if (is.null(varname) && max.print > 0) {
# execute query -- when we are printing with an enforced max.print we
# use dbFetch so as to only pull down the required number of records
Expand Down Expand Up @@ -633,6 +633,11 @@ eng_sql = function(options) {

} else print(display_data) # fallback to standard print
})
if (is.numeric(data) && length(data) == 1 && is.null(varname)) {
options$results = 'asis'
# format = "fg" instead of "d". Row counts on DB may be greater than integer max value
output = paste0("Number of affected rows: ", formatC(data, format = "fg", big.mark = ','))
}
if (options$results == 'hide') output = NULL

# assign varname if requested
Expand Down