Browse files

Integrate UI builder into Shiny

Replace example #1 HTML with builder
  • Loading branch information...
1 parent 442f3d9 commit 04081ec2d3393cbcaa89e8cc68f9534a177bad1e @jcheng5 jcheng5 committed Jul 18, 2012
Showing with 125 additions and 79 deletions.
  1. +18 −0 NAMESPACE
  2. +89 −55 R/ui.R
  3. +14 −1 examples/01_allcaps/app.R
  4. +0 −21 examples/01_allcaps/www/index.html
  5. +3 −1 man/startApp.Rd
  6. +1 −1 shiny.Rproj
View
18 NAMESPACE
@@ -1,10 +1,28 @@
+export(br)
+export(defineUI)
+export(div)
+export(h1)
+export(h2)
+export(head)
+export(header)
+export(img)
+export(input)
+export(inputs)
export(invalidateLater)
+export(outputs)
+export(p)
+export(page)
export(reactive)
export(reactivePlot)
export(reactiveTable)
export(reactiveText)
export(reactiveTimer)
export(runApp)
+export(script)
+export(shinyPlot)
+export(shinyText)
+export(style)
+export(tag)
S3method(reactive,default)
S3method(reactive,"function")
S3method("$",reactvaluesreader)
View
144 R/ui.R
@@ -1,10 +1,10 @@
-
-tag <- function(name, ...) {
+#' @export
+tag <- function(`_tag_name`, ...) {
# create basic tag data structure
tag <- list()
class(tag) <- "shiny.tag"
- tag$name <- name
+ tag$name <- `_tag_name`
tag$attribs <- list()
tag$children <- list()
@@ -14,72 +14,92 @@ tag <- function(name, ...) {
if (is.null(varArgsNames))
varArgsNames <- character(length=length(varArgs))
- for (i in 1:length(varArgsNames)) {
- # save name and value
- name <- varArgsNames[[i]]
- value <- varArgs[[i]]
-
- # process attribs
- if (nzchar(name))
- tag$attribs[[name]] <- value
-
- # process child tags
- else if (inherits(value, "shiny.tag")) {
- tag$children[[length(tag$children)+1]] <- value
- }
-
- # process lists of children
- else if (is.list(value)) {
- for(child in value) {
- if (inherits(child, "shiny.tag"))
- tag$children[[length(tag$children)+1]] <- child
- else
- tag$children[[length(tag$children)+1]] <- as.character(child)
+ if (length(varArgsNames) > 0) {
+ for (i in 1:length(varArgsNames)) {
+ # save name and value
+ name <- varArgsNames[[i]]
+ value <- varArgs[[i]]
+
+ # process attribs
+ if (nzchar(name))
+ tag$attribs[[name]] <- value
+
+ # process child tags
+ else if (inherits(value, "shiny.tag")) {
+ tag$children[[length(tag$children)+1]] <- value
+ }
+
+ # process lists of children
+ else if (is.list(value)) {
+ for(child in value) {
+ if (inherits(child, "shiny.tag"))
+ tag$children[[length(tag$children)+1]] <- child
+ else
+ tag$children[[length(tag$children)+1]] <- as.character(child)
+ }
+ }
+
+ # everything else treated as text
+ else {
+ tag$children[[length(tag$children)+1]] <- as.character(value)
}
- }
-
- # everything else treated as text
- else {
- tag$children[[length(tag$children)+1]] <- as.character(value)
}
}
# return the tag
return (tag)
}
+#' @export
h1 <- function(...) {
tag("h1", ...)
}
+#' @export
h2 <- function(...) {
tag("h2", ...)
}
+#' @export
p <- function(...) {
tag("p", ...)
}
+#' @export
div <- function(...) {
tag("div", ...)
}
+#' @export
img <- function(...) {
tag("img", ...)
}
+#' @export
head <- function(...) {
tag("head", ...)
}
+#' @export
script <- function(...) {
tag("script", ...)
}
+#' @export
style <- function(...) {
tag("style", ...)
}
+#' @export
+input <- function(...) {
+ tag("input", ...)
+}
+
+#' @export
+br <- function(...) {
+ tag("br", ...)
+}
+
htmlEscape <- local({
.htmlSpecials <- list(
`&` = '&amp;',
@@ -119,24 +139,34 @@ htmlEscape <- local({
}
})
+#' @export
shinyPlot <- function(outputId) {
list(head(script(src="foobar.js"),
style(src="foobar.css")),
- img(id = outputId, class ="live-plot"))
+ div(id = outputId, class ="live-plot"))
+}
+
+#' @export
+shinyText <- function(outputId) {
+ div(id = outputId, class = "live-text")
}
+#' @export
header <- function(...) {
div(class="shiny-header", ...)
}
+#' @export
inputs <- function(...) {
div(class="shiny-inputs", ...)
}
+#' @export
outputs <- function(...) {
div(class="shiny-outputs", ...)
}
+#' @export
defineUI <- function(...) {
div(class="shiny-ui", ...)
}
@@ -243,32 +273,36 @@ renderPage <- function(ui, connection) {
con = connection)
}
+#' @export
+page <- function(ui, path='/') {
+ function(ws, header) {
+ if (header$RESOURCE != path)
+ return(NULL)
+
+ textConn <- textConnection(NULL, "w")
+ on.exit(close(textConn))
+
+ renderPage(ui, textConn)
+ html <- paste(textConnectionValue(textConn), collapse='\n')
+ return(http_response(ws, 200, content=html))
+ }
+}
-ui <- defineUI(
- header(
- h1("My first application"),
- p("This is a really exciting application")
- ),
- inputs(
- p("Here are the inputs")
- ),
- outputs(
- p("Check out my shiny plot:"),
- shinyPlot("plot1"),
- p("Check out my other shiny plot:"),
- shinyPlot("plot2")
- )
-)
+# ui <- defineUI(
+# header(
+# h1("My first application"),
+# p("This is a really exciting application")
+# ),
+# inputs(
+# p("Here are the inputs")
+# ),
+# outputs(
+# p("Check out my shiny plot:"),
+# shinyPlot("plot1"),
+# p("Check out my other shiny plot:"),
+# shinyPlot("plot2")
+# )
+# )
#renderPage(ui, stdout())
-
-
-
-
-
-
-
-
-
-
View
15 examples/01_allcaps/app.R
@@ -1,9 +1,22 @@
library(shiny)
+
+ui <- defineUI(
+ h1("Example 1: All Caps"),
+ p(
+ "Input:", br(),
+ input(name='val', type='text', value='Hello World!')
+ ),
+ p(
+ "You said:", br(),
+ shinyText("valUpper")
+ )
+)
+
app <- function(input, output) {
output$valUpper <- reactive(function() {
toupper(input$val)
})
}
-runApp(client='./www', server=app)
+runApp(client=page(ui), server=app)
View
21 examples/01_allcaps/www/index.html
@@ -1,21 +0,0 @@
-<html>
-<head>
- <script src="shared/jquery.js" type="text/javascript"></script>
- <script src="shared/shiny.js" type="text/javascript"></script>
- <link rel="stylesheet" type="text/css" href="shared/shiny.css"/>
-</head>
-<body>
- <h1>Example 1: All Caps</h1>
-
- <p>
- Input:<br/>
- <input name="val" type="text" value="Hello World!"/>
- </p>
-
- <p>
- You said:<br/>
- <div id="valUpper" class="live-text"/>
- </p>
-
-</body>
-</html>
View
4 man/startApp.Rd
@@ -9,7 +9,9 @@
\arguments{
\item{client}{Path to the root of the
application-specific www files (which should include
- index.html).}
+ index.html); or, a function that knows how to serve up
+ www files (TODO: document); or, a list of one or more
+ paths and/or functions.}
\item{server}{If a character string, a path to the R file
that contains the server application logic. If a
View
2 shiny.Rproj
@@ -14,4 +14,4 @@ LaTeX: pdfLaTeX
RootDocument:
BuildType: Package
-PackageRoxygenize: rd
+PackageRoxygenize: rd,namespace

0 comments on commit 04081ec

Please sign in to comment.