Two actions in Shiny mode

I am writing a brilliant function containing two actionButtons. These are two buttons on the left and right that help the plot move when pressed. Both buttons work fine the first time you press. The problem occurs when I click them again. It will go back and forth between the two ranges no matter which button I press. Perhaps the problem is that after clicking the button, the "right" and "left" do not return to 0. What will be the alternative way to solve my question? Thanks in advance!

My code is below:

shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
  sliderInput("test", label="test", min=0, max=1808, value=c(0,50)),
  actionButton("left", "Left"),
  actionButton("right", "Right")

),
mainPanel(
  plotOutput("bar")
)
)
))


shinyServer(function(input, output, session){
observe({
if(input$right){
  isolate({
    if(input$test[2]+ round((input$test[2]-input$test[1])/2) <= 1808)
    updateSliderInput(session, "test", value=c(input$test[1] + round((input$test[2]-input$test[1])/2), input$test[2] + round((input$test[2]-input$test[1])/2)))
})
}
if (input$left){
  isolate({
    if(input$test[1] - round((input$test[2]-input$test[1])/2) > 0)
    updateSliderInput(session, "test", value=c(input$test[1] - round((input$test[2]-input$test[1])/2), input$test[2] - round((input$test[2]-input$test[1])/2)))
})      
}
})

subdata <- reactive({
  sub.data <- data[input$test[1]:input$test[2]]
})

output$bar <- renderPlot({
  barplot(subdata())
})  

})
+4
source share
2 answers

You can try using two expressions observe, one for each button.

observe({
  if(input$right){
    isolate({
        if(input$test[2]+ round((input$test[2]-input$test[1])/2) <= 1808)
          updateSliderInput(session, "test", value=c(input$test[1] + round((input$test[2]-input$test[1])/2), input$test[2] + round((input$test[2]-input$test[1])/2)))
      })
    }
})

observe({
  if(input$left){ 
    isolate({
        if(input$test[1] - round((input$test[2]-input$test[1])/2) > 0)
          updateSliderInput(session, "test", value=c(input$test[1] - round((input$test[2]-input$test[1])/2), input$test[2] - round((input$test[2]-input$test[1])/2)))
      }) 
  }
})

, reset .

+1

0.11 , actionButton. observeEvent() , (, actionButton)

( 0.11):

library(shiny)
server <- function(input, output, session) {
    observeEvent(input$left, function() {
        if(input$test[2]+ round((input$test[2]-input$test[1])/2) <= 1808)
        updateSliderInput(session, "test", value=c(input$test[1] + round((input$test[2]-input$test[1])/2), input$test[2] + round((input$test[2]-input$test[1])/2)))
    }

    )
    observeEvent(input$right, function() {
         if(input$test[1] - round((input$test[2]-input$test[1])/2) > 0)
    updateSliderInput(session, "test", value=c(input$test[1] - round((input$test[2]-input$test[1])/2), input$test[2] - round((input$test[2]-input$test[1])/2)))
    }
    )
}

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
    sliderInput("test", label="test", min=0, max=1808, value=c(0,50)),
      actionButton("left", "Left"),
      actionButton("right", "Right")
    ),
    mainPanel(


    )
  )
)

shinyApp(ui = ui, server = server)
+3

All Articles