Skip to content

Instantly share code, notes, and snippets.

@MayaGans
Last active February 1, 2020 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MayaGans/338355e5c63fc8fea4e037fabc010fa1 to your computer and use it in GitHub Desktop.
Save MayaGans/338355e5c63fc8fea4e037fabc010fa1 to your computer and use it in GitHub Desktop.
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 })
# Probably don't even need this
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?
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