首页 文章

R Shiny,使用反应式数据帧 - NA

提问于
浏览
1

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

这是常规r脚本中代码的关键 - 用户输入是:size(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年以上的申请人数据,可能或许多人不会随着时间的推移被分配给替代品(基于循环中的规则) .

代码以当前形式工作 - 除了输出表中充满了NA ......任何形式的帮助都会从我所处的位置上升一步!

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)

1 回答

  • 0

    而不是在 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() })
        }
    

相关问题