Reputation: 43
I'm working with a Shiny project in which I would like to take text rendered inside the Shiny app and make each word in that text a hyperlink that when clicked sends the clicked text as an input to another function inside the app.
Here is a toy example of what I'm working with so far which only attempts to get the hyperlinked words to trigger an reactiveEvent
and render the text that was clicked. Say my rendered text is in data
. I markup the text in data
and output the results as HTML to markup
. Ideally what would happen is when I click the hyperlinked word 'This', the clicked_text
output would display the text 'This'.
The actionLink syntax I'm using for the markup has two problems: 1) only the first hyperlinked word triggers input$clicked_text
and 2) the value of input$clicked_text
is an integer registering how many times the hyperlink as been clicked.
library(shiny)
ui <- fluidPage(
htmlOutput("markup"),
textOutput("clicked_text")
)
# sample data
data <- c("This", "is", "a", "sentence")
# function to markup text
markup.text <- function(data) {
markup <- lapply(data, function(text) {
paste0("<a id = 'clicked_text' href = '#' class = 'action-button shiny-bound-input' value = '",
text,
"'>",
text,
"</a>")
}) %>%
paste(collapse = " ")
return(markup)
}
server <- function(input, output) {
output$markup <- renderUI({ # display the marked-up text
HTML(markup.text(data))
})
output$clicked_text <- eventReactive(input$clicked_text, { # display clicked text output
input$clicked_text
})
}
shinyApp(ui = ui, server = server)
Your help and suggestions are highly appreciated.
Upvotes: 1
Views: 1102
Reputation: 2508
I think these revisions should do the trick:
<a href='#' onclick='detect_click(this)'>text</a>
function detect_click(el) {
Shiny.onInputChange('clicked_text', el.innerHTML);
}
ui <- fluidPage(
htmlOutput("markup"),
textOutput("clicked_text"),
includeScript("detect_click.js")
)
Upvotes: 3