Skip to content

Commit

Permalink
initial commit for demonstration purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
timelyportfolio committed Jul 13, 2019
0 parents commit 27f8bfe
Show file tree
Hide file tree
Showing 17 changed files with 5,182 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
^node_modules$
^srcjs$
^app\.R$
^package\.json$
^webpack\.config\.js$
^yarn\.lock$
^flipcard\.Rproj$
^\.Rproj\.user$
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Rhistory
.RData
.Rproj.user
node_modules
17 changes: 17 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Package: flipcard
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R:
person(given = "First",
family = "Last",
role = c("aut", "cre"),
email = "[email protected]")
Description: What the package does (one paragraph).
License: What license it uses
Encoding: UTF-8
LazyData: true
Imports:
htmltools,
shiny,
reactR
RoxygenNote: 6.1.1
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Generated by roxygen2: do not edit by hand

export(flipcardInput)
export(updateFlipcardInput)
importFrom(htmltools,htmlDependency)
importFrom(htmltools,tags)
importFrom(reactR,createReactShinyInput)
importFrom(shiny,restoreInput)
43 changes: 43 additions & 0 deletions R/flipcard.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#' <Add Title>
#'
#' <Add Description>
#'
#' @importFrom shiny restoreInput
#' @importFrom reactR createReactShinyInput
#' @importFrom htmltools htmlDependency tags
#'
#' @export
flipcardInput <- function(
inputId,
default = "",
configuration = list()
) {
reactR::createReactShinyInput(
inputId,
"flipcard",
list(
reactR::html_dependency_mobx(),
htmltools::htmlDependency(
name = "flipcard-input",
version = "1.0.0",
src = "www/flipcard/flipcard",
package = "flipcard",
script = "flipcard.js"
)
),
default,
configuration,
htmltools::tags$div
)
}

#' <Add Title>
#'
#' <Add Description>
#'
#' @export
updateFlipcardInput <- function(session, inputId, value, configuration = NULL) {
message <- list(value = value)
if (!is.null(configuration)) message$configuration <- configuration
session$sendInputMessage(inputId, message);
}
65 changes: 65 additions & 0 deletions app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
library(shiny)
library(flipcard)

ui <- fluidPage(
titlePanel("reactR Input Example"),
wellPanel(style = "width:400px;", "Quick Example demonstrating flipping in Shiny."),
flipcardInput(
"flipInput",
configuration = list(
containerStyle = list(width = "400px", height = "400px"),
front = list(
tag = "img",
props = list(
src = "//static.pexels.com/photos/59523/pexels-photo-59523.jpeg",
style = list(height = "100%", width = "100%")
),
content = NULL
),
back = list(
tag = "img",
props = list(
src = "//img.buzzfeed.com/buzzfeed-static/static/2014-04/enhanced/webdr06/4/16/enhanced-11136-1396643149-13.jpg?no-auto",
style = list(height = "100%", width = "100%")
),
content = NULL
)
)
)
)

server <- function(input, output, session) {
isFlipped <- FALSE
observe({
invalidateLater(1000)
isFlipped <<- !isFlipped
flipcard::updateFlipcardInput(
session = session,
inputId = "flipInput",
value = NULL,
configuration = list(
containerStyle = list(width = "400px", height = "400px"),
isFlipped = isFlipped,
front = list(
tag = "img",
props = list(
src = "//static.pexels.com/photos/59523/pexels-photo-59523.jpeg",
style = list(height = "100%", width = "100%")
),
content = NULL
),
back = list(
tag = "img",
props = list(
src = "//img.buzzfeed.com/buzzfeed-static/static/2014-04/enhanced/webdr06/4/16/enhanced-11136-1396643149-13.jpg?no-auto",
style = list(height = "100%", width = "100%")
),
content = NULL
)
)
)
})

}

shinyApp(ui, server)
50 changes: 50 additions & 0 deletions app_mobx.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
library(shiny)
library(flipcard)

ui <- fluidPage(
tags$script(HTML(
"
// set up global state observable that we can reference with flipcardInput
state = mobx.observable({isFlipped: true});
// set up a Shiny message handler so we can change the state from R
Shiny.addCustomMessageHandler('flip', function(message) {
state.isFlipped = !state.isFlipped;
});
"
)),
titlePanel(tags$span("reactR Input Example with ", tags$a(href = "https://mobx.js.org/intro/overview.html", "mobx"))),
wellPanel(style = "width:400px;", "In the latest version of reactR, I added mobx. Here we leverage the value argument to reference a global observable state. A user can now click the card to flip."),
flipcardInput(
"flipInput",
default = "state",
configuration = list(
containerStyle = list(width = "400px", height = "400px"),
front = list(
tag = "img",
props = list(
src = "//static.pexels.com/photos/59523/pexels-photo-59523.jpeg",
style = list(height = "100%", width = "100%")
),
content = NULL
),
back = list(
tag = "img",
props = list(
src = "//img.buzzfeed.com/buzzfeed-static/static/2014-04/enhanced/webdr06/4/16/enhanced-11136-1396643149-13.jpg?no-auto",
style = list(height = "100%", width = "100%")
),
content = NULL
)
)
)
)

server <- function(input, output, session) {
# observe({
# invalidateLater(2000, session)
# session$sendCustomMessage("flip", list())
# })
}

shinyApp(ui, server)
54 changes: 54 additions & 0 deletions app_mobx2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
library(shiny)
library(flipcard)

ui <- fluidPage(
tags$script(HTML(
"
// set up global state observable that we can reference with flipcardInput
state = mobx.observable({isFlipped: true});
// set up a Shiny message handler so we can change the state from R
Shiny.addCustomMessageHandler('flip', function(message) {
state.isFlipped = !state.isFlipped;
});
// set up a click handler to flip in JavaScript
function handleClick(){ state.isFlipped = !state.isFlipped }
"
)),
titlePanel("reactR Input Example"),
wellPanel(style = "width: 400px;", "Now we have three mechanisms to flip. We can click on the image or on any of the buttons."),
tags$button("JavaScript Flip Button", onclick="handleClick()"),
actionButton("btnflip", "Shiny Flip Button"),
flipcardInput(
"flipInput",
default = "state",
configuration = list(
containerStyle = list(width = "400px", height = "400px"),
front = list(
tag = "img",
props = list(
src = "//static.pexels.com/photos/59523/pexels-photo-59523.jpeg",
style = list(height = "100%", width = "100%")
),
content = NULL
),
back = list(
tag = "img",
props = list(
src = "//img.buzzfeed.com/buzzfeed-static/static/2014-04/enhanced/webdr06/4/16/enhanced-11136-1396643149-13.jpg?no-auto",
style = list(height = "100%", width = "100%")
),
content = NULL
)
)
)
)

server <- function(input, output, session) {
observeEvent(input$btnflip, {
session$sendCustomMessage("flip", list())
})
}

shinyApp(ui, server)
21 changes: 21 additions & 0 deletions flipcard.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Version: 1.0

RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
Loading

0 comments on commit 27f8bfe

Please sign in to comment.