r - 如何使 textOutput 更快?
问题描述
我正在尝试创建一个仅将文本添加到 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)
解决方案
这是由于闪亮使用输入的方式。在 javascript 中,它有一个 250 毫秒的“去抖动”选项,这解释了为什么它仅在您停止输入四分之一秒后才更新。
您可以覆盖它,但它似乎涉及为 textInput 编写替换。关键是 javascript 中的 getRatePolicy 函数。
library(shiny)
library(shinyCustom)
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)
推荐阅读
- c++ - Memory leak during unordered_map::insert KeyEqual exception with GCC - breaking the strong exception safety guarantee?
- c - Linux Kernel Module : Problem with kernel_write function
- database - 如何解决启动 oracle-xe 问题?
- c - 警告:从不兼容的指针类型 [-Wincompatible-pointer-types]| 传递“transform_labels”的参数 2
- reactjs - 如何使用 Material-UI 更改 React 中链接的颜色?
- swift - 有没有办法将 Google Sheets API 与 Swift 一起使用来构建 macOS Cocoa 应用程序?
- mysql - Mysql:表名加密
- php - PHP 脚本不启动本地 shell 脚本
- php - 从多个插入中获取最后一个 id
- amazon-web-services - 有没有办法通过 terraform 模板更新 KOPS 集群配置详细信息(标记)并应用?