gpt4 book ai didi

r - 如何使 textOutput 更快?

转载 作者:行者123 更新时间:2023-12-03 23:00:32 25 4
gpt4 key购买 nike

我正在尝试创建一个只向 mainPanel 添加文本的应用程序。但是,添加文本时文本输出非常慢。
我想让这个瞬间快速而不是花费太多时间。有没有办法让它在浏览器中处理而不是去R?
代码

library(shiny)

ui <- fluidPage(sidebarLayout(
sidebarPanel(textInput("text", label = NULL)),
mainPanel(textOutput("textout"))
))


server <- function(input, output, session) {

output$textout <- renderText({
input$text
})
}

shinyApp(ui, server)
enter image description here

最佳答案

这是由于 Shiny 使用输入的方式。在 javascript 中,它有一个“去抖动”为 250 毫秒的选项,这解释了为什么它仅在您停止输入四分之一秒后才会更新。
您可以覆盖它,但它似乎涉及编写 textInput 的替代品。关键是 javascript 中的 getRatePolicy 函数。
enter image description here

library(shiny)

textinput_script <- "
<script>
var customTextInputBinding = $.extend({}, Shiny.inputBindings.bindingNames['shiny.textInput'].binding, {
find: function(scope) {
return $(scope).find('input.customTextInput');
},
subscribe: function(el, callback) {
$(el).on('keyup.customTextInputBinding input.customTextInputBinding', function(event) {
callback();
});
$(el).on('focusout.customTextInputBinding', function(event) { // on losing focus
callback();
});
},
unsubscribe: function(el) {
$(el).off('.customTextInputBinding');
},
getRatePolicy: function() {
return {
policy: 'direct'
};
}
});

Shiny.inputBindings.register(customTextInputBinding, 'shiny.customTextInput');
</script>
"

ui <- fluidPage(sidebarLayout(
sidebarPanel(
HTML(textinput_script),
customTextInput("text", label = NULL)
),
mainPanel(textOutput("textout"))
))

server <- function(input, output, session) {
output$textout <- renderText({
input$text
})
}

shinyApp(ui, server)
这是从 here 中提取的,并且通常执行此操作的实际指南是 here .

关于r - 如何使 textOutput 更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66079601/

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