在Shiny,Rstudio

时间:2015-08-07 20:11:44

标签: r validation shiny

我已经编写了我的代码的简化版本,所以希望有人能够给我一个方向。我试图在闪亮的情况下执行validate()过程,这样它就会阻止我的函数SPC_macro运行如果它失败。我知道你可以编写多个need子句,这可以使验证过程独立失败,但我需要将这些子句组合起来。例子:下面我有一个mean = 1,sigma = 1,phase1 = 15和userk = 3.1的验证子句。我试图让validate函数只返回一个假sigma(这将阻止我的函数运行)如果这些值全部设置在一起。因此,如果用户要输入mean = 1,sigma = 1,phase1 = 15且userk = 3.8,则验证将通过并且函数将运行。我也想知道是否可以制作多个失败的组合。我已经运行了3次模拟,这些是我试图阻止用户重新运行的模拟。

sim 1:mean = 1,sigma = 1,phase1 = 15,userk = 3.1

sim 2:mean = 2,sigma = 2,phase1 = 30,userk = 3.6

sim 3:mean = 2,sigma = 2,phase1 = 30,userk = 4.0

`%then%` <- shiny:::`%OR%`
mean<-c(1,2,3)
sd<-c(1,2,3)
phase1<-c(15,20,25,30)
k<-(c(3,3.2,3.4,3.6,3.8,4.0))
shinyServer(function(input, output) {
output$mean <- renderUI({
  selectInput('mean', 'Mean', mean)
})
output$sigma<-renderUI({

  selectInput('sigma',"variation",sd)
})
output$phase1<-renderUI({
  selectInput('phase1','phase1', phase1)
})
output$userk<-renderUI({
  selectInput('userk','userk',k)
})

rankuser<-eventReactive(input$gobutton, {
  validate(
    need(input$mean !="1","Data Already Exists")%then%
    need(input$sigma !="1", "data exists")%then%
    need(input$phase1!="15", "data extists")%then%
    need(input$userk != "3.1", "data already exists")
  )
  SPC_macro(nphase1=input$phase1, nphase2=100000, varcount=1, meanshift=input$mean, sigmashift=input$sigma, k=input$userK, simID=100)
  userdata<-rank_user_combined
})
})

编辑: 我尝试了@nice建议做的事情:

rankuser<-eventReactive(input$gobutton, {
    data_calculated<-list(val_data)
     validate(
      need(any(sapply(data_calculated,
              function(x) identical(x,c(input$usernphase1,input$usermean,input$userSD,input$userk))),"Data Already Exists")
    )
    SPC_macro(nphase1=input$usernphase1, nphase2=100000, varcount=1, meanshift=input$usermean, sigmashift=input$userSD, k=input$userK, simID=100)
    userdata<-rank_user_combined
  })

data_calculated的结构(该列表总共有大约400行):

> data_calculated
[[1]]
    nphase1 meanshift sigmashift   k
1        10         4        1.0 3.0
2        15         0        1.0 3.0
3        15         0        1.0 3.1
4        15         0        1.0 3.2
5        15         0        1.0 3.3
6        15         0        1.0 3.4
7        15         0        1.0 3.5
8        15         0        1.0 3.6
9        15         0        1.0 3.7
10       15         0        1.0 3.8

1 个答案:

答案 0 :(得分:2)

一种解决方案可能是列出您已计算数据的所有参数,例如:

#make a list of vectors for which you have calculated values
data_calculated<-split(val_data,row.names(val_data))
data_calculated <- lapply(data_calculated,as.numeric)

当用户点击go时,您可以检查参数是否在此列表中:

any(sapply(data_calculated,
           function(x) identical(x,c(input$mean,input$sigma,input$phase1,input$userk))
    )
    )

sapply将遍历data_calculated并将其与用户输入所构成的向量进行比较。如果找到相同的向量,any将返回TRUE,否则返回false。

如果将其包装到need中,则应验证输入。