Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny: allow reactivity to be user-selectable

Tags:

r

shiny

I'm building an app with several tabs, some which involve excessive calculations and others that calculate quickly. A checkbox which would allow the user to choose between reactivity or manual updating, in combination with a "refresh"-button, would be ideal.

The simple example below illustrates what I'm aiming for. It nearly works, except for one final refresh when the "Automatically refresh"-checkbox is unchecked, which is a pain, should a computationally intensive tab be open. Is there any way around this?

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

Many thanks in advance!

like image 741
mholopai Avatar asked Jan 14 '15 18:01

mholopai


People also ask

What is a reactive source in a shiny application?

In a Shiny application, the source typically is user input through a browser interface. For example, when the selects an item, types input, or clicks on a button, these actions will set values that are reactive sources. A reactive endpoint is usually something that appears in the user’s browser window, such as a plot or a table of values.

Can I call reactive expressions outside of functions in shiny?

In fact, Shiny will prevent you from calling reactive expressions outside of these functions. Time to fix the broken check box for “Adjust prices for inflation.” Your user should be able to toggle between prices adjusted for inflation and prices that have not been adjusted.

How many types of Reactive Objects does shiny have?

Presently, Shiny has one class of objects that act as reactive sources, one class of objects that act as reactive conductors, and one class of objects that act as reactive endpoints, but in principle there could be other classes that implement these roles.

What is a reactive endpoint in shiny?

A reactive endpoint is usually something that appears in the user’s browser window, such as a plot or a table of values. In a simple Shiny application, reactive sources are accessible through the input object, and reactive endpoints are accessible through the output object.


1 Answers

In this solution, I made two observers: one for when the refresh button is hit and a second for when choice is changed. The first always updates the output.

The second checks the status of input$autoRefresh and then either just exits or updates the renderText.

Unfortunately you must have the runif command written twice, which can be bad for updating your code (easier to introduce errors if you are doing something twice). In practice you might want to make a new function and then just call that function if this is a complex/multi-line process in your actual app.

  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))})
      }  
    })
  })
like image 150
John Paul Avatar answered Sep 24 '22 00:09

John Paul