【问题标题】:R Shiny, working with reactive dataframes - NAsR Shiny,使用响应式数据帧 - NA
【发布时间】:2017-09-22 16:17:58
【问题描述】:

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

这是常规 r-script 中代码的症结所在 - 用户输入是:大小(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)

【问题讨论】:

  • 您能否添加s_list 的示例,以便您的应用完全可重现?
  • @cmaher 更新了第一个代码块以提供完整的功能,我将花一天的时间来改进它 - 看看我是否可以在没有循环的情况下做同样的事情
  • 你可以暂时忽略这个,我正在重写整个事情以使其更简单和更快。

标签: r shiny reactive


【解决方案1】:

与其在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() })
    }

【讨论】:

  • 它有效它有效它有效!!!!女士,您是学者!对刚开始使用闪亮的人有什么建议吗?这是处理事情的最佳方式吗?在外面定义函数,然后在里面调用它们?然后我就可以去做很多很酷的事情了!
  • 更好的方法实际上是在单独的文件中编写函数并使用source("filename.R")在您的服务器中获取它。
猜你喜欢
  • 2019-12-19
  • 1970-01-01
  • 2017-01-06
  • 2019-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-27
  • 2020-01-04
相关资源
最近更新 更多