Skip to content

Commit

Permalink
showLog() no longer needs to be specified in useShinyjs(), and it…
Browse files Browse the repository at this point in the history
… can be used by just calling it in the server code once (fixes #105)
  • Loading branch information
daattali committed Dec 11, 2016
1 parent e91a1fb commit 2969dc7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# shinyjs 0.8.xxxx

- added support for shinyAce editor as the input for `runcodeUI()` (#93)
- `showLog()` no longer needs to be specified in `useShinyjs()`, and it can be used by just calling it in the server code once (#105)
- fixed bug where `showLog()` would only show the last message if multiple messages were printed in succession (#99)
- fixed bug where date inputs could not be reset to empty (#100)
- fixed textArea inputs not getting disabled when disabling a parent element
Expand Down
19 changes: 18 additions & 1 deletion R/showLog.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#'
#' shinyApp(
#' ui = fluidPage(
#' useShinyjs(showLog = TRUE), # Set up shinyjs with showLog
#' useShinyjs(),
#' textInput("text", "Type something")
#' ),
#' server = function(input, output) {
Expand All @@ -33,6 +33,23 @@
#' @export
showLog <- function() {
session <- getSession()

# Capture the console.log function and overwrite it to send the message to R
shiny::insertUI("head", "beforeEnd", {
shiny::singleton(shiny::tags$head(shiny::tags$script(
'(function(){
var oldLog = console.log;
var queue = new ShinySenderQueue();
console.log = function (message) {
try {
queue.send("shinyjs-showLog", message);
} catch(err) {}
oldLog.apply(console, arguments);
};
})();'
)))
})

shiny::observeEvent(session$input[['shinyjs-showLog']], {
message("JAVASCRIPT LOG: ",
jsonlite::toJSON(session$input[['shinyjs-showLog']],
Expand Down
29 changes: 7 additions & 22 deletions R/useShinyjs.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
#' using this option, view the
#' \href{https://github.com/daattali/shinyjs}{README} online to learn
#' how to use shinyjs in these apps.
#' @param showLog Set this to \code{TRUE} if you want to print all JavaScript
#' log messages in the R console. This is useful for debugging. If using this
#' option, you must also call the \code{\link[shinyjs]{showLog}} function in
#' the server.
#' @param showLog Deprecated.
#' @return Scripts that \code{shinyjs} requires that are automatically inserted
#' to the app's \code{<head>} tag.
#' @examples
Expand All @@ -44,11 +41,15 @@
#' \code{\link[shinyjs]{extendShinyjs}}
#' @export
useShinyjs <- function(rmd = FALSE, debug = FALSE, html = FALSE,
showLog = FALSE) {
showLog = NULL) {
stopifnot(rmd == TRUE || rmd == FALSE)
stopifnot(debug == TRUE || debug == FALSE)
stopifnot(html == TRUE || html == FALSE)
stopifnot(showLog == TRUE || showLog == FALSE)

if (!missing(showLog)) {
warning("'showLog' has been deprecated. You do not need to call it anymore.",
call. = FALSE)
}

# `astext` is FALSE in normal shiny apps where the shinyjs content is returned
# as a shiny tag that gets rendered by the Shiny UI, and TRUE in interactive
Expand Down Expand Up @@ -78,22 +79,6 @@ useShinyjs <- function(rmd = FALSE, debug = FALSE, html = FALSE,
initJS <- "shinyjs.debug = false;"
}

# Capture the console.log function and overwrite it to send the message to R
if (showLog) {
initJS <- paste0(
initJS,
'(function(){
var oldLog = console.log;
var queue = new ShinySenderQueue();
console.log = function (message) {
try {
queue.send("shinyjs-showLog", message);
} catch(err) {}
oldLog.apply(console, arguments);
};
})();')
}

# include CSS for hiding elements
initCSS <- inlineCSS(".shinyjs-hide { display: none !important; }")

Expand Down
2 changes: 1 addition & 1 deletion man/showLog.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions man/useShinyjs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2969dc7

Please sign in to comment.