【问题标题】:Shiny app produces error: "arguments imply differing number of rows: 0, 1"闪亮的应用程序产生错误:“参数暗示不同的行数:0、1”
【发布时间】:2021-11-25 21:35:05
【问题描述】:

我正在尝试创建一个 R Shiny 应用程序,该应用程序使用岭回归计算分数,然后在随机森林模型中使用该分数。我将两个模型都保存为 RDS,并将它们保存在 app.R 所在的同一文件夹中。

然后我读取预测概率的模型和数据并定义一些函数:

    # Reading data
    phats <- read.csv("Predicted_probabilities_training.csv")
    phats_graph <- phats %>% mutate(`PTD Event Status` = ifelse(Observed_Event=="PTD", "PTD Event", "No PTD Event"))
    phats_event <- phats[phats$Observed_Event=="PTD",]
    phats_nonevent <- phats[phats$Observed_Event=="No_PTD",]
            
    # load the models
    ridge_model <- readRDS("ridge_model.rds")
    final_model <- readRDS("final_model.rds")

        # Defining some functions
matrix.for.ILS.function <- function(input){
  return(data.matrix(data.frame(input$MIP_1a,
               input$MIP_3a,
               input$RANTES,
               input$sIL_6R,
               input$ITAC,
               input$IL_21,
               input$Fractalkine,
               input$TNF_a,
               input$IL_1b,
               input$IL_7,
               input$IL_10,
               input$GM_CSF,
               input$MIP_1b)))
}

ILS.function <- function(input){
  ILS.ridge = as.numeric(predict(ridge_model, matrix.for.ILS.function(input), s = 0.7414409, type="link"))
  return(ILS.ridge)
}

test.data.function <- function(input){
  test.data = data.frame(Age=input$Age,
                  Gender=input$Gender,
                  GCS_Bestin24=input$GCS_Bestin24,
                  Premorbid_depression=input$Premorbid_depression,
                  Antidep_first6m=input$Antidep_first6m)
  return(cbind(test.data, ILS.ridge=ILS.function(input)))
}

pred_prob_func <- function(input){
  pred_prob = predict(final_model, test.data.function(input), type="prob")[,"PTD"]
  classification <- ifelse(pred_prob >= input$thresholdslider, "PTD Event", "No Event")
  return(list(pred_prob=pred_prob, classification=classification))
}

然后在编写 UI 和服务器代码后,我尝试生成一个图表,但它显示错误:“参数意味着不同的行数:0、1”。我测试了pred_prob_func 会产生该错误。虽然当我尝试通过创建变量的随机值列表并尝试查看pred_prob_func(input) 是否产生一个值来创建一个名为input 的列表时,它实际上会产生一个值。但是当我尝试运行该应用程序时,该图没有显示并给我这个错误。我在服务器部分使用以下代码来生成图表。

  output$int_plot <- renderPlotly({

    phats_graph$`Total Percentile` <- unlist(t(sapply(phats_graph$Phat, function(x) quantile_fun(value=x)))[,1])
    phats_graph$`PTD Percentile` <- unlist(t(sapply(phats_graph$Phat, function(x) quantile_fun(value=x)))[,2])
    phats_graph$`No PTD Percentile` <- unlist(t(sapply(phats_graph$Phat, function(x) quantile_fun(value=x)))[,3])

    int_plot <- ggplot(phats_graph, aes(ptd_per=`No PTD Percentile`)) + geom_density(aes(x=Phat, fill = `PTD Event Status`), alpha=0.5) +
      geom_vline(xintercept = input$thresholdslider, linetype = 'dashed') +
      geom_vline(xintercept = pred_prob_func(input)$pred_prob) +
      xlab('Threshold Percentage') + ylab('Density') +
      theme_minimal() + scale_fill_manual(values=c("#5D3A9B", "#E66100"), name="")


    ggplotly(int_plot, tooltip=c("x", "ptd_per"))

  })

我如何定义函数有问题吗?

编辑:

所有文件(数据、RDS 模型)和应用程序都可以在此Google Drive 中找到以进行测试。

我已经使用以下示例输入测试了函数(当我在函数中使用 this 作为参数时会产生结果,但在尝试运行应用程序时会显示此错误):

input=list(Age=20, Gender=1, GCS_Bestin24=4, Premorbid_depression=0, Antidep_first6m=0,
           MIP_1a=1,
           MIP_3a=1,
           RANTES=1,
           sIL_6R=1,
           ITAC=1,
           IL_21=1,
           Fractalkine=1,
           TNF_a=1,
           IL_1b=1,
           IL_7=1,
           IL_10=1,
           GM_CSF=1,
           MIP_1b=1,
           thresholdslider=0.5)

【问题讨论】:

  • 如果我可以提供更多信息,请告诉我,我可以编辑问题。
  • 您是否尝试确定错误发生的确切位置?我认为它可能在test.data.functioncbind 函数中。您应该在此函数中添加一些 print(input$Age) 等...以确保您的输入确实具有值。
  • @gdevaux 我已经使用编辑中的输入测试了函数。

