0

When I run this app in RStudio, I get this error: Warning: Error in : external pointer is not valid. This app allows the user to paste comments into a text box input and it creates a word network of nouns and adjectives. The code was running perfectly at first, but after restarting R, now I get this issue.

I confirmed all libraries used in the app are installed. I restarted R and my computer. I also confirmed the correct udpipe file is in the same folder as the app's source code.

Here is my full code:

library(shiny)
library(shinythemes)
library(udpipe)
library(igraph)
library(ggraph)
library(tm)
library(SnowballC)
library(DT)

# Define the UI
ui <- fluidPage(
  theme = shinytheme("flatly"),
  
  titlePanel("Co-occurrence Network Map"),
  
  sidebarLayout(
    sidebarPanel(
      textAreaInput("comments", "Paste comments here", rows = 10),
      sliderInput("word_distance", "Word Distance", min = 0, max = 5, value = 3),
      sliderInput("head_words", "Number of Words", min = 10, max = 50, value = 30),
      actionButton("create_map", "Create Network Map")
    ),
    mainPanel(
      plotOutput("network_plot"),
      br(),
      dataTableOutput("comments_table")
    )
  )
)

# Define the server logic
server <- function(input, output) {
  network <- reactiveVal(NULL)
  comments_data <- reactiveVal(NULL)
  
  observeEvent(input$create_map, {
    req(input$comments)
    
    # Preprocessing
    comments <- strsplit(input$comments, "\n")[[1]]
    preprocessed_comments <- lapply(comments, function(comment) {
      comment <- removeNumbers(comment)
      comment <- removePunctuation(comment)
      comment <- tolower(comment)
      comment <- removeWords(comment, stopwords("english"))
      comment <- stripWhitespace(comment)
      comment <- wordStem(comment)
      comment
    })
    
    # Combine preprocessed comments into a single text
    combined_comments <- paste(preprocessed_comments, collapse = " ")
    
    # Load the language model
    ud_model <- udpipe_load_model(file = "R:/Marketing/Market Research/2023 Projects/Seth 2023/Open Ended Questions Text Analytics/WordCooccurenceMap/english-ewt-ud-2.5-191206.udpipe")
    
    # Perform text annotation
    x <- udpipe_annotate(ud_model, x = combined_comments)
    x <- as.data.frame(x)
    
    # Co-occurrence analysis
    stats <- cooccurrence(x = x$lemma, relevant = x$upos %in% c("NOUN", "ADJ"), skipgram = input$word_distance)
    
    # Create the network graph
    wordnetwork <- head(stats, input$head_words)  # Limit to user-selected number of words
    wordnetwork <- graph_from_data_frame(wordnetwork)
    network(wordnetwork)
    
    # Store comments data
    comments_data(data.frame(Comments = comments))
  })
  
  output$network_plot <- renderPlot({
    graph <- network()
    
    if (!is.null(graph)) {
      ggraph(graph, layout = "fr") +
        geom_edge_link(aes(width = cooc, edge_alpha = cooc), edge_colour = "pink", size = 1.5) +
        geom_node_text(aes(label = name), col = "darkgreen", size = 8) +
        theme_graph(base_family = "Arial Narrow") +
        theme(legend.position = "none") +
        labs(title = "Cooccurrences within Word Distance",
             subtitle = paste("Nouns and Adjectives"))
    }
  })
  
  output$comments_table <- renderDataTable({
    comments <- comments_data()
    if (!is.null(comments)) {
      datatable(comments, options = list(scrollX = TRUE))
    }
  })
}

# Run the application
shinyApp(ui = ui, server = server)
  • Welcome to stack overflow. Does the error occur when you initially run the app or when you take a certain action? – Paul Stafford Allen Jul 14 '23 at 12:32
  • @PaulStaffordAllen It occurs when I click "Generate Network Map" not when it launches – Seth Seefeldt Jul 14 '23 at 12:35
  • It may be the line starting `ud_model <- ...` - you have provided an explicit local path - see here: https://stackoverflow.com/a/32239230/16730940 – Paul Stafford Allen Jul 14 '23 at 12:35
  • @PaulStaffordAllen Oh so the working directory needs to be set for the apps location? I will see if that is the issue – Seth Seefeldt Jul 14 '23 at 12:38
  • @PaulStaffordAllen that didn't seem to do the trick. I ran the app with the wd set to my folder that the app is in. – Seth Seefeldt Jul 14 '23 at 12:41
  • I think it's worth checking into and is generally good practice if you plan to distribute the app – Paul Stafford Allen Jul 14 '23 at 12:41
  • @PaulStaffordAllen That's good to know for the future. It's still giving me the error. It's weird because I used this same udfile in another program and it works perfect. – Seth Seefeldt Jul 14 '23 at 12:48
  • @PaulStaffordAllen Also, I forgot this message also appears. heres the whole output: Listening on http://127.0.0.1:7526 This looks like you restarted your R session which has invalidated the model object, trying now to reload the model again from the file at english-ewt-ud-2.5-191206.udpipe in order to do the annotation. Warning: Error in : external pointer is not valid 1: runApp – Seth Seefeldt Jul 14 '23 at 12:53
  • Seems to be an issue with udpipe - see the discussion here: https://stackoverflow.com/questions/67409044/problems-with-udpipe-models – Paul Stafford Allen Jul 14 '23 at 12:57
  • 1
    @PaulStaffordAllen You were correct. The udpipe file in the app folder was incomplete. It was only 1/3 the size of the full file in my other app. I replaced it and now its working fine. Thank you for the help! – Seth Seefeldt Jul 14 '23 at 13:11

0 Answers0