Home > Net >  Pass reactiveVal() as values in sliderInput() in a shiny app
Pass reactiveVal() as values in sliderInput() in a shiny app

Time:01-06

In the shiny app below I color the dynamic box() based on the number of rows of a dynamic dataset (valurange).I just use the iris dataset as an example here.

When the number of rows of the dataset is less than 5 the color of the box will be red, otherwise it will be green.The issue is that I cannot use my reactiveVal() as values in the shiny widget that sets the number of rows in my dynamic dataset.

I get Error in : In sliderInput(): min, max, and value cannot be NULL, NA, or empty.

## app.R ##
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    tags$head(
      tags$style(HTML("
                      #mybox2Red{border-top-style: none; border-left-color: red; border-left-style: solid;}
                      #mybox2Green{border-top-style: none; border-left-color: green; border-left-style: solid;}
                      "))
      ),
    uiOutput("box2")
      )
      )


server <- function(input, output) {
  Value1 <- reactiveVal(5)
  Value2 <- reactiveVal(10)
  
  observeEvent(input$vr, Value1(input$vr))
  observeEvent(input$vr, Value2(input$vr))
  
  valuerange<-reactive({
    iris<-iris[Value1():Value2(),]
    
  })
  
  
  output$box2 <- renderUI({
    req(Value1)
    req(Value2)
    box(
      id=ifelse(nrow(valuerange())<=5, "mybox2Red", "mybox2Green"),
      title = "title", 
      closable = TRUE, 
      width = 10,
      status = "danger", 
      solidHeader = F, 
      collapsible = TRUE,
      collapsed = T,
      sliderInput("vr","Set value range", min = 0, max=10, value =  c(Value1(),Value2()))
    )
  })
}

shinyApp(ui, server)

CodePudding user response:

You are setting Value1 and Value2 to the same vector from the slider and not separating the lower and upper values. I think you meant

  observeEvent(input$vr, Value1(input$vr[1]))
  observeEvent(input$vr, Value2(input$vr[2]))
  • Related