Last active
June 17, 2021 18:37
-
-
Save jcheng5/3239667 to your computer and use it in GitHub Desktop.
Shiny example: Diamonds Explorer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
) | |
) |
Nevermind. Found I had another bug. Print command works FINE for both.
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
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?