Instantly share code, notes, and snippets.

@jcheng5 /server.R
Last active Jul 30, 2018

Embed
What would you like to do?
Shiny example: Diamonds Explorer
library(shiny)
library(ggplot2)
function(input, output) {
dataset <- reactive({
diamonds[sample(nrow(diamonds), input$sampleSize),]
})
output$plot <- renderPlot({
p <- ggplot(dataset(), aes_string(x=input$x, y=input$y)) + geom_point()
if (input$color != 'None')
p <- p + aes_string(color=input$color)
facets <- paste(input$facet_row, '~', input$facet_col)
if (facets != '. ~ .')
p <- p + facet_grid(facets)
if (input$jitter)
p <- p + geom_jitter()
if (input$smooth)
p <- p + geom_smooth()
print(p)
}, height=700)
}
library(shiny)
library(ggplot2)
dataset <- diamonds
pageWithSidebar(
headerPanel("Diamonds Explorer"),
sidebarPanel(
sliderInput('sampleSize', 'Sample Size', min=1, max=nrow(dataset),
value=min(1000, nrow(dataset)), step=500, round=0),
selectInput('x', 'X', names(dataset)),
selectInput('y', 'Y', names(dataset), names(dataset)[[2]]),
selectInput('color', 'Color', c('None', names(dataset))),
checkboxInput('jitter', 'Jitter'),
checkboxInput('smooth', 'Smooth'),
selectInput('facet_row', 'Facet Row', c(None='.', names(dataset))),
selectInput('facet_col', 'Facet Column', c(None='.', names(dataset)))
),
mainPanel(
plotOutput('plot')
)
)
@ramsdgp

This comment has been minimized.

Copy link

ramsdgp commented Aug 29, 2013

what is the source of the data set, can i access that.

dataset <- reactive(function() {
diamonds[sample(nrow(diamonds), input$sampleSize),]
})

@paulrougieux

This comment has been minimized.

Copy link

paulrougieux commented Mar 21, 2014

Impressive once I figured out which facet variable would be interesting to choose. For example X = price, Y=carat, Color=color, Facet Row = cut, Facet Column = clarity.

runApp() gives a warning: "Passing functions to 'reactive' is deprecated. Please use expressions instead." ?reactive doesn't talk about a replacement function. How would you update server.R to fix this deprecated function?

@jcheng5

This comment has been minimized.

Copy link
Owner

jcheng5 commented Mar 24, 2014

@paul4forest: Fixed, thanks. You stop passing in a real function and just pass in an expression. So reactive(function() { "foo" }) now becomes reactive({ "foo" }).

@MaxGhenis

This comment has been minimized.

Copy link

MaxGhenis commented Sep 27, 2014

@ramsdgp: diamonds is a dataset built into the ggplot2 package. So you can see it more directly with something like:

library(ggplot2)
head(diamonds)
@geotheory

This comment has been minimized.

Copy link

geotheory commented Nov 4, 2014

Hi. This fails for me with Error in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : polygon edge not found. Any idea why? (edit--- please ignore I just needed to re-install Cairo)
Really useful example, thanks for sharing jcheng5!

@HuichunChien

This comment has been minimized.

Copy link

HuichunChien commented Dec 10, 2014

Hi guys, I have a problem of building web apps with R using googleVis on shiny. My OS system is MacbookPro and I use RStudio. I updated GoogleVis to 0.56 version as well as shiny.
When I tried to repeat the first example on the following website (http://www.magesblog.com/2013/02/first-steps-of-using-googlevis-on-shiny.html), I got the error msg below.

Error:

  • Unable to deploy package dependency 'manipulate'

    The package was installed locally from source. Only packages
    installed from CRAN, BioConductor and GitHub are supported.

This is same error msg as the one I got two days ago when I tried to run GoogleVis and shiny together.
According to the website, "using googleVis on shiny" should be feasible, right!?
Any idea what's wrong on my issue?

Thanks for your time on reading and help.

@jmolayem

This comment has been minimized.

Copy link

jmolayem commented Jul 22, 2015

For some reason, my app does not like printing both ggplot and qplot with the same function. Does qplot() have some different print command I'm not aware of?

@jmolayem

This comment has been minimized.

Copy link

jmolayem commented Jul 22, 2015

Nevermind. Found I had another bug. Print command works FINE for both.

@ganong123

This comment has been minimized.

Copy link

ganong123 commented Feb 14, 2017

I am getting an error:

Warning: Error in as.character: cannot coerce type 'environment' to vector of type 'character'
Stack trace (innermost first):
    4: <Anonymous>
    3: do.call
    2: print.shiny.appobj
    1: <Promise>
Error : cannot coerce type 'environment' to vector of type 'character'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment