2016-01-04 17 views
5

Tôi đang gặp một số sự cố khi sử dụng nhiều nút hành động trong sáng bóng. Tôi đã xây dựng một vùng văn bản nơi văn bản có thể được chèn vào. Văn bản này được xử lý sao cho ba chuỗi là kết quả. Ba chuỗi này sau đó được tạo thành nhãn của ba nút hành động. Khi một trong các nút được bấm, nó sẽ thao tác với văn bản đầu vào.Yêu cầu đặt lại nút hành động (hoặc thay thế)

Khi tôi nhấp vào nút hành động, văn bản được xử lý chính xác, nhưng hành động được lặp lại không chính xác. Điều này là do nút tác vụ không thể được đặt lại. Tôi đã tìm thấy nhiều trang web đối phó với vấn đề này và tôi đã thử nhiều giải pháp và cách giải quyết, nhưng không có gì có vẻ hoạt động. Tôi đã trình bày đoạn code dưới đây:

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") 

      ) 
     ) 
) 
) 

Trả lời

2

Vấn đề là trong renderUI bạn sử dụng trong observeEvent có một sự phụ thuộc trên input$text_in thông qua hàm new_word()input$text_in trong đối số thứ hai. Vì vậy, mỗi khi văn bản thay đổi, renderUI sẽ được gọi lại, đó là lý do tại sao hành động được lặp lại vô thời hạn.

Hãy thử sử dụng cô lập để loại bỏ những phụ thuộc, ví dụ:

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]))}) 
}) 
+0

này hoạt động! Tôi đã áp dụng cô lập theo 10000 cách khác nhau, nhưng không phải theo cách này có vẻ như .. Cảm ơn bạn! – Maarten

Các vấn đề liên quan