R 如何在特定条件下使对象保持相同的光泽?

R 如何在特定条件下使对象保持相同的光泽?,r,shiny,R,Shiny,对于我的工作,我正在创建一个闪亮的应用程序。目前它运行良好,但获取数据需要我与SQL接口,这需要一段时间。此应用程序以两种方式使用数据:1)显示数据,2)使用数据运行模拟(这也很耗时)。我需要能够为许多不同的客户获取数据 到目前为止,我可以将数据从模拟中分离出来,并直接在模拟中使用我已经拥有的数据,前提是我已经拥有了正确的数据。如果客户编号发生变化,我可以获取数据并运行模拟。所有这些都很棒,但有一件事我不能做,那就是如果我还没有正确的数据,就显示我为模拟得到的数据 我已经尝试了几种方法来实现这一

对于我的工作,我正在创建一个闪亮的应用程序。目前它运行良好,但获取数据需要我与SQL接口,这需要一段时间。此应用程序以两种方式使用数据:1)显示数据,2)使用数据运行模拟(这也很耗时)。我需要能够为许多不同的客户获取数据

到目前为止,我可以将数据从模拟中分离出来,并直接在模拟中使用我已经拥有的数据,前提是我已经拥有了正确的数据。如果客户编号发生变化,我可以获取数据并运行模拟。所有这些都很棒,但有一件事我不能做,那就是如果我还没有正确的数据,就显示我为模拟得到的数据

我已经尝试了几种方法来实现这一点,但我目前正在使用的方法是,如果我已经拥有数据,并且客户编号正确,但在所有其他情况下都会发生变化,则尝试使数据对象保持不变。我尝试过的所有方法要么给我一个无限循环,要么给我一个空白数据集。我已经创建了一个简化版的应用程序(因为我无法显示真实的应用程序),它应该突出我遇到的问题。这就是我目前拥有的:

library(shiny)

shinyUI(fluidPage(
  fluidRow(wellPanel(
    #create at action button to generate data.
    actionButton("gen", "Generate"),
    textOutput("check")
    ),
    wellPanel(
      #'create an action button for simulation.  Here there is no simulation because
      #'I am not having a problem with simulation, but this should also generate
      #'data if there is no data, or the customer numbers do not match
      actionButton("sim", "Simulate"),
      #indicates whether the customer numbers match
      checkboxInput("same", "Same customer number", value = T),
      #output the data table
      dataTableOutput("data")
      ))
  ))

library(mvtnorm)


shinyServer(function(input, output){
  #'every time I hit the sim button, this is set as the gen button's count.
  #'By checking to see if this equals the count of the gen button, I can generate
  #'data every time I hit the gen button, but not every time I hit the sim button
  gen.check<-reactive({
    input$sim
    isolate({as.numeric(input$gen)})
  })
  output$check<-renderText({
    gen.check()<input$gen
  })
  #indicates whether there is already data or not
  check<-reactive({
    if(input$gen==0 & input$sim==0){
      FALSE
    }else{
      TRUE
    }
  })
  #generate the data
  data<-reactive({
    #'this stops the app from displaying data when it opens.  Not super important
    #'here, but in the actual app, getting the data is time consuming, so I don't
    #'want it to start out by getting data I don't need
    if(input$gen==0 & input$sim==0){
      return()
    }
    #This line makes data show up every time I hit the gen button
    if(input$gen>gen.check()){
      rmvnorm(20, 1:5, diag(5))
    }else{
      #'I had hoped this blank loop here would make the data stay the same if there
      #'already was data, and the customer numbers matched, but instead it makes the
      #'data disappear under these conditions
      if(isolate({input$same}) & check()){
      }else{
        #'This generates data if there is none, or if the numbers don't match when
        #'you hit sim
        rmvnorm(200, 1:5, diag(rep(.1, 5)))
      }
    }
  })
  #'This piece is separate because in the actual app I need to use the data generated
  #'above two different ways.  I am just using it here to check when the data changes.
  output$data<-renderDataTable({
    if(input$gen==0 & input$sim==0){
      return()
    }
    as.data.frame(data())
  })
})
库(闪亮)
shinyUI(fluidPage)(
fluidRow(井面板)(
#在操作按钮上创建以生成数据。
操作按钮(“生成”、“生成”),
文本输出(“检查”)
),
井面板(
#'为模拟创建操作按钮。此处没有模拟,因为
#“我在模拟方面没有问题,但这也会产生
#'如果没有数据或客户编号不匹配,则返回数据
动作按钮(“模拟”、“模拟”),
#指示客户编号是否匹配
checkboxInput(“相同”、“相同的客户编号”,值=T),
#输出数据表
dataTableOutput(“数据”)
))
))
图书馆(mvtnorm)
shinyServer(功能(输入、输出){
#“每次我按下sim按钮时,它都被设置为gen按钮的计数。
#'通过检查这是否等于gen按钮的计数,我可以生成
#“我每次按gen按钮时都会收到数据,但不是每次按sim卡按钮时都会收到数据

gen.Check你的问题有点难理解。你能编辑它吗?添加示例代码应该如何工作,以及它是如何失败的。例如“当你单击模拟按钮时,X应该发生,但Y却发生”或者随便什么。谢谢。正如@John Paul所说,这真的有点难理解。如果我理解正确,你的问题可以通过某种缓存来处理。我已经成功地在Shiny中使用了package
memoise
来有效地处理这一问题。亲爱的@不重要,这个问题需要澄清。为了避免你的问题被关闭,作为对你的帮助如果您可以使用
memoise
发布一个自包含的示例,这将是一件好事。这里欢迎您回答自己的问题。您这样做甚至可能会获得一些荣誉。如果我的评论不清楚,请道歉。发布一个示例它是如何工作的,这是一种选择方法,如果有这些问题,请随意重命名您的变量应该透露一些专有的东西。
grabber<-memoise(data.grab)