Skip to content

Commit

Permalink
add showLog() function to print javascript log messages in R console;
Browse files Browse the repository at this point in the history
fixes #88
  • Loading branch information
daattali committed Oct 15, 2016
1 parent bcc9f38 commit f23c089
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 30 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: shinyjs
Title: Easily Improve the User Experience of Your Shiny Apps in Seconds
Version: 0.7.9004
Version: 0.7.9005
Authors@R: person("Dean", "Attali", email = "[email protected]",
role = c("aut", "cre"))
Description: Perform common useful JavaScript operations in Shiny apps that will
Expand All @@ -16,6 +16,7 @@ Depends:
Imports:
digest (>= 0.6.8),
htmltools (>= 0.2.6),
jsonlite,
miniUI (>= 0.1.1),
shiny (>= 0.11.1),
stats
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export(runcodeServer)
export(runcodeUI)
export(runjs)
export(show)
export(showLog)
export(toggle)
export(toggleClass)
export(toggleState)
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# shinyjs 0.7.xxxx

- added `runcodeUI()` and `runcodeServer()` functions that you can add to your app in order to run arbitrary R code interactively
- add `alert()` as an alias for `info()`
- added `showLog()` function which lets you redirect all JavaScript logging statements to the R console, to make it easier and quicker to debug apps without having to open the JS console
- added `alert()` as an alias for `info()`

# shinyjs 0.7

Expand Down
8 changes: 5 additions & 3 deletions R/jsFunc-messageFuncs.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#' Show a message
#'
#' \code{alert} (and its alias \code{info}) shows a message to the user as a
#' simple popup.\cr
#' simple popup.\cr\cr
#' \code{logjs} writes a message to the JavaScript console. \code{logjs} is
#' mainly used for debugging purposes as a way to non-intrusively print
#' messages, but it is also visible to the user if they choose to inspect the
#' console.
#' console. You can also use the \code{\link[shinyjs]{showLog}} function to
#' print the JavaScript message directly to the R console.
#'
#' @param text The message to show. Can be either simple text or an R object.
#' @seealso \code{\link[shinyjs]{useShinyjs}},
#' \code{\link[shinyjs]{runExample}}
#' \code{\link[shinyjs]{runExample}},
#' \code{\link[shinyjs]{showLog}}
#' @note \code{shinyjs} must be initialized with a call to \code{useShinyjs()}
#' in the app's ui.
#' @examples
Expand Down
40 changes: 40 additions & 0 deletions R/showLog.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#' Print any JavaScript console.log messages in the R console
#'
#' When developing and debugging a Shiny that uses custom JavaScript code,
#' it can be helpful to use \code{console.log()} messages in JavaScript. This
#' function allows you to see these messages printed in the R console directly
#' rather than having to open the JavaScript console in the browser to view the
#' messages.\cr\cr
#' This function must be called in a Shiny app's server function, and you also
#' need to pass the \code{showLog=TRUE} parameter to \code{useShinyjs()}.
#' @note Due to an issue in shiny (see
#' https://github.com/rstudio/shiny/issues/928), duplicated consecutive log
#' messages will not get printed in R.
#' @examples
#' if (interactive()) {
#' library(shiny)
#'
#' shinyApp(
#' ui = fluidPage(
#' useShinyjs(showLog = TRUE), # Set up shinyjs with showLog
#' textInput("text", "Type something")
#' ),
#' server = function(input, output) {
#' showLog()
#' observe({
#' logjs(paste("Length of text:", nchar(input$text)))
#' })
#' }
#' )
#' }
#' @seealso \code{\link[shinyjs]{logjs}}
#' @export
showLog <- function() {
session <- getSession()
shiny::observeEvent(session$input[['shinyjs-showLog']], {
message("JAVASCRIPT LOG: ",
jsonlite::toJSON(session$input[['shinyjs-showLog']],
auto_unbox = TRUE)
)
})
}
32 changes: 26 additions & 6 deletions R/useShinyjs.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
#' \code{shinyjs} functions to work.\cr\cr
#' You can call \code{useShinyjs()} from anywhere inside the UI.
#'
#' @param rmd Set this to \code{TRUE} only if you are using \code{shinyjs} inside an
#' interactive R markdown document. If using this option, view the
#' @param rmd Set this to \code{TRUE} only if you are using \code{shinyjs}
#' inside an interactive R markdown document. If using this option, view the
#' \href{https://github.com/daattali/shinyjs}{README} online to learn how to
#' use shinyjs in R markdown documents.
#' @param debug Set this to \code{TRUE} if you want to see detailed debugging
#' statements in the JavaScript console. Can be useful when filing bug reports
#' to get more information about what is going on.
#' @param html Set this to \code{TRUE} only if you are using \code{shinyjs} in a Shiny
#' app that builds the entire user interface with a custom HTML file. If using this
#' option, view the \href{https://github.com/daattali/shinyjs}{README} online to learn
#' @param html Set this to \code{TRUE} only if you are using \code{shinyjs} in
#' a Shiny app that builds the entire user interface with a custom HTML file. If
#' 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.
#' @return Scripts that \code{shinyjs} requires that are automatically inserted
#' to the app's \code{<head>} tag.
#' @examples
Expand All @@ -38,10 +43,12 @@
#' @seealso \code{\link[shinyjs]{runExample}}
#' \code{\link[shinyjs]{extendShinyjs}}
#' @export
useShinyjs <- function(rmd = FALSE, debug = FALSE, html = FALSE) {
useShinyjs <- function(rmd = FALSE, debug = FALSE, html = FALSE,
showLog = FALSE) {
stopifnot(rmd == TRUE || rmd == FALSE)
stopifnot(debug == TRUE || debug == FALSE)
stopifnot(html == TRUE || html == FALSE)
stopifnot(showLog == TRUE || showLog == 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 @@ -71,6 +78,19 @@ 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;
console.log = function (message) {
Shiny.onInputChange("shinyjs-showLog", message);
oldLog.apply(console, arguments);
};
})();')
}

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

Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ list of the common functions:

