Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reactive Value in recogito() does not trigger re-render, which causes annotations to fail #15

Closed
datapumpernickel opened this issue Aug 8, 2022 · 6 comments

Comments

@datapumpernickel
Copy link

datapumpernickel commented Aug 8, 2022

Hi,

thank you so much for this great package! I have been trying to build a little annotation tool in shiny that would allow me to have some co-workers create a more standardized annotation process for documents.

Something I am struggling with, is to include a sort of "next" button. It is difficult to build a nice repex, because there is an underlying API that serves the paragraphs to be coded.

Essentially I have built a "load text" button, which calls a new text to be loaded into the recogito rendering. the "Save" button then sends the finished annotations back to the API. I would expect recogito to re-render, whenever I call the "load" button, because of the eventReactive way and the fact that text() is called in the renderRecogito part, linking them. It does load a new text, but sometimes the linkages from the last text are still displayed and it does not properly see annotations anymore.

Do you have any idea how I could force the renderRecogito to kind of like "restart" after loading a new text?

Currently, the only thing that helps is a refresh with F5.

I hope this is remotely understandeable.

library(shiny)
library(recogito)
library(httr)
library(rvest)
library(tidyverse)
library(glue)
library(jsonlite)


empty_results <-
    tibble(
        id = NA_character_,
        type = NA_character_,
        label = "none",
        chunk_text     = NA_character_ ,
        chunk_start    = NA_character_  ,
        chunk_end      = NA_character_  ,
        relation_from  = NA_character_  ,
        relation_to    = NA_character_  ,
        chunk_comment = "none"    
    )


get_one_text <- function() {
    id <-
        GET("http://127.0.0.1:4207/list_id") %>% content() %>% unlist()
    
    params = list(`file_id` = id[1],
                  `para_id` = id[2])
    
    text_data <-
        httr::POST(url = 'http://127.0.0.1:4207/get_text', query = params) %>% content(as = "text") %>% fromJSON()
    return(text_data)
}


post_one_result <- function(result_data) {
    body <- toJSON(result_data)
    
    POST("http://127.0.0.1:4207/post_result",
         body = body)
}


ui <- fluidPage(
                tags$br(),
                recogitoOutput(outputId = "annotation_text"),
                
                actionButton("load_text", "Load data"),
                actionButton("save_text", "Save"))


server <- function(input, output) {
    
    text <- eventReactive(input$load_text, {
        get_one_text()
    })
    
    output$annotation_text <- renderRecogito({
        recogito(inputId = "annotations", text = text()$text_paragraphs, tags = c("LOCATION", "TIME", "ORIGIN","DESTINATION"))
    })

    observeEvent(input$save_text, {
        if(nrow(read_recogito(input$annotations))==0){
            post_one_result(empty_results %>% mutate(filename_safe = text()$filename_safe[1]))
        } else {
            print(read_recogito(input$annotations) %>% mutate(filename_safe = text()$filename_safe[1]))
            post_one_result(read_recogito(input$annotations) %>% mutate(filename_safe = text()$filename_safe[1]))
        }
    })

}

shinyApp(ui, server, options = list(port = 3838))
@jwijffels
Copy link
Member

jwijffels commented Aug 8, 2022

Which version of the recogito R package are you using? The one released today on cran which is version 0.1.2 or an older one? Please use at least version 0 1.2 which is put today on cran

@datapumpernickel
Copy link
Author

Mea culpa. I just updated the version and voilá - it works!
Thanks!

@jwijffels
Copy link
Member

I've only been testing this in the following setting (only tagging, no relations)

library(shiny)
library(recogito)
data(brussels_reviews, package = "udpipe")
texts <- data.frame(doc_id = brussels_reviews$id,
                    text = brussels_reviews$feedback,
                    stringsAsFactors = FALSE)

ui <- fluidPage(
  tags$br(),
  actionButton(inputId = "ui_next", label = "Next"),
  tags$hr(),
  recogitotagsonlyOutput(outputId = "annotation_text"),
  tags$hr(),
  tags$h3("Results"),
  verbatimTextOutput(outputId = "annotation_result"))
server <- function(input, output) {
  current_text <- reactiveValues(id = 0,
                                 text = "Hello there, here is some text to annotate\nCan you identify the locations, persons and timepoints.\nJosh went to the bakery in Brussels.\nWhat an adventure!")
  observeEvent(input$ui_next, {
    ## Put here your save calls (as in save to a database or file or API)
    ## Go to the next text
    i <- sample(seq_len(nrow(texts)), size = 1)
    current_text$id   <- texts$doc_id[i]
    current_text$text <- texts$text[i]
  })
  output$annotation_text <- renderRecogito({
    recogito("annotations", text = current_text$text, tags = c("LOCATION", "TIME", "PERSON"), type = "tags")
  })
  output$annotation_result <- renderPrint(read_recogito(input$annotations))
}
shinyApp(ui, server)

@jwijffels
Copy link
Member

jwijffels commented Aug 9, 2022

Feel free to open up new issues. I mainly use the package for tagging text (relations has not been tested that much), and for the image annotations which have been tested more.
This pull request tries to add more options #13 but in the release of yesterday on CRAN (0.1.2) the annotations are cleared when new text is passed in renderRecogito

@datapumpernickel
Copy link
Author

Thanks again, also for the "next" button example. Looks less convoluted then the load/save logic. I will let you know, if I find anything weird with the relations. Although it does work perfectly well now.

@jwijffels
Copy link
Member

I see there was still a bug if you are using recogitoOutput together with recogito(type = "relations"), the button on top is not correctly updated, making it sometimes irresponsive. I've made fixes for this in 786212a and 434412d so if you need the relations, you should install the newest package from github, otherwise you can just use the cran version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants