secret
Created

Trying out shiny

  • Download Gist
server.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
cn<-names(USArrests)
cn[3]<-"All"
sn<-rownames(USArrests)
USArrests$state<-sn
 
shinyServer(function(input, output) {
output$crime <- renderUI({
selectInput("crime", "Crime:", choices = as.list(cn), selected=as.list(cn[1]))
})
output$murder <- renderUI({
if(!is.null(input$crime)) {
if(input$crime==cn[1]){
selectInput("murder", "Choose States:", choices = as.list(sn), multiple=TRUE, selected=as.list(sn))
}
}
})
output$assult <- renderUI({
if(!is.null(input$crime)) {
if(input$crime==cn[2]){
selectInput("assult", "Choose States:", choices = as.list(sn), multiple=TRUE, selected=as.list(sn))
}
}
})
output$rape <- renderUI({
if(!is.null(input$crime)) {
if(input$crime==cn[4]){
selectInput("rape", "Choose States:", choices = as.list(sn), multiple=TRUE, selected=as.list(sn))
}
}
})
getdf <- reactive({
if(!is.null(input$crime)) {
if(input$crime==cn[1])
df2 <- subset(USArrests, state %in% input$murder)
if(input$crime==cn[2])
df2 <- subset(USArrests, state %in% input$assult)
if(input$crime==cn[4])
df2 <- subset(USArrests, state %in% input$rape)
if(input$crime==cn[3])
df2 <- USArrests
}
list(df2=df2)
})
getplot <- reactive({
if(!is.null(input$crime)) {
df2<-getdf()$df2
if(input$crime==cn[1])
plot(df2$UrbanPop, df2$Murder)
if(input$crime==cn[2])
plot(df2$UrbanPop, df2$Assult)
if(input$crime==cn[3])
plot(df2)
if(input$crime==cn[4])
plot(df2$UrbanPop, df2$Rape)
}
})
output$table <- renderTable({
values<-getdf()
values$df2
})
output$plot <- renderPlot({
getplot()
})
output$downloadData <- downloadHandler(
filename ="data.csv",
content = function(file) {
write.csv(getdf(), file, row.names=FALSE)
})
output$downloadPlot <- downloadHandler(
filename = "plot.png",
content = function(file) {
png(file)
getplot()
dev.off()
})
})
ui.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
library(shiny)
#runApp("./R/temp")
 
shinyUI(pageWithSidebar(
headerPanel('Example'),
sidebarPanel(
htmlOutput("crime"),
htmlOutput("murder"),
htmlOutput("assult"),
htmlOutput("rape"),
br(),
downloadButton('downloadData', 'Download Data'),
downloadButton('downloadPlot', 'Download Plot')
),
mainPanel(
tabsetPanel(
tabPanel("Plot", plotOutput("plot", height = "7in")),
tabPanel("Data", tableOutput("table"))
)
)
))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.