Skip to content

Commit

Permalink
Add check if input values are non-empty (fixes #2) and encapsulate co…
Browse files Browse the repository at this point in the history
…de for querying TinyURL into '.queryTinyURL()'
  • Loading branch information
aoles committed Jan 22, 2016
1 parent bb21c50 commit 09eda18
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: shinyURL
Type: Package
Title: Save and restore the state of Shiny apps
Version: 0.0.20
Version: 0.0.21
Encoding: UTF-8
Author: Andrzej Oleś
Maintainer: Andrzej Oleś <[email protected]>
Expand Down
64 changes: 35 additions & 29 deletions R/shinyURL.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,36 +67,12 @@ shinyURL.server = function(session) {

## initialize from query string
init = .initFromURL(session, queryValues, init)

## encode current app's state
.encodeURL(session, inputId)

## use TinyURL for shortening URL
useTinyURL = ".getTinyURL" %in% names(isolate(reactiveValuesToList(session$input, all.names=TRUE)))

if ( useTinyURL ) {
input = session$input
.busyMsg = "Please wait..."

## construct a query string from the current URL
tinyURLquery = eventReactive(input$.getTinyURL, {
sprintf("http://tinyurl.com/api-create.php?url=%s", input[[inputId]])
})

## set busy message
observeEvent( tinyURLquery(), {
updateTextInput(session, inputId, value=.busyMsg)
runTinyURLquery$resume()
})

## query TinyURL
runTinyURLquery = observe({
if ( input[[inputId]]==.busyMsg ) {
tinyurl = tryCatch(getURL(tinyURLquery()), error = function(e) "Error fetching tinyURL!")
updateTextInput(session, inputId, value=tinyurl)
runTinyURLquery$suspend()
}
}, suspended=TRUE)
}
## use TinyURL for shortening the URL
.queryTinyURL(session)

## Initial invalidation needed to execute scheduled input updates when the
## browser is refreshed switched off because it interferes with dynamic UIs
Expand All @@ -105,6 +81,7 @@ shinyURL.server = function(session) {
invisible(NULL)
}


#' Deprecated functions
#'
#' @param ... Arguments passed to the new methods \code{\link{shinyURL.server}}
Expand Down Expand Up @@ -180,8 +157,12 @@ initFromURL = function(session, nestedDependency = FALSE, self, encode, resume =
## ".url" field to avoid self-dependency
inputValues = reactiveValuesToList(session$input, all.names=FALSE)

## quit if there is there are no inputs to encode
if (length(inputValues)==0) return()

This comment has been minimized.

Copy link
@aoles

aoles Jan 25, 2016

Author Owner

This is necessary for interactive rmarkdown documents, in which session$input starts off empty. rstudio/shiny#1040


## remove actionButtons
inputValues = inputValues[!unlist(lapply(inputValues, function(x) inherits(x, "shinyActionButtonValue")), use.names=FALSE)]
isActionButton = unlist(lapply(inputValues, function(x) inherits(x, "shinyActionButtonValue")), use.names=FALSE)
inputValues = inputValues[!isActionButton]

## remove ggvis specific inputs
idx = grep("_mouse_(over|out)$", names(inputValues))
Expand Down Expand Up @@ -255,7 +236,6 @@ initFromURL = function(session, nestedDependency = FALSE, self, encode, resume =

.initFromURL = function(session, queryValues, self) {
observe({
session = getDefaultReactiveDomain()
queryValuesCopy = queryValues

## suspend if nothing to do
Expand Down Expand Up @@ -309,3 +289,29 @@ initFromURL = function(session, nestedDependency = FALSE, self, encode, resume =
session$sendInputMessage(names(queryValues)[i], list(value=q))
}
}


.queryTinyURL = function(session) {
input = session$input
.busyMsg = "Please wait..."

## construct a query string from the current URL
tinyURLquery = eventReactive(input$.getTinyURL, {
sprintf("http://tinyurl.com/api-create.php?url=%s", input[[inputId]])
})

## set busy message
observeEvent(tinyURLquery(), {
updateTextInput(session, inputId, value=.busyMsg)
runTinyURLquery$resume()
})

## query TinyURL
runTinyURLquery = observe({
if ( input[[inputId]]==.busyMsg ) {
tinyurl = tryCatch(getURL(tinyURLquery()), error = function(e) "Error fetching tinyURL!")
updateTextInput(session, inputId, value=tinyurl)
runTinyURLquery$suspend()
}
}, suspended=TRUE)
}

0 comments on commit 09eda18

Please sign in to comment.