- `inlineCSS` - easily add inline CSS to a Shiny app.

- `logjs` - print a message to the JavaScript console (mainly used for
debugging purposes).

- `runjs` - run arbitrary JavaScript code (not recommended to use this
in a published Shiny app).

Expand All @@ -119,9 +116,18 @@ list of the common functions:
information is available in the section "Calling your own JavaScript
functions from R".

- `runcodeUI()`+`runcodeServer()` - adds a text input to your app that
#### Functions that help you develop and debug your app

- `runcodeUI`+`runcodeServer` - adds a text input to your app that
lets you run arbitrary R code live

- `showLog` - print any JavaScript `console.log()` messages in the R
console, to make it easier and quicker to debug apps without having
to open the JS console

- `logjs` - print a message to the JavaScript console (mainly used for
debugging purposes).

[Check out the demo Shiny app](http://daattali.com/shiny/shinyjs-demo/)
to see some of these in action, or install `shinyjs` and run
`shinyjs::runExample()` to see more demo apps.
Expand Down
4 changes: 2 additions & 2 deletions inst/srcjs/shinyjs-default-funcs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// shinyjs 0.7.9003 by Dean Attali
// shinyjs 0.7.9005 by Dean Attali
// Perform common JavaScript operations in Shiny apps using plain R code

shinyjs = function() {
Expand Down Expand Up @@ -468,7 +468,7 @@ shinyjs = function() {
// write a message to the console for debugging purposes if debug mode is on
debugMessage : function(text) {
if (shinyjs.debug) {
console.log(text);
console.info(text);
}
},

Expand Down
8 changes: 5 additions & 3 deletions man/messageFuncs.Rd

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

44 changes: 44 additions & 0 deletions man/showLog.Rd

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

18 changes: 12 additions & 6 deletions man/useShinyjs.Rd

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

12 changes: 8 additions & 4 deletions vignettes/shinyjs.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,21 @@ an element

- `inlineCSS` - easily add inline CSS to a Shiny app.

- `logjs` - print a message to the JavaScript console (mainly used for
debugging purposes).

- `runjs` - run arbitrary JavaScript code (not recommended to use this in a
published Shiny app).

- `extendShinyjs` - allows you to write your own JavaScript functions and use
`shinyjs` to call them as if they were regular R code. More information is
available in the section "Calling your own JavaScript functions from R".

- `runcodeUI()`+`runcodeServer()` - adds a text input to your app that lets you run arbitrary R code live
#### Functions that help you develop and debug your app

- `runcodeUI`+`runcodeServer` - adds a text input to your app that lets you run arbitrary R code live

- `showLog` - print any JavaScript `console.log()` messages in the R console, to make it easier and quicker to debug apps without having to open the JS console

- `logjs` - print a message to the JavaScript console (mainly used for
debugging purposes).

[Check out the demo Shiny app](http://daattali.com/shiny/shinyjs-demo/) to see
some of these in action, or install `shinyjs` and run `shinyjs::runExample()`
Expand Down

0 comments on commit f23c089

Please sign in to comment.