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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| dans la partie "UI.R":
library(shiny)
source('ref.R')
PAGE_WIDTH <- 1700
span.widths <- Reduce(paste, sapply(1:12, function(n) paste0('.span', n, '{width:', floor(n * PAGE_WIDTH / 12), 'px;}')))
shinyUI(fluidPage(
#Barre de navigation
navbarPage( title="Test", windowTitle="Test", collapsible=TRUE,
tabPanel("page 1",
#Sidebar Panel
sidebarLayout(position="left", fluid=TRUE,
sidebarPanel( width= 2,
h4("c1/c2"),
do.call(verticalLayout,row.list.c1)
),
mainPanel(width = 10,
h4(textOutput('txtCurrentc1')),
uiOutput('c2.buttons'),
h4(textOutput('txtc2Name'))
))
),
tabPanel("page 2")
)))
la partie "server.R":
source('ref.R')
last.states.c1 <<- sapply(as.character(unique(df.parts$c1)), function(Y) 0)
#generation de la liste de bouttons c2 en fonction de c1 selectionne
renderc2Buttons <- function(currentc1) {
renderUI({
lapply(rownames(df.parts[df.parts$c1 == as.character(currentc1), ]), function(cl) {
with(df.parts[df.parts$c2 == cl, ],
actionButton(inputId=paste0('btn', cl),label=cl))
})
})
}
### MAIN SERVER LOOP
shinyServer(function(input, output, session) {
###############################################
button.states.c1 <- reactive({
sapply(as.character(unique(df.parts$c1)), function(c1) {input[[paste0("btn",c1)]]})
})
# c1 # selection button observer
observe({
# Detect which button was pressed
states.c1 <<- button.states.c1()
change.c1 <<- states.c1 != last.states.c1
if (sum(change.c1) > 0) {
print("checkpoint")
currentc1 <<- names(which(change.c1))[1]
last.states.c2 <<- sapply(as.character(df.parts$c2[df.parts$c1 == as.character(currentc1)]), function(x) 0)
states.c2 <<- sapply(as.vector(df.parts[df.parts$c1 == currentc1,2]), function(x) 0)
print("Current c1 Script Executed")
#Update the UI
output$txtCurrentc1 <- renderText(paste("Colonne selectionnée ",currentc1))
output$c2.buttons <- renderc2Buttons(currentc1)
}
# Remember button states
last.states.c1 <<- states.c1
})
######################################################
button.states.c2 <- reactive({
sapply(as.vector(df.parts[df.parts$c1 == currentc1,2]), function(cl) input[[paste0('btn', cl)]])
})
######################################################
# c2 # selection button observer
observe({
# Detect which button was pressed
states.c2 <<- button.states.c2()
print(states.c2)
change.c2 <- states.c2 != last.states.c2
print(change.c2)
if (sum(change.c2) != 0) {
print("checkpoint c2")
currentc2 <<- names(which(change.c2))[1]
# Update the UI
output$txtc2Name <- renderText(as.character(df.parts$c2[df.parts$c2 == as.character(currentc2)]))
}
# Remember button states
last.states.c2 <<- states.c2
})
})
et enfin la partie "ref.R":
currentc1 <- NULL
#Creation du data frame
df.parts <- data.frame(c1 = c("a","a","a","a","a","b","b","b","c","c","c","c","c","c"), c2 = c("1","2","3","4","5","6","7","8","9","10","11","12","13","14"))
rownames(df.parts) <- df.parts$c2
row.list.c1 <- lapply(unique(df.parts$c1), function(p){
c1.buttons <- lapply(p, function(c1){
actionButton(inputId=paste0("btn",c1), label=c1)
})
fluidRow(id= p, c1.buttons)
}) |
Partager