https://github.com/shinyworks/shinyfocus
Use Browser Focus Events in 'shiny'
https://github.com/shinyworks/shinyfocus
Last synced: 4 months ago
JSON representation
Use Browser Focus Events in 'shiny'
- Host: GitHub
- URL: https://github.com/shinyworks/shinyfocus
- Owner: shinyworks
- License: other
- Created: 2023-01-16T18:43:48.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-14T16:07:08.000Z (over 1 year ago)
- Last Synced: 2024-08-13T07:11:10.440Z (8 months ago)
- Language: R
- Homepage: https://shinyworks.github.io/shinyfocus/
- Size: 413 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.Rmd
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Support: .github/SUPPORT.md
Awesome Lists containing this project
- jimsghstars - shinyworks/shinyfocus - Use Browser Focus Events in 'shiny' (R)
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```# shinyfocus
[](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[](https://CRAN.R-project.org/package=shinyfocus)
[](https://app.codecov.io/gh/shinyworks/shinyfocus?branch=main)
[](https://github.com/shinyworks/shinyfocus/actions/workflows/R-CMD-check.yaml)The goal of shinyfocus is to make it easy to trigger server events in {shiny} apps based on elements of the app gaining or losing focus in the browser.
## Installation
You can install the development version of shinyfocus from [GitHub](https://github.com/) with:
``` r
# install.packages("pak")
pak::pak("shinyworks/shinyfocus")
```## Examples
I've envisioned two primary use cases (although I hope and assume that there are many others):
- Showing help related to an input.
- Validating an input when the user is "done" with an input.The app demonstrated here does both.
``` r
library(shinyfocus)ui <- shiny::fluidPage(
shinyfocus_js_dependency(),
shiny::column(
2,
shiny::textInput("name", "Name:"),
shiny::textInput("title", "Title:")
),
shiny::column(
2,
shiny::textOutput("explanation")
)
)server <- function(input, output, session) {
on_focus(
"name",
output$explanation <- shiny::renderText({
"Enter the name you want me to call you. It will be converted to Title Case."
})
)
on_focus(
"title",
output$explanation <- shiny::renderText({
"Describe your role in 10 characters or fewer."
})
)
on_blur(
"name",
shiny::updateTextInput(
inputId = "name",
value = stringr::str_to_title(shiny::isolate(input$name))
)
)
on_blur(
"title",
{
if (nchar(input$title) > 10) {
shiny::updateTextInput(
inputId = "title",
value = paste(
"Typer of",
nchar(input$title),
"Characters"
)
)
}
}
)
}shiny::shinyApp(ui, server)
```Note: I feel like the "explanation" output could be cleaner.
I'd like to find a better way to implement the "explanatin" output, having it switch based on which input is selected (rather than the roundabout double observer).
I hope to update that soon!## Related Work
- {[shinyjs](https://deanattali.com/shinyjs/)} has a function, `onevent()`, that can be used for similar functionality.
However, that implementation is different than what we do here.## Code of Conduct
Please note that the shinyfocus project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/1/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.