Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple users changing reactive values in R shiny

Tags:

r

shiny

Is it possible for multiple users of the same app to make changes to the same set of reactive values?

This question (handling multiple users simulaneously in an R Shiny app) suggests that multiple users in different sessions can make changes to the same value (by declaring it outside of server() and using <<- instead of <- ) But that is for just plain old values/variables. Is this possible for reactive values?

Ideally, I would like a change made by user A to be immediately reflected in some output viewed by user B.

like image 977
Lee88 Avatar asked Aug 06 '19 05:08

Lee88


People also ask

How does reactive work in R Shiny?

A reactive expression is an R expression that uses widget input and returns a value. The reactive expression will update this value whenever the original widget changes. To create a reactive expression use the reactive function, which takes an R expression surrounded by braces (just like the render* functions).

What does the renderPlot function do in Shiny?

renderPlot is an reactive function that can take input data from the ui. R script and feed it into the server. R script. It then actively updates the information within its function.

How do you make a Shiny reactive?

You can create reactive output with a two step process. Add an R object to your user interface. Tell Shiny how to build the object in the server function. The object will be reactive if the code that builds it calls a widget value.

What is isolate Shiny R?

The isolate function lets you read a reactive value or expression without establishing this relationship. The expression given to isolate() is evaluated in the calling environment. This means that if you assign a variable inside the isolate() , its value will be visible outside of the isolate() .


1 Answers

Here's a minimal working example based on RStudio's default one-file Shiny app:

library(shiny)

slidervalue <- 30

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = slidervalue)
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot"),
           textOutput('txt')
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {

    observe({
        slidervalue <<- input$bins
    })

    reactive_slidervalue <- reactivePoll(100, session, 
        checkFunc = function() { slidervalue },
        valueFunc = function() { slidervalue }
    )

    output$txt <- renderText(reactive_slidervalue())

    observe({
        updateSliderInput(session, 'bins', value = reactive_slidervalue())
    })

    output$distPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = reactive_slidervalue() + 1)

        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

Basically, I am using a global variable (as you and and the post suggested), and then hooked it back into server by using the reactivePoll function to make the external dependency reactive.

like image 120
MrGumble Avatar answered Nov 05 '22 14:11

MrGumble