Reputation: 127
I am new to Shiny and I did my search on two relavant questions:
Both examples involved one variable nested within another variable. With selection of A, you have second level of options 1, 2 and 3. With selection of B, you have options 4, 5 and 6. My problem is similar yet different. I am trying to create two selectInput
menus beased on two variables (Y: A, B, C and X: D, E, F) but they are partially dependent with no hierarchy. I use the table below to illustrate:
x\y A B C
D v v
E v v
F v v
So what I want is when I:
and vice versa
What will be the best way to approach this? I created a toy example to illustrate what I want to achieve:
library(lattice)
library(shiny)
y=c("A","A","A","A","A","A","A","A","A","A",
"B","B","B","B","B","B","B","B","B","B",
"B","B","B","B","B","C","C","C","C","C")
x=c("D","D","D","D","D","E","E","E","E","E",
"D","D","D","D","D","E","E","E","E","E",
"F","F","F","F","F","F","F","F","F","F")
mean.y=c(10.75,10.97,10.62,10.15,10.58,10.41,10.22,10.59,10.05,10.24,
10.84,10.54,10.38,10.06,10.70,10.14,10.80,10.99,10.43,10.59,
10.66,10.55,10.93,10.71,10.90,10.28,10.62,10.76,10.63,10.86)
mean.x=c(5.19,5.22,5.99,5.05,5.38,5.72,5.14,5.22,5.78,5.05,
5.94,5.39,5.71,5.45,5.66,5.61,5.46,5.24,5.79,5.67,
5.00,5.30,5.44,5.27,5.60,5.20,5.94,5.67,5.06,5.25)
dat=as.data.frame(cbind(y,x,mean.y,mean.x))
X=as.list(unique(as.character(dat$x)))
Y=as.list(unique(as.character(dat$y)))
ui=fluidPage(
titlePanel("X vs. Y Scatter Plots"),
uiOutput("x"),
uiOutput("y"),
plotOutput(outputId="scatter")
)
server=function(input,output){
output$x=renderUI({
selectInput("x","X",x)
})
output$y=renderUI({
selectInput("y","Y",y)
})
output$scatter=renderPlot({
dat=subset(dat,x==input$x & y==input$y)
xyplot(dat$mean.y~dat$mean.x)
})
}
shinyApp(ui=ui,server=server)
Edited: BTW, I would like to retrive those levels from the data instead of calling the level in the code since I will have several files that will be analyzed with different dependency.
Thank you all very much for the help!
Upvotes: 0
Views: 2498
Reputation: 7871
I think you need isolate
and updateSelectInput
Try
library(shiny)
y=c("A","A","A","A","A","A","A","A","A","A",
"B","B","B","B","B","B","B","B","B","B",
"B","B","B","B","B","C","C","C","C","C")
x=c("D","D","D","D","D","E","E","E","E","E",
"D","D","D","D","D","E","E","E","E","E",
"F","F","F","F","F","F","F","F","F","F")
dd=data.frame(x,y,stringsAsFactors = F)
ui=fluidPage(
titlePanel("X vs. Y Scatter Plots"),
selectInput("x","X",c("",unique(dd$x))),
selectInput("y","Y",c("",unique(dd$y)))
)
server=function(input,output,session){
observeEvent(input$x,{
if(input$x==""){
updateSelectInput(session,"y",choices = c("",unique(dd$y)))
}else{
updateSelectInput(session,"y",choices = unique(dd$y[dd$x==input$x]),selected = isolate(input$y))
}
})
observeEvent(input$y,{
if(input$y==""){
updateSelectInput(session,"x",choices = c("",unique(dd$x)))
}else{
updateSelectInput(session,"x",choices = unique(dd$x[dd$y==input$y]),selected = isolate(input$x))
}
} )
}
shinyApp(ui=ui,server=server)
Update
Try add "" as choises to reset ( any time "" chiosed)
ui=fluidPage(
titlePanel("X vs. Y Scatter Plots"),
selectInput("x","X",c("_",unique(dd$x))),
selectInput("y","Y",c("_",unique(dd$y)))
)
server=function(input,output,session){
observeEvent(input$x,{
if(input$x=="_"){
updateSelectInput(session,"y",choices = c("_",unique(dd$y)))
}else{
updateSelectInput(session,"y",choices = c("_",unique(dd$y[dd$x==input$x])),selected = isolate(input$y))
}
})
observeEvent(input$y,{
if(input$y=="_"){
updateSelectInput(session,"x",choices = c("_",unique(dd$x)))
}else{
updateSelectInput(session,"x",choices = c("_",unique(dd$x[dd$y==input$y])),selected = isolate(input$x))
}
} )
}
shinyApp(ui=ui,server=server)
Upvotes: 1