Created
February 3, 2020 14:41
-
-
Save MayaGans/1ddd35b53f969d611ffa8206c214a48b to your computer and use it in GitHub Desktop.
Use HTML <select> to change Shiny radioButton
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) | |
# MODULE UI | |
customInputUI <- function(id) { | |
ns <- NS(id) | |
tagList( | |
tags$div( | |
HTML(" | |
<select id='yes_no'> | |
<option value='yes'>Yes</option> | |
<option value='no'>No</option> | |
</select>")), | |
verbatimTextOutput(ns("debug")), | |
radioButtons(ns("my_radio"), "Radio", choices = c("Yes", "No")) | |
) | |
} | |
# MODULE SERVER | |
customInput <- function(input, output, session) { | |
output$debug <- renderPrint({ input$testing }) | |
# This prints yes or no, can I use this to change the input? | |
# I think sendCustomMessage might be used the other way | |
# To communicate R -> JS | |
observe({ | |
session$sendCustomMessage(type = 'testmessage', message = input$testing) | |
}) | |
} | |
# ACTUAL UI | |
ui <- fixedPage( | |
customInputUI("custom_input"), | |
tags$script( | |
" | |
// this works in vanialla JS | |
// but here I get a weird ( syntax error?! | |
// $('#yes_no').change(function(){ | |
// $('input[name='custom_input-my_radio'][value=' + $(this).val() + ']') | |
// .prop('checked', true); | |
// }).trigger('change'); | |
// get the selected value from JS | |
// on page load it's NULL but that's fine for now | |
$('select#yes_no').change(function() { | |
var selectedDropdown = $(this).children('option:selected').val(); | |
Shiny.setInputValue('custom_input-testing', selectedDropdown) | |
// Set input$my_radio using setInputValue? | |
// Shiny.setInputValue('custom_input-my_radio', $(this).val()) | |
}); | |
Shiny.addCustomMessageHandler('testmessage', function(message) { | |
// leverage this to change input$my_radio using JS? | |
// Probably the wrong approach | |
console.log(message) | |
}); | |
" | |
) | |
) | |
# ACTUAL SERVER | |
server <- function(input, output, session) { | |
df <- callModule(customInput, "custom_input") | |
} | |
shinyApp(ui, server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment