Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny Reactivity

Tags:

r

shiny

I've got an application with a large number of parameters. Each parameters has lots of granularity which make finding the desired one a pain. This causes the reactive portion to constantly calculate which slows things down. I added a submitButton which solved the above problem but then experience another problem in turn.

Below is a simple replication of the framework I build. The parameter input takes in a number from 1 to 1000, which indicates the sample to which I want. What I would like to do is be able to do above but also be able to resample with the same set of parameters. What is happening now after adding the submit button is that it renders the resample button inoperable unless I click resample first AND then update button.

Any ideas of making them both working separately?

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;   
))

EDIT based on Joe's Answer:

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;   
))
like image 494
user1234440 Avatar asked Jul 17 '13 15:07

user1234440


1 Answers

The answer was given by Joe Cheng in a comment above, but seeing that the OP had difficulty understanding it, I write it out explicitly below, for the record:

# 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
  )
})

Note that having input$action inside reactive(), where "action" is the actionButton's inputID, is enough to trigger a new rendering of the plot. So you need only one actionButton.

like image 94
PatrickT Avatar answered Sep 21 '22 07:09

PatrickT