闪亮的反应性

时间:2013-07-17 15:16:52

标签: r shiny

我有一个包含大量参数的应用程序。每个参数都有很多粒度,这使得找到所需的参数变得很痛苦。这导致反应部分不断地计算,这减慢了事情。我添加了一个submitButton,它解决了上述问题,但后来又遇到了另一个问题。

下面是我构建的框架的简单复制。参数输入采用1到1000之间的数字,表示我想要的样本。我想做的是能够在上面做,但也能够使用相同的参数集重新采样。添加提交按钮后现在发生的事情是,除非我先单击重新采样然后再单击更新按钮,否则它会使重采样按钮无法运行。

任何让它们分开工作的想法?

shinyServer(function(input, output) { 
  getY<-reactive({
    a<-input$goButton
    x<-rnorm(input$num)
    return(x)
  })

  output$temp <-renderPlot({
     plot(getY())
  }, height = 400, width = 400)
})

shinyUI(pageWithSidebar(
  headerPanel("Example"),
  sidebarPanel(
    sliderInput("num",
            "Number of Samples",
            min = 2,
            max = 1000,
            value = 100),
    actionButton("goButton", "Resample"),
    submitButton("Update View")        
  ),  
  mainPanel(
    tabsetPanel(      
      tabPanel("Heatmap",
               plotOutput("temp")
      ),
      tabPanel("About"),      
      id="tabs"
    )#tabsetPanel      
  )#mainPane;   
))

编辑基于乔的答案:

shinyServer(function(input, output) { 
  getY<-reactive({

    isolate({a<-input$goButton
      x<-rnorm(input$num)
      return(x)})
  })

  output$temp <-renderPlot({
     b<-input$goButton1
     plot(getY())
  }, height = 400, width = 400)
})

shinyUI(pageWithSidebar(
  headerPanel("Example"),
  sidebarPanel(
    sliderInput("num",
            "Number of Samples",
            min = 2,
            max = 1000,
            value = 100),
    actionButton("goButton", "Resample"),
    actionButton("goButton1","Update View")        
  ),  
  mainPanel(
    tabsetPanel(      
      tabPanel("Heatmap",
               plotOutput("temp")
      ),
      tabPanel("About"),      
      id="tabs"
    )#tabsetPanel      
  )#mainPane;   
))

2 个答案:

答案 0 :(得分:9)

答案是由Joe Cheng在上面的评论中给出的,但是看到OP难以理解它,我在下面明确地写出来,记录:

# ui.R

library("shiny")
shinyUI(
  pageWithSidebar(
    headerPanel("Example")
    ,
    sidebarPanel(
      sliderInput("N", "Number of Samples", min = 2, max = 1000, value = 100)
      ,
      actionButton("action", "Resample")
    )
    ,  
    mainPanel(
      tabsetPanel(      
        tabPanel("Plot", plotOutput("plotSample"))
        ,
        id = "tabs1"
      )
    )
  )
)

# server.R

library("shiny")
shinyServer(
  function(input, output, session) { 
    Data <- reactive({
        input$action
        isolate({ 
            return(rnorm(input$N))
            return(x)
        })
    })
  output$plotSample <-renderPlot({
      plot(Data())
    } , height = 400, width = 400
  )
})

请注意,在reactive()中输入$ action,其中“action”是actionButton的inputID,足以触发绘图的新渲染。所以你只需要一个actionButton。

答案 1 :(得分:4)

  • 更改getY,以便除第一行以外的所有行都包含在isolate({...})
  • 将submitButton更改为actionButton
  • 在renderPlot中添加一行以读取新的actionButton