RStudio中Shiny应用报错:external pointer is not valid 求助
问题:Shiny应用重启R后报「external pointer is not valid」错误
运行一款支持用户粘贴评论、生成名词与形容词共现网络的Shiny应用时,原本运行正常,但重启R后出现报错:
Warning: Error in : external pointer is not valid
已完成的排查项:
- 确认应用依赖的所有R包均已安装
- 重启过R及电脑
- 确认udpipe模型文件与应用源码在同一目录
错误原因分析
这个错误的核心原因是udpipe模型加载后生成的外部指针失效,常见触发场景:
- 模型加载路径问题:代码中使用了绝对路径,若重启后路径权限变化、文件路径实际变更(比如映射盘符变动),会导致模型加载失败,返回无效指针
- 重复加载模型:每次点击按钮都重新加载模型,容易引发外部指针的资源冲突或回收问题
- 预处理步骤冲突:
tm包的词干化(wordStem)会改变词形,可能导致udpipe无法正确完成词性标注,间接引发指针错误
解决方案
- 改用相对路径加载模型:如果模型文件确实在当前工作目录,替换绝对路径为相对路径,避免盘符/权限问题
- 全局加载模型:将模型加载移到server函数外部,只加载一次,避免重复加载导致的指针失效
- 调整预处理逻辑:将词干化步骤移到udpipe标注之后(基于lemma处理),避免破坏原始词形影响标注
修正后的完整代码
library(shiny) library(shinythemes) library(udpipe) library(igraph) library(ggraph) library(tm) library(SnowballC) library(DT) # 全局加载udpipe模型(只加载一次,避免指针问题) # 若模型在当前工作目录,使用相对路径;否则确保绝对路径正确 ud_model <- udpipe_load_model(file = "english-ewt-ud-2.5-191206.udpipe") # 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:调整顺序,先保留原始词形用于udpipe标注 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 }) # Combine preprocessed comments into a single text combined_comments <- paste(preprocessed_comments, collapse = " ") # Perform text annotation:使用全局加载的模型 x <- udpipe_annotate(ud_model, x = combined_comments) x <- as.data.frame(x) # 基于udpipe的lemma进行词干化(可选,若不需要可删除) x$lemma <- wordStem(x$lemma) # 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)
内容的提问来源于stack exchange,提问作者Seth Seefeldt
相关产品推荐
相关产品推荐

