Actionbutton reset necessary (or alternative)

I'm having trouble using multiple action buttons in brilliant state. I built a text box where the text can be inserted. This text is managed in such a way that the result is three lines. These three lines then form the label of the three action buttons. When one of the buttons is pressed, it should manipulate the input text.

When I click the action button, the text is processed correctly, but the action is repeated indefinitely. This is because the action button cannot be reset. I found several web pages dedicated to this problem, and I tried several solutions and workarounds, but nothing works. I presented the following code:

server.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]))})}) }) 

ui.R

 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") ) ) ) ) 
+5
source share
1 answer

The problem is that in the renderUI that you use in observeEvent , there is a dependency on input$text_in via the new_word() function and input$text_in in the second argument. Therefore, every time the text changes, renderUI will be called again, so the action repeats endlessly.

Try using isolation to remove these dependencies, for example:

 observeEvent(input$action1, {output$input_textarea <- renderUI({ tags$textarea(id="text_in", rows=3, cols=40, modify_text_input(isolate(new_word()),isolate(input$text_in),ngram_input,answer[1]))}) }) 
+2
source

Source: https://habr.com/ru/post/1239751/


All Articles