Skip to content

@gjabel /server.R secret

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Trying out shiny
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()
})
})
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"))
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.