首页 > 解决方案 > Shiny 无法正确加载 RDS 对象----警告:predict_sbo_predictor 中的错误:外部指针无效?

问题描述

基于下面描述的问题,我有一个两部分的问题:

问题:

我是否正确保存了“corpus_train”SBO 对象以用于已部署的 Shiny 应用程序?

如何确保 Shiny 能够正确读取 RDS 文件,让应用程序完全部署并提供结果?

问题描述: 我为一个使用SBO 预测器对象作为预测结果源的类创建了一个非常基本的单词预测 APP。该 SBO 文件名为“corpus_train”,并在 RStudio 环境中列为“类 'sbo_predictor' 的外部指针。

APP 在本地按预期工作。但是,当我部署到 Shiny 时,我收到以下日志错误:“警告:predict_sbo_predictor 中的错误:外部指针无效”

这是我用来部署到 Shiny 的过程:为了避免上传非常大的原始 txt 文件,其中“corpus_train”预测器是其中的一个子集,我执行了以下操作:

  1. 使用以下代码保存“corpus_train”: saveRDS(corpus_train, file = "corpus_train_app.RDS")
  2. 将此代码添加到 global.R 文件中:corpus_train <- readRDS("./corpus_train_app.RDS")
  3. 将 RDS 文件与 global.R、server.R 和 ui.R 文件一起上传。

我在下面包含了闪亮的代码。并且,创建了一个 github来存放原始文件和所有其他文件,包括“corpus_train_app.RDS”文件。

全局.R代码

library(sbo) library(shiny) corpus_train <- readRDS("./corpus_train_app.RDS")

服务器.R代码

shinyserver <- function(input, output) {
    
    output$result_output <-renderText({
            predict(corpus_train,input$text)
    })

}

ui.R 代码

shinyUI(pageWithSidebar(
    
    headerPanel("Predicitve Text APP"),
    sidebarPanel(
            textInput("text", label = h3("Text input"), value = "Enter text..."),
            
            
            
    ),
    
    mainPanel(
            h4("Predicted Words:"),
            verbatimTextOutput("result_output"),

            
            
            h6("This APP generates three predicted next words based on the text you input. 
               The prediction algorithm relies on word frequencies in the English twitter, 
               blogs, and news datasets at:"),
            h6(a("http://www.corpora.heliohost.org/")),
            br(),
            h6("Created May 2021 as part of my Captsone project for the 
            Data Science Specialization provided by Johns Hopkins University and Coursera.
            All code can be located on GitHub at:") ,
            h6(a("https://github.com/themonk99/predictive.git"))
            
    )
    
    

))

提前感谢您的时间和反馈!两者都非常感谢-

标签: rshiny

解决方案


导致错误“警告:predict_sbo_predictor 中的错误:外部指针无效?”的问题 是双重的,我在这里找到了解决方案,在 SBO 包的文档中:SBO 包文档。有关更多详细信息/示例,请参阅“内存不足”部分。

  1. 用于预测下一个单词的语料库应该首先使用 sbo_predtable 函数创建。

    corpus_train <- sbo_predtable(object = combine_sample_final, N = 3, dict = target ~ 0.75, .preprocess = sbo::preprocess, EOS = ".?!:;", lambda = 0.4, L = 3L, filtered = "" )

  2. 然后 sbo.predtable 对象应保存为 .rda 对象

    save(corpus_train, file = "corpus_train_save.rda")

  3. 然后,在 global.R 文件上,加载 .rda 文件,然后使用 sbvo_predictor 函数识别预测器。

    load("corpus_train.rda")

    corpus_train <- sbo_predictor(corpus_train)

然后应用程序部署没有问题。


推荐阅读