以下工作示例源自我先前的问题Prevent an event happened in Shiny if no UI inputs are available。现在,我想用逗号格式化numericInput
字段,以帮助用户读取大数字。我按照本文“ https://beta.rstudioconnect.com/barbara/format-numbers/”中的选项2的示例进行了更改,以这种样式更改了Total
numericInput
。关键是创建一个.js
文件,该文件将数字格式化并将其存储在与闪亮脚本相同的文件夹下的www
目录中。
与Total
numericInput一起很好地工作。但是,如何为更新后添加的numericINputs
使用相同的格式?面临的挑战是我不知道以后会添加多少numericInput
,因此如果我不知道要添加到该文件的Inpur ID,则很难修改format_numbers.js
文件。format_numbers.js
如下。
$(document).ready(function() {
// Helper function to guarantee cross-browser compatibility
// adapted from: http://stackoverflow.com/a/16157942
function localeString(x, sep, grp) {
var sx = (''+x).split('.'), s = '', i, j;
sep || (sep = ','); // default separator
grp || grp === 0 || (grp = 3); // default grouping
i = sx[0].length;
while (i > grp) {
j = i - grp;
s = sep + sx[0].slice(j, i) + s;
i = j;
}
s = sx[0].slice(0, i) + s;
sx[0] = s;
return sx.join('.');
}
// To change Total's input field (lose arrows and other functionality)
$('#Total')[0].type = 'text';
// To format the number when the app starts up
$('#Total').val(localeString($('#Total').val()));
// To format the number whenever the input changes
$('#Total').keyup(function(event) {
$(this).val(localeString($(this).val().replace(/,/g, '')));
});
});
闪亮的脚本如下。
library(shiny)
# Define UI
ui <- fluidPage(
# Modify tags
tags$head(tags$script(src = "format_numbers.js")),
# Action button to add numeric input
actionButton("add", "Add UI"),
actionButton("sum", "Sum"),
# Numeric Input
numericInput(inputId = "Total", label = "Total", value = 0),
# Text output
"The number is ",
textOutput(outputId = "out_num", inline = TRUE)
)
# Server logic
server <- function(input, output, session){
# Add numeric input
observeEvent(input$add, {
insertUI(
selector = "#add",
where = "afterEnd",
ui = numericInput(paste0("txt", input$add), label = "Number", value = 0)
)
})
# Reactive values for Total
Num_In <- reactiveValues(
Total_In = 0
)
# Convert number to character
# This is to fill in the Total numeric input formatting with comma
total_num_as_char <- reactive({format(Num_In$Total_In, big.mark = ",", trim = TRUE)})
total_input <- reactive({Num_In$Total_In})
observeEvent(input$sum, {
num_names <- names(input)[grepl("^txt", names(input))]
if (length(num_names) == 0) {
foo <- 0
} else {
foo <- sum(sapply(num_names, function(x) input[[x]]), na.rm = TRUE)
}
Num_In$Total_In <- foo
updateNumericInput(session = session,
inputId = "Total",
value = total_num_as_char())
})
# Convert input to numeric
total_num <- reactive({as.numeric(gsub(",", "", input$Total))})
# Create text output
output$out_num <- renderText({total_num()})
}
# Complete app with UI and server components
shinyApp(ui, server)
最佳答案
对我来说,以下工作。
用insertUI
添加UI组件时,将触发JS事件shiny:bound
。然后,我们可以利用它:
// Helper function to guarantee cross-browser compatibility
// adapted from: http://stackoverflow.com/a/16157942
function localeString(x, sep, grp) {
var sx = (''+x).split('.'), s = '', i, j;
sep || (sep = ','); // default separator
grp || grp === 0 || (grp = 3); // default grouping
i = sx[0].length;
while (i > grp) {
j = i - grp;
s = sep + sx[0].slice(j, i) + s;
i = j;
}
s = sx[0].slice(0, i) + s;
sx[0] = s;
return sx.join('.');
}
$(document).ready(function() {
// To change Total's input field (lose arrows and other functionality)
$('#Total')[0].type = 'text';
// To format the number when the app starts up
$('#Total').val(localeString($('#Total').val()));
// To format the number whenever the input changes
$('#Total').keyup(function(event) {
$(this).val(localeString($(this).val().replace(/,/g, '')));
});
});
$(document).on('shiny:bound', function(evt){
var id = evt.target.getAttribute('id');
if((/^(txt)/).test(id)){
var selector = '#' + id;
$(selector)[0].type = 'text';
$(selector).val(localeString($(selector).val()));
$(selector).keyup(function(event) {
$(this).val(localeString($(this).val().replace(/,/g, '')));
});
}
});
现在,在R中:
unformat <- function(x) as.numeric(gsub(",", "", x))
和
observeEvent(input$sum, {
num_names <- names(input)[grepl("^txt", names(input))]
if (length(num_names) == 0) {
foo <- 0
} else {
foo <- sum(sapply(num_names,
function(x) unformat(input[[x]])), na.rm = TRUE)
}
Num_In$Total_In <- foo
updateNumericInput(session = session,
inputId = "Total",
value = total_num_as_char())
})
关于javascript - 如何使用更新的UI在Shiny中格式化numericInput?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55584508/