使用renderUI更新数据集而不是条件面板

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

每当我选择categoryname包含的变量时,web将生成提供分隔符的滑块.它将所选变量分为两组,并形成一个新列,其中包含添加到原始数据集的组名.

这里的人帮助我在this question中使用条件面板来解决问题,但是现在我使用了renderUI和shinyjs,因为条件面板在我的大项目中不起作用.

我被一个小虫子(似乎)困住了:

Error in data.frame: arguments imply differing number of rows: 32, 0

以下是我的代码,如何更改它以使该功能有效?

library(shiny)
library(shinyjs)
library(stringr)

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

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

  useShinyjs(),

  # 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", 1, 100, 20)
      #   optionalSliderInputValMinMax("divider", "divide slider", c(50,0,100), ticks = FALSE)
      # )
      uiOutput("divider")
    ),

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

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

  output$divider <- renderUI({
    if (input$arm %in% categoryname){
      show("divider")
    }
    else{
      hide("divider")
    }
    sliderInput("divider", "divide slider", 0, 100, 50)
  })

  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
          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)

          # generate a new column and bind
          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")
          colnames(divi) <- id_arm_var
          MT_EG <- cbind(MT_EG,divi)
        }

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

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

我只是使用mtcars数据集,以便所有人都可以访问

最佳答案 observeEvent和renderUI都依赖于“或由”输入$arm触发.发生的事情是observeEvent在可能在UI中渲染renderUI之前请求输入$divider并且输入$divider变得可用于observeEvent,正如我在上面提到的那样输入$divider以值NULL结束.

要解决这个问题,只需在MT_EG = MT_EG [,!(%input $arm中的colnames(MT_EG)%)]之前添加req(输入$divider).同时将输出$divider更改为输出$dividerUI,因为Shiny不允许输入和输出具有相同的id.

有关更多详细信息,请参阅?shiny :: req.

点赞