闪亮的滑块被迫回到初始位置

时间:2019-03-15 07:00:19

标签: r shiny

我正在编写一个闪亮的应用程序以实现以下效果:

每当我选择类别名称中包含的变量时,Web都会生成提供分隔符的滑块(此处使用条件面板)。它将选定的变量分为2组,并形成一个添加到原始数据集的新列。

我的问题是:

每当我在categoryname中选择变量并尝试更改滑块时,都会将滑块强制到初始点,这意味着我无法使用滑块更改输出数据集。

在代码中,我仅使用mtcars数据集,以便所有人都可以访问。

library(shiny)
library(stringr)

categoryname = c("mpg_group", "disp_group")
MT_EG = mtcars[,1:5]

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

  # Application title
  titlePanel("Mtcars Data"),

  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(

      selectInput(inputId = "arm",
                  label = "ARM VARIABLE",
                  choices = c("mpg_group", "cyl", "disp_group", "hp", "drat"),
                  selected = "cyl"),
      conditionalPanel(
        #condition = "categoryname.includes(input.arm)",
        condition = "input.arm == 'disp_group' | input.arm == 'mpg_group'",

        sliderInput("divider", "divide slider", 0, 100, 50)
      )
    ),

    # Show a plot of the generated distribution
    mainPanel(
      uiOutput("data")
    )
  )
)

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

  observeEvent(
    input$arm,
    observe(
      {
        if (input$arm %in% categoryname){
          #browser()
          # start over and remove the former column if exists
          MT_EG = MT_EG[, !(colnames(MT_EG) %in% input$arm)]

          id_arm_var <- input$arm
          id_arm <- unlist(str_split(id_arm_var,'_'))[1]

          # change the range of the slider
          #input$divider$max = max(MT_EG$id_arm)
          val <- input$divider
          mx = max(MT_EG[[id_arm]])
          mn = min(MT_EG[[id_arm]])
          updateSliderInput(session, inputId = "divider", min=floor(mn/2),max = mx + 4,step = 1,value = (mn+1)%%2 + 1)
          #input$divider$min = min(MT_EG$id_arm)

          # generate a new column and bind
          #divi <- data.frame(id_arm_var = MT_EG$id_arm>input$divider)
          divi <- data.frame(id_arm_var = MT_EG[[id_arm]]>input$divider)
          divi$id_arm_var[divi$id_arm_var==TRUE] <- paste0(id_arm_var, " Larger")
          divi$id_arm_var[divi$id_arm_var==FALSE] <- paste0(id_arm_var, " Smaller")
          MT_EG <- cbind(MT_EG,divi)
        }

        output$data=renderTable(MT_EG)
      }
    )
)
}

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

有什么想法吗?谢谢你们!

1 个答案:

答案 0 :(得分:0)

问题在于您正在不断更新除法器中的值 试试这个:

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

  observeEvent(
    input$arm,
    observe(
      {
        if (input$arm %in% categoryname){
          #browser()
          # start over and remove the former column if exists
          MT_EG = MT_EG[, !(colnames(MT_EG) %in% input$arm)]

          id_arm_var <- input$arm
          id_arm <- unlist(str_split(id_arm_var,'_'))[1]

          # change the range of the slider
          #input$divider$max = max(MT_EG$id_arm)
          val <- input$divider
          mx = max(MT_EG[[id_arm]])
          mn = min(MT_EG[[id_arm]])
          updateSliderInput(session, inputId = "divider", min=floor(mn/2),max = mx + 4,step = 1,value = input$divider)
          #input$divider$min = min(MT_EG$id_arm)

          # generate a new column and bind
          #divi <- data.frame(id_arm_var = MT_EG$id_arm>input$divider)
          divi <- data.frame(id_arm_var = MT_EG[[id_arm]]>input$divider)
          divi$id_arm_var[divi$id_arm_var==TRUE] <- paste0(id_arm_var, " Larger")
          divi$id_arm_var[divi$id_arm_var==FALSE] <- paste0(id_arm_var, " Smaller")
          MT_EG <- cbind(MT_EG,divi)
        }

        output$data=renderTable(MT_EG)
      }
    )
  )
}

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

通过updateSliderInput(session, inputId = "divider", min=floor(mn/2),max = mx + 4,step = 1,value = input$divider),其中value = input$divider可以确保每次更新时都保留分频器值,以便查看结果。