Created
December 28, 2019 01:11
-
-
Save helgasoft/9b377508946b2faecc95ce3b93fbe561 to your computer and use it in GitHub Desktop.
Customize Shiny dropdown label thru Javascript
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
library(shiny) | |
indiv <- c('NA'=1,'STOCH'=2,'SMI'=3,'MACD'=4,'ROC'=5,'MFI'=6,'RSI'=7,'CCI'=8) | |
ui <- fluidPage( | |
titlePanel("Customize dropdown label thru Javascript"), | |
mainPanel( | |
fluidRow( selectInput(inputId="indik", label="Indicator", choices=indiv), | |
"click checkbox to reset the dropdown" | |
), | |
tags$script( | |
HTML(" window.onload = function() { | |
var dlb = document.getElementById('indik').parentNode.parentNode.firstElementChild; // Shiny's natural ordering | |
var myspan = document.createElement('span'); | |
var lbl = document.createElement('LABEL'); lbl.textContent='Indicator'; lbl.className='control-label'; //lbl.htmlFor='indik-selectized'; | |
var cckbox = document.createElement('input'); cckbox.type='checkbox'; cckbox.id='iunsel'; cckbox.class='shinyjs-resettable shiny-bound-input'; cckbox.style.marginLeft='30px'; | |
myspan.appendChild(lbl); myspan.appendChild(cckbox); | |
dlb.parentNode.replaceChild(myspan, dlb); | |
} | |
")) | |
) | |
) | |
server <- function(input, output, session) { | |
# see also session$onFlushed(function() { session$sendCustomMessage(type='jsCode', list(value=myScript)) }, once=TRUE) | |
observeEvent(input$iunsel, { | |
updateSelectInput(session, 'indik', selected=1 ) | |
}) | |
} | |
shinyApp(ui = ui, server = server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment