Shiny Responds to Enter

Shiny Responds to Enter

In your case, the problem is reactive programming and this is the reason that you need something to manage this situation. My recommendation is to use observer pattern or validate function.

  • Observer pattern: shiny implements the observer pattern which is
    useful to act when an event happens in an object (it can be a click
    in a button, new value in an input...).

  • Validate function: the functionality of this process is similar to an
    if/else statement. Indeed, there is need what is the if to check the
    parameter, if the values are wrong, there will be an error message.

To know how to use observe pattern and the validate function, click on the previous link (in the Shiny website is everything explained).

Shiny responds to enter, a not-quite-working example

I think this will do what you need:

library(shiny)
library(shinydashboard)
library(tidyverse)

years = 1912:1971

ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(numericInput("start_year", "Enter", 1950)),
dashboardBody(
tags$script('
$(document).on("keyup", function(e) {
if(e.keyCode == 13){
Shiny.onInputChange("keyPressed", Math.random());
}
});
'),
box(plotOutput("temp_plot"))
)
)

server <- function(input, output) {

the_year <- reactiveVal()

observeEvent(input[["keyPressed"]], {
the_year(input[["start_year"]])
}, ignoreNULL = FALSE)

output$temp_plot <- renderPlot({
df <- tibble(nhtemp) %>%
mutate(year = years) %>%
filter(year > the_year())

ggplot(df, aes(x = year, y = nhtemp)) +
geom_line()
})
}

shinyApp(ui, server)

Using enter key with action button in R Shiny

I was able to figure this out using the jQuery is(":focus") function, the code I used was:

$(document).keyup(function(event) {
if ($("#number").is(":focus") && (event.key == "Enter")) {
$("#goButton").click();
}
});

Shiny: Using enter key with action button on login screen

For anyone else who stumbles upon this thread, this solution (unlike the accepted solution to the above-linked SO post Using enter key with action button in R Shiny) does not require an external js script file.

The js script should have been included inside the modalDialog() instead, and inside the HTML() function, as follows:

library(shiny)
library(shinydashboard)

Logged = FALSE
my_username <- "test"
my_password <- "test"

js <- '
$(document).keyup(function(event) {
if ($("#password").is(":focus") && (event.keyCode == 13)) {
$("#ok").click();
}
});
'

ui <- dashboardPage(skin = "blue",
dashboardHeader(title = "Dashboard"),
dashboardSidebar(),
dashboardBody("Test",
verbatimTextOutput("dataInfo")
)
)

server = function(input, output, session) {

values <- reactiveValues(authenticated = FALSE)

# Return the UI for a modal dialog with data selection input. If 'failed'
# is TRUE, then display a message that the previous value was invalid.
dataModal <- function(failed = FALSE) {
modalDialog(
tags$script(HTML(js)),
textInput("username", "Username:"),
passwordInput("password", "Password:"),
footer = tagList(
# modalButton("Cancel"),
actionButton("ok", "OK")
)
)
}

# Show modal when button is clicked.
# This `observe` is suspended only whith right user credential

obs1 <- observe({
showModal(dataModal())
})

# When OK button is pressed, attempt to authenticate. If successful,
# remove the modal.

obs2 <- observe({
req(input$ok)
isolate({
Username <- input$username
Password <- input$password
})
Id_username <- which(my_username == Username)
Id_password <- which(my_password == Password)
if (length(Id_username) > 0 & length(Id_password) > 0) {
if (Id_username == Id_password) {
Logged <<- TRUE
values$authenticated <- TRUE
obs1$suspend()
removeModal()

} else {
values$authenticated <- FALSE
}
}
})

output$dataInfo <- renderPrint({
if(values$authenticated){
"OK!!!!!"
} else {
"You are NOT authenticated"
}
})

}

shinyApp(ui,server)

Also, as a side note, I believe that the js script was originally inspired by this example.

Shiny: Action button is not working after clicking enter

You'll have to bind this manually. Try implementing the following, and replace InputId with the InputId for your actionButton.

$(document).keyup(function(event) {
if(event.keyCode == 13) {
$('#InputId').click();
}
});

Shiny: Getting a user input into a future function

I solved it. Not enterily sure why, but isolate does the trick.
This code works for me:

library(shiny)
library(promises)
library(future)

plan(multisession)

# example function
subfct = function(n) {
Sys.sleep(3)
return(n*2)
}

# shiny page
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
numericInput("mem_pos", min = 1, max = 30, value = 1, label="mem pos"),
actionButton("mem_button", label="set mem value")
),
mainPanel(
tableOutput("result")
)
)
)

server <- function(input, output) {
superval = reactiveValues(mem = rep(list(0), 10))

# set the future calculations
observeEvent(input$mem_button, {future({return(subfct( isolate(input$mem_pos) ))}) %...>% {superval$mem[[input$mem_pos]] = .}}) # here lied the problem

# show result table
observe( {output$result = renderTable({unlist(superval$mem)})})
}

# Run the application
shinyApp(ui = ui, server = server)

only one of the two actionButtons responds in shiny

The issue appears to be with the actionBttn being inside box(). It works just fine without. Any chance you could find another way to get the same style without box()?

Sample Image

R Shiny key input binding

You can add a listener for keypresses. The Shiny.onInputChange can be used to bind the key pressed to a shiny variable:

library(shiny)
runApp( list(ui = bootstrapPage(
verbatimTextOutput("results"),
tags$script('
$(document).on("keypress", function (e) {
Shiny.onInputChange("mydata", e.which);
});
')
)
, server = function(input, output, session) {

output$results = renderPrint({
input$mydata
})
}
))

for keydown events you can substitute:

  tags$script('
$(document).on("keydown", function (e) {
Shiny.onInputChange("mydata", e.which);
});
')


Related Topics



Leave a reply



Submit