Last active
December 20, 2015 16:29
-
-
Save klittle314/6161538 to your computer and use it in GitHub Desktop.
simple dynamic ui shiny example
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
#Global function for test of DRG State Hospital display | |
# 29 July 2013 Kevin Little, Ph.D. | |
#create data elements: for each state, there are multiple hospitals that report | |
#values for multiple DRGs | |
State<-as.factor(rep(c("CA","CA","CA","CA","NJ","NJ","NJ","WY","WY"),3)) | |
DRG<-as.factor(rep(c("101","102","103"),each=9)) | |
Hospital<-as.factor(rep(c("C1","C2","C3","C4","N1","N2","N3","W1","W2"),3)) | |
set.seed(1234) | |
x1<-10+rnorm(27) | |
x2<-x1+0.5*rnorm(27) | |
#now create a data frame | |
df_test<-cbind.data.frame(DRG,State,Hospital,x1,x2) | |
DRGName<-levels(df_test$DRG) | |
names(DRGName)<-DRGName | |
States<-levels(df_test$State) | |
#INITIALIZE HOSPITAL NAME LIST | |
StateHospitalNames<-vector("list",3) | |
HospNames<-function(i){ | |
ds<-as.factor(df_test[df_test$State==States[i],3]) | |
ds<-droplevels(ds) | |
dss<-levels(ds) | |
} | |
for(i in 1:3){ | |
StateHospitalNames[[i]]<-HospNames(i) | |
} |
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
#server.R | |
#accept input string of state name from ui, pass back hospital within state; create | |
# data summaries for DRG, for state, and for hospital within state. | |
# library(googleVis) | |
# suppressPackageStartupMessages(library(googleVis)) | |
shinyServer(function(input, output) { | |
StateDataInput <- reactive({ | |
# cat(as.character(paste(input$State,"Z "))) | |
d3<-df_test[df_test[,2]==input$State,] | |
d3<-droplevels(d3) | |
return(d3) | |
}) | |
DRGDataInput<-reactive({ | |
d4<-df_test[df_test[,1]==input$DRG,] | |
d4<-droplevels(d4) | |
return(d4) | |
}) | |
#The next code creates a reactive drop down list, allowing you to choose a hospital | |
#for a state. | |
output$HospList<-renderUI({ | |
data<-StateDataInput() | |
if(is.null(data)) return(NULL) | |
HospList<-levels(StateDataInput()$Hospital) | |
selectInput("HospList","Choose Hospital", HospList) | |
}) | |
output$summary1 <- renderPrint({ | |
dataset <- DRGDataInput() | |
summary(dataset) | |
}) | |
output$summary2 <- renderPrint({ | |
dataset2 <- StateDataInput() | |
summary(dataset2) | |
}) | |
output$summary3 <- renderPrint({ | |
dataset3 <- StateDataInput()[StateDataInput()$DRG==input$DRG & StateDataInput()$Hospital==input$HospList,] | |
dataset3<-droplevels(dataset3) | |
summary(dataset3) | |
}) | |
}) |
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
# ui.R | |
# create drop down lists for DRG and state (fixed) and hospital within state (dynamic) | |
shinyUI(pageWithSidebar( | |
headerPanel("2011 CMS $ Charged and Paid by DRG and state"), | |
sidebarPanel( | |
selectInput("DRG","Choose a DRG:", | |
choices = DRGName), | |
selectInput("State", "Choose a state:", | |
choices = States), | |
uiOutput("HospList") | |
), | |
#Display some output | |
mainPanel( | |
tabsetPanel( | |
tabPanel("DRG", verbatimTextOutput("summary1")), | |
tabPanel("State",verbatimTextOutput("summary2")), | |
tabPanel("Hospital",verbatimTextOutput("summary3")) | |
)) | |
)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment