R Shiny,使用反应式数据框 - NAs

R Shiny, working with reactive dataframes - NAs

我正在尝试根据一些用户定义的规则填充数据。frame/matrix。我设法在 R 中创建了一个函数,但一直试图将其复制为一个 Shiny 应用程序[这是我第一次使用 Shiny,我是个白痴才开始使用它]

这是常规 r 脚本中代码的症结所在 - 用户输入是:大小(1~3),变化(1~2)和迭代(10~1000)

school_choice_function<- function(changes, size, iterations )

{
######## 1509
##### List of schools
p<-1
j<-1
k<-1
l<-1

s_list<- rep(0,80)

for (i in 1:80) {

if (i <= 26)  {  
  schl<- paste(LETTERS[p],LETTERS[i],sep = "")  
  s_list[i]<- schl }

if (i>26 & i<=52) {p<- 2
schl<- paste(LETTERS[p],LETTERS[j],sep = "")  
s_list[i]<- schl  
j=j+1}

if (i>52 & i<=78) {p<- 3
schl<- paste(LETTERS[p],LETTERS[k],sep = "")  
s_list[i]<- schl  
k=k+1}  

if (i>78 ) {p<- 4
schl<- paste(LETTERS[p],LETTERS[l],sep = "")  
s_list[i]<- schl  
l=l+1}    

}
rm(p,i,j,k,l)


########## Applicant Data
a<- c(2011:2015)
c<- 1:size
d<- 1:changes
y<-0
v<-1
w<-10

mat <- matrix(ncol=5, nrow=(iterations*10))


for(pop in 1:iterations){

for (z in v:w)
{

  b<- s_list[(1+y):(8+y)]

  e<- rep(0,5)
  e[1]<- b[1]
  g<- sample(d,1)

  h<- sample(2:5,g, replace = FALSE)
  f1<- rep(0,length(h))


  for(j in 1:g){

    for(i in 1:length(h))
    {
      f<- sample(c, 1)
      f1[i]<- paste(sample(b,f,replace = FALSE),collapse = ",")
      e[h[i]]<- f1[i]
    }

  }

  for(i in c(which(e %in% 0))){

    e[i]<- e[i-1]
  }


  mat[z,]<- e
  y<-y+8
}
v<- w+1
w<- w+10
y<-0

}


df<- data.frame(mat,stringsAsFactors = FALSE)
colnames(df)<- c("2011","2012","2013","2014","2015")

return(df)

}

忽略在编码中使用最糟糕的做法(我刚刚学会了从循环的角度思考),我正在使用这是一个像这样的闪亮应用程序。 "s_list/schools"是一个80个元素的字符矩阵,在此代码之前创建。

只是为了让您直观地了解这到底是什么 - 基本上是 5 年以上的申请人数据,随着时间的推移,可能会或许多人不会被分配到替代方案,(基于循环中出现的规则).

代码在当前形式下工作——除了输出 table 充满了 NAs....任何形式的帮助都会比我现在的位置更上一层楼!

ui<- fluidPage(

numericInput(inputId="Changes", label="Changes", value=1, min = 1, max = 3, step = 1),
numericInput(inputId="Size", label="Size", value=2, min = 1, max = 3, step = 1),
numericInput(inputId="Iterations", label="Iterations", value=10, min = 10, max = 1000, step = 10), 
tableOutput("dframe")
)



server<- function(input,output) {

Changes<- reactive({input$Changes})
Size<- reactive({input$Size})
Iterations<- reactive({input$Iterations})

schools<- s_list

########## Applicant Data
a<- c(2011:2015)
cc<- reactive(1:(Size()))
d<- reactive(1:(Changes()))
y<-0
v<-1
w<-10

mat <- reactive(matrix(ncol=5, nrow=((input$Iterations)*10)))
pop<- 0
z<- 0
i<- 0
j<- 0

this<- reactive({
for(pop in 1:(Iterations())){

  for (z in v:w)
  {

    b<- schools[(1+y):(8+y)]

    e<- rep(0,5)
    e[1]<- b[1]
    g<- reactive(sample(d(),1))

    h<- reactive(sample(2:5,g(), replace = FALSE))
    f1<- reactive(rep(0,length(h())))


    for(j in 1:g()){

      for(i in 1:length(h()))
      {
        f<- reactive(sample(cc(), 1))
        f1()[i]<- reactive(paste(sample(b,f(),replace = FALSE),collapse = ","))
        e[h()[i]]<- f1()[i]
      }

    }

    for(i in cc()(which(e %in% 0))){

      e[i]<- e[i-1]
    }


    mat()[z,]<- e
    y<-y+8
  }

  v<- w+1
  w<- w+10
  y<-0

}

})


df<- reactive(data.frame(mat(),stringsAsFactors = FALSE))

output$dframe <- renderTable({  df() })
}

shinyApp(ui= ui, server = server)

为什么不在您的 server 中编写函数 school_choice_function 的全部代码,为什么不在您的服务器外部定义该函数并从您的服务器内部调用它。像这样:

 server<- function(input,output) {

      Changes<- reactive({input$Changes})
      Size<- reactive({input$Size})
      Iterations<- reactive({input$Iterations})

      df<- reactive({

        df <- school_choice_function(Changes(), Size(), Iterations())
        return(data.frame(df, stringsAsFactors = FALSE))

      })

      output$dframe <- renderTable({ df() })
    }