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)