How to retain sliderInput range of a variable(s) when additional variables added dynamically?

我们两清 提交于 2019-12-25 08:48:10

问题


The following code allows to create sliderInput control widgets for selected number of variables. For the brevity, filtering code as per sliderInput range is removed. So, please neglect what is being seen in the data table.
sliderInput widgets pop up whenever a new variable selected or added to the list of already existing variables. However, these widgets do not retain the previously selected values, i.e., let's say you selected sepal.length, modified slider range, and then selected sepal.width. We'll have two widgets but widget of sepal.length does not retain the modified values, i.e., the slider falls back to the original values. Agreed that retaining modified values is not being coded, and I'm unable to figure it out how it can be done.
I understand that I need to read sliderInput range values as soon as they modified, save new modified values, and then update sliderInput range with the saved values before the list of functions called in renderUI. And, the other way is read new values as global variable and use them while creating in widgets, but I plan to access the control widgets in other tabs, so, this does not appear to be a neat idea. Greatly appreciate suggestions.

# Create a data frame of which information is used in creating (dynamical) control widgets
varnames <- names(iris[,1:4]) # names
varinit <- apply(iris[,1:4],2,median) # initival value used in slider
varmin <- apply(iris[,1:4],2,min) # min.
varmax <- apply(iris[,1:4],2,max) # max. 

ui <- navbarPage(
   tabPanel("Plot",
            sidebarLayout(
               sidebarPanel(
                  checkboxGroupInput("ConditioningVariables", "Conditioning variables (choose one or more):",
                                     varnames,inline = TRUE),
                  uiOutput("ControlWidgetsofConditioningVariables")  
               ),
               mainPanel(
                  dataTableOutput("data")
               )
               )
            )
   )

server <- function(input, output, session) {
   allControls <- lapply(setNames(varnames, varnames), function(x) {

                sliderInput(x, x, varmin[x], varmax[x], c(varmin[x], varinit[x]), 
                  round = -2)   
   })
   output$ControlWidgetsofConditioningVariables <- renderUI({    
      if (is.null(input$ConditioningVariables)){
         return() 
      } else {
         allControls[input$ConditioningVariables]
      }
   })
   ## filtering related code is removed, so, data does not change ...
   newdata <- reactive({
         iris
   })
   output$data <- renderDataTable({ newdata() })
}
shinyApp(ui, server)

来源:https://stackoverflow.com/questions/40430899/how-to-retain-sliderinput-range-of-a-variables-when-additional-variables-added

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!