0

I am trying to code a simple example where i can change reactive sources, not by mouse interaction but by changing into R code the value of that reactive source.

Here is a working example:

ui.R :

# coming from 001-helloworld example....
library(shiny)

shinyUI(fluidPage(

  titlePanel("Hello Shiny!"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
    ),
    mainPanel(
      plotOutput("distPlot")
    )
  )
))

server.R :

library(shiny)

shinyServer(function(input, output) {

  output$distPlot <- renderPlot({
    x    <- faithful[, 2]  # Old Faithful Geyser data
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    hist(x, breaks = bins, col = 'darkgray', border = 'white')
  })

})

I want to be able to access and change "bins" in R code and make the graph react to that (not using the mouse).

EDIT: To clarify, the change in bins should be produced in R code in runtime (once the shiny "app" is running). So, it is like "replacing" the mouse interaction with "code in runtime" interaction.

Is there a way to do it? I am mostly sure to miss some obvious point here... sorry .

Thanks.

Miguel Vazq
  • 1,459
  • 2
  • 15
  • 21
  • Miguel The question is not very clear .. you want to change "bins" on the shiny app code or on the running application side? – Keniajin Jan 14 '15 at 20:16
  • Hi @Keniajin, yes, I want to change the value of "bins" in runtime code. For example it would be that i make in R (once the shiny example is running) bins = 100, and i want to see the change into the "shiny objects" (the histogram and the bins value in the slider). I added comment in the question to clarify. – Miguel Vazq Jan 15 '15 at 09:37

1 Answers1

0

The short answer is no. Once you make something a reactive source, it cannot be modified except reactively. The main issue is that if a value or an output is supposed to update to reflect changes made by the user, and then the program changes it - should it change back to what the user indicated or not. See here for a little more discussion of this.

What you can do, however, is have an app decide between using a reactive value (input$bins here) or some other value. Below is a short app that does this.If the user chooses more than 25 bins, the app will use 3 instead.

library(shiny)
runApp(list(
  ui=fluidPage(
    titlePanel("Hello Shiny!"),
    sidebarLayout(
      sidebarPanel(
        sliderInput("bins",
                    "Number of bins:",
                    min = 1,
                    max = 50,
                    value = 30)
      ),
      mainPanel(
        plotOutput("distPlot")
      )
    )
  ),

  server=function(input, output) {

    output$distPlot <- renderPlot({
      x    <- faithful[, 2]  # Old Faithful Geyser data
      bins <- seq(min(x), max(x), length.out = if(input$bins>25) {4} else{input$bins + 1})
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })

  }
))

You could also have, for example, a checkboxInput that turned was used to choose between a reactive variable or a non-reactive one. You could also use more complex logic then demonstrated here, but this should give you the general idea.

Community
  • 1
  • 1
John Paul
  • 12,196
  • 6
  • 55
  • 75