@@ -59,6 +59,43 @@ disable_errorist = function() {
5959}
6060
6161
62+ warning_msg_format = function (x = warnings()) {
63+ # Retrieve the error message
64+ warning_messages = names(x )
65+ # Retrieve the call location for the error
66+ # E.g. fun(1, 2) : this is a warning
67+ warning_calls = as.character(x )
68+
69+ # Set default sep value
70+ sep_val = " : "
71+
72+ # Build a traditional warning string
73+ combined_warning = paste(warning_calls , warning_messages , sep = sep_val )
74+
75+ # Format string by removing instances of NULL
76+ missing_call = which(warning_calls == " NULL" )
77+ if (length(missing_call )) {
78+ # Compute number of characters to drop
79+ call_drop = nchar(paste0(" NULL" , sep_val )) + 1L
80+
81+ # Remove NULL in string.
82+ combined_warning [missing_call ] =
83+ substr(combined_warning [missing_call ],
84+ call_drop ,
85+ nchar(combined_warning [missing_call ]))
86+ }
87+
88+ # Determine number of warnings
89+ n = length(combined_warning )
90+
91+ # Ensure a localized warning is given.
92+ translated_warning_prefix = ngettext(n , " Warning message:\n " , " Warning messages:\n " )
93+
94+ # Return fixed warning string
95+ paste0(translated_warning_prefix , combined_warning )
96+ }
97+
98+
6299warning_handler = function (search_func =
63100 getOption(" errorist.warning" , searcher :: search_google )) {
64101 # Write trigger as a closure with the required four inputs
@@ -74,22 +111,23 @@ warning_handler = function(search_func =
74111 # see https://developer.r-project.org/TaskHandlers.pdf
75112 function (expr , value , ok , visible ) {
76113 # Determines if a warning was triggered
77- last_warning_value = get0( " last.warning " , envir = baseenv() )
114+ last_warning_value = warnings( )
78115
79116 if (! is.null(last_warning_value )) {
80- warning_contents = names (last_warning_value )[ 1 ]
117+ warning_contents = warning_msg_format (last_warning_value )
81118 } else {
82- warning_contents = NA
119+ warning_contents = NA
83120 }
84121
85122 # Trigger search with added protections since this caller is repeatedly
86123 # called regardless if a new warning is detected.
87124 if (! is.na(warning_contents ) &&
88- (
89- is.na(.errorist_env $ captured_last_search_warning ) ||
90- .errorist_env $ captured_last_search_warning != warning_contents
91- )) {
92- search_func(warning_contents )
125+ (is.na(.errorist_env $ captured_last_search_warning ) ||
126+ .errorist_env $ captured_last_search_warning != warning_contents )
127+ ) {
128+ for (warning_msg in warning_contents ) {
129+ search_func(warning_msg )
130+ }
93131 }
94132
95133 # Update last warning
0 commit comments