gpt4 book ai didi

javascript - 如何使用更新的 UI 在 Shiny 中格式化 numericInput?

转载 作者:行者123 更新时间:2023-11-30 19:39:15 26 4
gpt4 key购买 nike

以下工作示例源 self 之前的问题 Prevent an event happened in Shiny if no UI inputs are available .现在我想用逗号格式化 numericInput 字段以帮助用户阅读大量数字。我按照这篇文章“https://beta.rstudioconnect.com/barbara/format-numbers/”中的选项 2 的示例,将 Total numericInput 更改为这种样式。关键是创建一个 .js 文件来格式化数字并将其存储在与 shiny 脚本相同的文件夹下的 www 目录中。

这与 Total numericInput 配合得很好。但是,如何对那些更新后添加的 numericINputs 使用相同的格式?挑战是我不知道以后会添加多少 numericInput,所以如果我不知道 inpur 就很难修改 format_numbers.js 文件要添加到该文件的 ID。

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, '')));
});
});

Shiny 的脚本如下。

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com