当您在数据框中进行更改时,来自 Shiny 的 pickerInput 无法正常工作

pickerInput from Shiny doesn't work properly when you make changes in your dataframe

我正在创建一个应用程序,您可以在其中 select 您想要的列 see/show 并对整个数据框进行对数或开方。第一个选项 (selection) 是 运行 到 pickerInput,第二个选项是 checkboxInputs.

为了显示 table 与您的 selection 或您在数据框中的更改,您必须单击 actionButton。列的 selection 工作正常,但如果您在 selection 之后单击 checkboxInput 之一,selection 将被删除,您将再次看到所有列.

这就是您想在 selection 之后进行对数时的样子。 select列的离子消失。

这是代码:

library(shiny)
library(shinyWidgets)
library(dplyr)

ui <- fluidPage(
  
  # Application title
  titlePanel("Old Faithful Geyser Data"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
     
      uiOutput("picker"),
      
      checkboxInput("play", strong("I want to play with my data"), value = FALSE),
      
      conditionalPanel(
        condition = "input.play == 1",
        checkboxInput("change_log2", "Log2 transformation", value = FALSE),
        checkboxInput("run_sqrt", "sqrt option", value = FALSE)),
    
    
      actionButton("view", "View Selection")
      
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      h2('Mydata'),
      DT::dataTableOutput("table"),
    )
  )
)

library(shiny)
library(DT)

server <- function(session, input, output) {
  
  data <- reactive({
    mtcars
  })
  
  data1 <- reactive({
    
    dat <- data()
    
    if(input$change_log2){
      dat <- log2(dat)
    }
    
    if(input$run_sqrt){
      dat <- sqrt(dat)
    }
    
    dat
  })
  
  observeEvent(input$play, {
    
    if(!input$play) {
      updateCheckboxInput(session, "change_log2", value = FALSE)
      updateCheckboxInput(session, "run_sqrt", value = FALSE)
    }
    
  })
  
  
  output$picker <- renderUI({
    pickerInput(inputId = 'pick', 
                label = 'Choose', 
                choices = colnames(data1()),
                options = list(`actions-box` = TRUE),
                multiple = T,
                selected = colnames(data1())
                )
  })
  
  datasetInput <- eventReactive(input$view,{
    
    datasetInput <- data1() %>% 
      select(input$pick)
    
    return(datasetInput)
    
  })
  
  output$table <- renderDT({
   
      datatable(
        datasetInput(),
        filter="top", 
        rownames = FALSE,
        extensions = 'Buttons',
        
        options = list(
          dom = 'Blfrtip',
          buttons =
            list('copy', 'print', list(
              extend = 'collection',
              buttons = list(
                list(extend = 'csv', filename = "File", title = NULL),
                list(extend = 'excel', filename = "File", title = NULL)),
              text = 'Download'
            ))
        ),
        class = "display"
      )
  })
}


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

有谁知道我应该怎么做才能解决这个问题?

非常感谢

此致

那是因为您的 pickerInput 是基于 data1() 的,并且会根据复选框的选择而变化。事实上,它应该使用 data()。试试这个

  output$picker <- renderUI({
    pickerInput(inputId = 'pick', 
                label = 'Choose', 
                choices = colnames(data()),
                options = list(`actions-box` = TRUE),
                multiple = T,
                selected = colnames(data())
    )
  })