मैं एक छोटा ऐप बना रहा हूं जहां उपयोगकर्ता कहानी बनाने के लिए शब्दों और डेटा को जोड़ सकता है। मैं चयनित टेक्स्ट को textInput()
फ़ील्ड में ढूंढने में सक्षम होना चाहता हूं और इसे टेक्स्ट में किसी भी स्थान पर radioGroupButton()
के चयन के साथ बदलना चाहता हूं। वास्तविक एप्लिकेशन में, कोई भी "____" अनुभाग नहीं होता है और उपयोगकर्ता किसी भी हाइलाइट किए गए टेक्स्ट को बदलने में सक्षम होना चाहिए या कुछ भी नहीं चुने जाने पर अंत में संलग्न करना चाहिए।
मैंने action_script
कोड को यह पोस्ट लेकिन यह पता नहीं लगा सका कि observeEvent()
में जानकारी को वापस कैसे शामिल किया जाए
library(shiny)
library(shinyWidgets)
replacement_location <-
'Shiny.addCustomMessageHandler("selected_text", function(NULL) {
var ctl = document.getElementById("phrase");
var startPos = ctl.selectionStart;
var endPos = ctl.selectionEnd;
alert(startPos + ", " + endPos);
});'
ui <-
fluidPage(
tags$head(tags$script(replacement_location)),
textAreaInput(
inputId = "phrase",
label = "I want to replace the selected text",
value = "Starts on ___ and ends on ___ of next week."
),
radioGroupButtons(
"wday",
NULL,
c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday"),
selected = ""
)
)
server <- function(input, output, session) {
observeEvent(input$wday, {
loc_start <- 10
loc_end <- 14
new_phrase <-
paste0(
substr(input$phrase, 1, loc_start),
input$wday,
substr(input$phrase, loc_end, nchar(input$phrase))
)
updateTextInput(
session = session,
"phrase",
value = new_phrase
)
session$sendCustomMessage(type = "selected_text", message = list(NULL))
})
}
shinyApp(ui, server)
0
yake84
29 नवम्बर 2020, 07:42
1 उत्तर
सबसे बढ़िया उत्तर
यह समाधान मेरे लिए काम कर गया। मूल कोड से कुछ बदलाव हैं:
Shiny.setInputValue()
के माध्यम से श्रेणियां प्राप्त करने के लिए स्क्रिप्ट जोड़ेंonmouseup
औरonkeyup
इवेंट के लिएtagAppendAttributes()
का इस्तेमाल करें- चयन परिवर्तन के रूप में परिवर्तन देखें
textinput
ऑब्जेक्ट को अपडेट करें (input$phrase
)
library(shiny)
library(shinyWidgets)
get_replacement_location <- # (1)
"Shiny.setInputValue(
'selected_text', [
document.getElementById('phrase').selectionStart,
document.getElementById('phrase').selectionEnd
]
);"
ui <-
fluidPage(
textAreaInput(
inputId = "phrase",
label = "I want to replace the selected text",
value = "Starts on ___ and ends on ___ of next week."
) %>%
# add event triggers # (2)
tagAppendAttributes( #
onmouseup = get_replacement_location, #
onkeyup = get_replacement_location #
), #
radioGroupButtons(
"wday",
NULL,
c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday"),
selected = ""
)
)
server <- function(input, output, session) {
# view in console as selected text changes # (3)
observeEvent(input$selected_text, print(input$selected_text)) #
# use info to update the textInput object when buttons are pressed
observeEvent(input$wday, {
# now have access to input # (4)
loc_start <- input$selected_text[1] #
loc_end <- input$selected_text[2] #
# add 1 for string splitting #
if (loc_start != loc_end) loc_end <- input$selected_text[2] + 1 #
new_phrase <-
paste0(
substr(input$phrase, 0, loc_start),
input$wday,
substr(input$phrase, loc_end, nchar(input$phrase))
)
updateTextInput(
session = session,
inputId = "phrase",
value = new_phrase
)
})
}
shinyApp(ui, server)
0
yake84
4 जिंदा 2021, 06:57