gpt4 book ai didi

r - 为什么在 Shiny 中出现 "subscript out of bounds"错误,但在 R 中却没有?

转载 作者:行者123 更新时间:2023-12-02 04:04:39 24 4
gpt4 key购买 nike

我最近在shiny google group中发布了类似的询问,但没有找到解决方案。我们正在开发一个 Shiny 的应用程序,正如主题所示,我们在运行该应用程序时收到“错误:下标越界”消息。然而,当我们隔离有问题的代码并在 RStudio 中单独运行它时,就没有错误了。

这让我想知道Shiny 本身是否存在错误,或者我们是否遗漏了某些内容。

请参阅下面的说明以及产生错误的小示例。我们使用的是 Shiny 版本 0.8.0 和 RStudio 0.98.501。

感谢您的帮助!

<小时/>

要运行应用程序,请将 ui.R 和 server.R(见下文)放入文件夹中并运行

library(shiny)
runApp("<folder path>")

它应该生成一个用户界面,左侧有一个按钮,但在右侧您将看到“错误:下标超出范围”。

但是,如果只运行以下三行代码(大约是 server.R 中的第 57-59 行)

show=data.frame(ps=c(4,-1,0,1),ns=c(0,1,0,0),ts=c(45842,15653,28535,21656))
best.fit1=regsubsets(ts~ps+ns,data=show,nvmax=1)
pred1=predict.regsubsets(best.fit1,show,id=1) # line that offends Shiny

在 RStudio 中(需要包含函数“predict.regsubsets” - 在 server.R 的开头给出),那么就不会出现错误。

#####################
## server.R
#####################

library(rms)
library(leaps)
library(shiny)
library(datasets)
library(stringr)
library(ttutils)
library(plyr)
library(utils)
library(ggplot2)

# object is a regsubsets object
# newdata is of the form of a row or collection of rows in the dataset
# id specifies the number of terms in the model, since regsubsets objects
# includes models of size 1 up to a specified number
predict.regsubsets=function(object,newdata,id,...){
form=as.formula(object$call[[2]])

mat=model.matrix(form,newdata)

mat.dims=dim(mat)
coefi=coef(object,id=id)
xvars=names(coefi)
# because mat only has those categorical variable categories associated with newdata,
# it is possible that xvars (whose variables are defined by the "best" model of size i)
# has a category that is not in mat
diffs=setdiff(xvars,colnames(mat))
ndiffs=length(diffs)
if(ndiffs>0){
# add columns of 0's for each variable in xvars that is not in mat
mat=cbind(mat,matrix(0,mat.dims[1],ndiffs))
# for the last "ndiffs" columns, make appropriate names
colnames(mat)[(mat.dims[2]+1):(mat.dims[2]+ndiffs)]=diffs
mat[,xvars]%*%coefi
}
else{
mat[,xvars]%*%coefi
}
}

# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {

mainTable1 <- reactive({

})

output$table21 <- renderTable({
mainTable1()
})


formulamodel1 <- reactive({
#ticketsale<-dataset1Input()

show=data.frame(ps=c(4,-1,0,1),ns=c(0,1,0,0),ts=c(45842,15653,28535,21656))
best.fit1=regsubsets(ts~ps+ns,data=show,nvmax=1)
pred1=predict.regsubsets(best.fit1,show,id=1)

})

output$model1fit <- renderPrint({
formulamodel1()

})

})

######################
## end server.R
######################

######################
## ui.R
######################

library(rms)
library(leaps)
library(shiny)
library(datasets)
library(stringr)
library(ttutils)
library(plyr)
library(utils)
library(ggplot2)

shinyUI(pageWithSidebar(

headerPanel("Forecasting ticket sales for xxx"),

sidebarPanel(
p(strong("Model Fitting")),

selectInput("order1", "Sort results by:",c("a","b","c")),
submitButton("Run Model")

),

mainPanel(

h3(strong("Model fit without using ticket sales") ),
tableOutput("table21"),
verbatimTextOutput(outputId = "model1fit")

)
))

最佳答案

这三行似乎仅在全局环境中执行时才起作用。如果您获取该代码片段并在 local({...}) block 内运行它,您将看到相同的错误。

错误来自 predict.regsubsets 的第一行,您可以在其中查看 object$call[[2]]object$call 根据是否在全局环境中执行而有很大不同;它是通过调用 sys.call(sys.parent()) 在 leaps:::regsubsets.formula 中创建的。也许这需要是 sys.call(sys.parent(0)) (只是猜测)?

关于r - 为什么在 Shiny 中出现 "subscript out of bounds"错误,但在 R 中却没有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23430645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com