In my ShinyR APP I am trying to get value from color picker. Using onevent I can get and put value in input control but when I am pressing save button (which triggers onclick event), I am receiving just “null” or empty value from the input control. Why input[['titleColorChart']]
gives me “null” or empty instead of right value.
library(shiny)
library(shiny)
library(shinyjs)
library(DT)
library(shinyjqui)
ui <- fluidPage(
useShinyjs(), # Set up shinyjs
extendShinyjs(text = jsCode, functions = c("updateColorTagValue")),
div(tags$input(id = str_c("titleColorChart","1"),
name = str_c("titleColorChart","1"),
selector = str_c("titleColorChart","1"),
type = "color",
`data-role` = "color-selector",
value = "",
class = "form-control input-xs",
style = "width: 20px;padding: 0;border: none;/* margin: 0; */"),
tags$input(id = str_c("titleColorChart"),
name = str_c("titleColorChart"),
selector = str_c("titleColorChart"),
type = "text",
value = "",
class = "form-control input-xs",
style = "padding: 0;border: none;/* margin: 0; */")
),
tags$button(id = "save",
type = "button",
class = "tool-button",
span("Save"))
)
server <- function(input, output, session) {
jsCode <- "
shinyjs.updateColorTagValue= function(params){
var colorString;
colorString = JSON.stringify($('#' + params[0] + '1').val());
colorString = colorString.substring(1, colorString.length-1);
alert(colorString)
$('#' + params[0] + '1' ).attr('value',colorString);
$('#' + params[0]).attr('value',colorString);
Shiny.setInputValue('#' + params[0] + '', colorString);
Shiny.onInputChange(params[0],params[1]);
}
"
nameOfDropDownList <- "titleColorChart"
observeEvent(input[[str_c(nameOfDropDownList,"1")]], {
input[[nameOfDropDownList]] <- input[[str_c(nameOfDropDownList,"1")]]
})
observe({
input[[str_c(nameOfDropDownList,"1")]]
output[[nameOfDropDownList]] <- renderText({
input[[str_c(nameOfDropDownList,"1")]]
})
})
observeEvent(input$save, {
updateColourInput(session, "titleColorChart",
value = input$titleColorChart1
)
})
onevent("change", str_c(nameOfDropDownList,"1") ,js$updateColorTagValue(nameOfDropDownList, input$titleColorChart))
onclick("save",alert(input[["titleColorChart"]]))
}
shinyApp(ui, server)