标签: r shiny r-caret caret glmnet


【解决方案1】:

发生原始错误是因为您要求的 input$id 与您在 textInput 中分配的 id 不同。

textInput("MIPa", label = HTML("MIP-1\\(\\alpha\\)") ##used in ui

input$MIP_1a ##used in server.

我认为使用 input 变量的闪亮存在问题。

通过将输入列表创建为input_list,并将您自己调用input 的位置更改为input(),它似乎可以工作。

我还需要在创建 test.data.function() 函数后立即删除 test.data.function(input) 以使应用程序运行。

    input_list <- reactive({
    input_list <- list( Age=as.numeric(input$Age),
                Gender=ifelse(input$Gender == "Female", 1, 0), 
                GCS_Bestin24=as.numeric(input$GCS_Bestin24),
             Premorbid_depression=as.numeric(input$Premorbid_depression),
                Antidep_first6m=as.numeric(input$Antidep_first6m),
                MIP_1a = as.numeric(input$MIPa),
                MIP_3a = as.numeric(input$MIP_3a),
                RANTES = as.numeric(input$RANTES),
                sIL_6R = as.numeric(input$sIL_6R),
                ITAC = as.numeric(input$ITAC),
                IL_21 = as.numeric(input$IL_21),
                Fractalkine = as.numeric(input$Fractalkine),
                TNF_a = as.numeric(input$TNF_a),
                IL_1b = as.numeric(input$ILb),
                IL_7 = as.numeric(input$IL_7),
                IL_10 = as.numeric(input$IL0),
                GM_CSF = as.numeric(input$GM_CSF),
                MIP_1b = as.numeric(input$MIPb))
    input_list
  })
##input changed to input_list()
##this needs to be done for each instance where input is used on its own.
geom_vline(xintercept = pred_prob_func(input_list())$pred_prob)  

要删除最后的错误消息,您需要将renderPlotly() 更改为renderUI(),并在input_list 中检查NA 值。

plotlyOutput 也需要在ui 部分更改为uiOutput()

ui <- fluidPage(
                uiOutput('int_plot') #changed from plotlyOutput('int_plot')
               )

server <- (
output$int_plot <- renderUI({
    if(anyNA(input_list())){
     helpText("Input User data")
    }else{
      phats_graph$`Total Percentile` <- unlist(t(sapply(phats_graph$Phat, function(x) quantile_fun(value=x)))[,1])
      phats_graph$`PTD Percentile` <- unlist(t(sapply(phats_graph$Phat, function(x) quantile_fun(value=x)))[,2])
      phats_graph$`No PTD Percentile` <- unlist(t(sapply(phats_graph$Phat, function(x) quantile_fun(value=x)))[,3])
      
      
      
      int_plot <- ggplot(phats_graph, aes(ptd_per=`No PTD Percentile`)) + geom_density(aes(x=Phat, fill = `PTD Event Status`), alpha=0.5) +
        geom_vline(xintercept = input$thresholdslider, linetype = 'dashed') +
        geom_vline(xintercept = pred_prob_func(input_list())$pred_prob) +
        xlab('Threshold Percentage') + ylab('Density') +
        theme_minimal() + scale_fill_manual(values=c("#5D3A9B", "#E66100"), name="")
      
      
      renderPlotly( ggplotly(int_plot, tooltip=c("x", "ptd_per")))
    }


  })
)

当年龄是 10 并且所有值都是 1(不知道这些应该是什么)时,这是输出:

【讨论】:

  • 感谢您的回答!我编辑了我的代码,但仍然收到一条错误消息,提示“变量‘年龄’、‘性别’、‘Premorbid_depression’、‘Antidep_first6m’被指定为不同的类型”。我检查了在运行随机森林模型以获得“final_model.RDS”的原始数据中,所有变量都是整数(其中一些是二进制(0/1)。我应该如何修复它以生成图表?应用程序是否在您尝试时生成了图表?
  • 我已将input_list 更新为可在提供数据时使用的版本。如果您将每个input$variable 更改为numericas.numeric(),它将返回正确的结果。您将需要检查如何将 Gender 变量分配给数值(例如 Female=1),并且仅在提供数据时才会生成图形。否则会有Error: User interrupt or internal error. 消息。
  • 非常感谢!你真棒!有没有办法抑制这个Error: User interrupt or internal error 或者显示一条要求输入值的消息?
  • 我已更新答案以添加消息而不是错误消息。您可以随意制作(例如显示当前患者值的表格)。
  • 当我在本地运行应用程序时它工作正常。但它向我显示一个错误“发生错误。检查您的日志或联系应用作者进行澄清。”当我将它部署到服务器时。我不确定为什么会这样。你有什么建议吗?
猜你喜欢
  • 1970-01-01
  • 2015-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-19
  • 1970-01-01
  • 2021-01-01
相关资源
最近更新 更多