我正在尝试编写一个javascript函数来扩展R闪亮action button demo。我希望用户能够通过单击操作按钮并在选择数字输入表单时按Enter键来输入数字。 ui.R的代码是:
shinyUI(pageWithSidebar(
headerPanel("actionButton test"),
sidebarPanel(
tags$head(tags$script(src = "enter_button.js")),
numericInput("number", "N:", min = 0, max = 100, value = 50),
br(),
actionButton("goButton", "Go!"),
p("Click the button to update the value displayed in the main panel.")
),
mainPanel(
verbatimTextOutput("nText")
)
))
和server.R:
shinyServer(function(input, output) {
ntext <- eventReactive(input$goButton, {
input$number
})
output$nText <- renderText({
paste(ntext(), input$goButton)
})
})
我将以下javascript包含在www /文件夹中名为enter_button.js的文件中:
$("#number").keyup(function(event){
if(event.keyCode == 13){
$("#goButton").click();
}
});
然而,当我运行应用程序时,选择数字输入表单并点击输入操作按钮不会增加,但如果我点击它会增加。或者,我也测试了只需按下输入文档中的任何位置:
$(document).keyup(function(event){
if(event.keyCode == 13){
$("#goButton").click();
}
});
这很有效,所以我怀疑jQuery找不到#number元素。提前谢谢!
答案 0 :(得分:20)
我能够使用jQuery is(":focus")
函数解决这个问题,我使用的代码是:
$(document).keyup(function(event) {
if ($("#number").is(":focus") && (event.key == "Enter")) {
$("#goButton").click();
}
});
答案 1 :(得分:0)
这是我最好的解决方案。我真的不喜欢它,但至少它是有效的。
library(shiny)
# This is a demo app to test a key binding on an actionButton
# Uncommenting the info item (on both UI and server) will display internal stuff
runApp(
list(
#############################################
# UI
#############################################
ui = bootstrapPage(
textInput ("myinput", label = "Write something here"),
tags$script('
$(document).on("keydown", function (e) {
Shiny.onInputChange("lastkeypresscode", e.keyCode);
});
'),
actionButton("GO", "Lancer le matching !"),
# verbatimTextOutput("info"),
verbatimTextOutput("results")
),
#############################################
# SERVER
#############################################
server = function(input, output, session) {
# There are state variables for the input text and GO button
curr.val <- "" # Corresponds to the current displayed input$myinput
curr.go <- 0 # Corresponds to the last known GO value (integer)
lastEvent <- reactive({
# Is reactive to the following events
input$GO
input$lastkeypresscode
# Decide which action should be taken
if(input$GO > curr.go) {
# The user pushed the GO actionButton, so take action
action <- 1
curr.go <<- input$GO
} else if(input$lastkeypresscode == 13) {
# The user pressed the Enter key, so take action
action <- 1
} else {
# The user did anything else, so do nothing
action <- 0
}
return(action)
})
output$results = renderPrint({
if(lastEvent() == 1) {
curr.val <<- isolate(input$myinput)
}
curr.val
})
# output$info = renderText({
# paste(curr.val, curr.go, input$lastkeypresscode, sep = ", ")
# })
}
)
)
答案 2 :(得分:0)
这个github页面的代码就像一个魅力: https://github.com/daattali/advanced-shiny/blob/master/proxy-click/app.R
来源:https://deanattali.com/blog/advanced-shiny-tips/#proxy-click
library(shiny)
jscode <- '
$(function() {
var $els = $("[data-proxy-click]");
$.each(
$els,
function(idx, el) {
var $el = $(el);
var $proxy = $("#" + $el.data("proxyClick"));
$el.keydown(function (e) {
if (e.keyCode == 13) {
$proxy.click();
}
});
}
);
});
'
ui <- fluidPage(
tags$head(tags$script(HTML(jscode))),
actionButton("btn", "Click me to print the value in the text field"),
div("Or press Enter when the text field is focused to \"press\" the button"),
tagAppendAttributes(
textInput("text", NULL, "foo"),
`data-proxy-click` = "btn"
)
)
server <- function(input, output, session) {
observeEvent(input$btn, {
cat(input$text, "\n")
})
}
shinyApp(ui, server)
答案 3 :(得分:0)
在闪亮的应用中添加以下代码。
外部Ui和服务器功能-
jscode <- '
$(function() {
var $els = $("[data-proxy-click]");
$.each(
$els,
function(idx, el) {
var $el = $(el);
var $proxy = $("#" + $el.data("proxyClick"));
$el.keydown(function (e) {
if (e.keyCode == 13) {
$proxy.click();
}
});
}
);
});
'
Ui内部功能-
tags$head(tags$script(HTML(jscode))),
`data-proxy-click` = "goButton"
goButton-动作按钮的名称
这将使您百分百享受。