2015-01-14 40 views
6

我正在构建一个包含多个选项卡的应用程序,其中一些涉及过多的计算和其他计算速度很快的应用程序。一个允许用户在反应性或手动更新之间进行选择的复选框,结合“刷新”按钮将是理想选择。闪亮:允许反应性是用户可选的

下面的简单例子说明了我的目标。它几乎可行,除非最后一次刷新时“自动刷新” - 复选框未被选中,这是一个痛苦,如果打开一个计算密集型选项卡。有没有办法解决?

ui.r

library(shiny) 
shinyUI(fluidPage(
    titlePanel("Test"), 
    sidebarLayout(
     sidebarPanel(
      checkboxInput("autoRefresh", "Automatically refresh", TRUE), 
      actionButton("refresh", "Refresh!"), 
      radioButtons("choice", "Choice of value:", 
       c("10" = 10, 
       "20" = 20)) 
      ), 

     mainPanel(
      tabsetPanel(type = "tabs", 
       tabPanel("Add random decimals to value", textOutput("value")) 
      ) 
     ) 
    ) 
)) 

server.r

library(shiny) 
shinyServer(function(input, output) { 
    output$value <- renderText({ 

     input$refresh 
     if(input$autoRefresh == 1) { 
      input$choice 
     } 
     isolate({ 
      output <- runif(1,0,1) + as.numeric(input$choice) 
     }) 
    }) 
}) 

提前非常感谢!

回答

2

在此方案中,我做了两个观察员快捷,返回它:一个当refresh按钮命中和第二时choice被改变。第一个总是更新输出。

第二个检查input$autoRefresh的状态,然后只是退出或更新renderText

不幸的是,你必须有runif命令写入两次,可用于更新你的代码是不好的(更容易,如果你正在做的事情两次引入错误)。在实践中,如果这是实际应用程序中的复杂/多行处理过程,您可能需要创建一个新函数,然后调用该函数。

shinyServer(function(input, output) { 
    observe({ 
     input$refresh 
     output$value<-renderText({ 
     isolate(runif(1,0,1) + as.numeric(input$choice)) 
     }) 
     }) 
    observe({ 
     input$choice 
     output$value<-if(input$autoRefresh==0) return() else { 
      renderText({isolate(runif(1,0,1) + as.numeric(input$choice))}) 
     } 
    }) 
    }) 
+0

错字:Refesh。 更重要的是,这不起作用。当箱子未被选中时它刷新。这是因为它不工作,你解释它的方式......“如果”语句,直到输入$选择的变化进入触发整个renderText()块的执行。 – hedgedandlevered

+0

@hedgedandlevered谢谢!更新了答案。 –

+0

此代码现在可用。 –

2

您可以缓存输出,并在适当的时候

library(shiny) 
shinyServer(function(input, output) { 
    output$value <- renderText({ 

    input$refresh 
    if(input$autoRefresh == 1) { 
     input$choice 
    } else return(cachedOutput) 
    isolate({ 
     cachedOutput <<- output <- runif(1,0,1) + as.numeric(input$choice) 
    }) 
    }) 
}) 
+0

不错的主意!然而,对于上面的代码,手动清爽不作为cachedOutput工作,如果总是返回输入$自动刷新== 0, – mholopai

+0

,我意识到,高速缓存可以在我的应用程序,其中有时只有一个表的部分需要重新计算的另一部分有用。这会派上用场,谢谢! – mholopai