我在使用闪亮的多个动作按钮时遇到了一些麻烦。我已经构建了一个可以插入文本的文本区域。该文本经过处理,结果是三个字符串。然后将这三个字符串作为三个动作按钮的标签。单击其中一个按钮时,它应该操纵输入文本。
当我单击动作按钮时,文本被正确操作,但动作无限重复。这是因为无法重置操作按钮。我找到了多个处理此问题的网页,并且尝试了多种解决方案和变通方法,但似乎没有任何效果。我已经提供了以下代码:
服务器.R
library(shiny)
library(stringi)
new_word_f <- function(x) {
x <- substr(x, nchar(x), nchar(x)) == " "
}
modify_text_input <- function(new_word, input_text, word_to_remove, answer) {
if (new_word == TRUE) {
paste(input_text, answer, " ")
} else {
paste(stri_replace_last_regex(input_text, word_to_remove, answer), " ")
}
}
start_input_text <- "Testing the lines "
ngram_input <- "lines"
answer <- c("a", "b", "c")
## Start shiny app
shinyServer(function(input, output) {
## New word or current mid-word
new_word <- reactive({new_word_f(input$text_in)})
output$input_textarea <- renderUI({tags$textarea(id="text_in", rows=3, cols=40, start_input_text)})
output$text1 <- renderText({input$text_in})
output$text2 <- renderText({new_word()})
output$but1 <- renderUI({actionButton("action1", label = answer[1])})
output$but2 <- renderUI({actionButton("action2", label = answer[2])})
output$but3 <- renderUI({actionButton("action3", label = answer[3])})
## On button press
observeEvent(input$action1, {output$input_textarea <- renderUI({tags$textarea(id="text_in", rows=3, cols=40, modify_text_input(new_word(), input$text_in, ngram_input, answer[1]))})})
observeEvent(input$action2, {output$input_textarea <- renderUI({tags$textarea(id="text_in", rows=3, cols=40, modify_text_input(new_word(), input$text_in, ngram_input, answer[2]))})})
observeEvent(input$action3, {output$input_textarea <- renderUI({tags$textarea(id="text_in", rows=3, cols=40, modify_text_input(new_word(), input$text_in, ngram_input, answer[3]))})})
})
用户界面
library(shiny)
library(stringi)
shinyUI(
fluidPage(
titlePanel("Word prediction"),
sidebarLayout(
sidebarPanel(
uiOutput("input_textarea"),
uiOutput("but1"),
uiOutput("but2"),
uiOutput("but3")
),
mainPanel(
textOutput("text1"),
textOutput("text2")
)
)
)
)