2

I am creating an app where you can select the columns that you want to see/show and do the logarithm or sqrt to the entire dataframe. The first option (selection) is running through pickerInput and the second with checkboxInputs.

In order to show the table with your selection or your changes in the dataframe, you have to click an actionButton. The selection of the columns works perfectly but if you click one of the checkboxInput after your selection, the selection is removed and you will see all the columns again.

image 1

image 2

This is how it looks when you want to do the logarithm after your selection. The selection of the columns disappear.

image 3

This is the code:

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)

Does anyone know what I should do to fix this?

Thanks very much in advance

Regards

emr2
  • 1,436
  • 7
  • 23

1 Answers1

1

That is because your pickerInput is based on data1(), and that changes based on the checkbox selection. In fact, it should be using data(). Try this

  output$picker <- renderUI({
    pickerInput(inputId = 'pick', 
                label = 'Choose', 
                choices = colnames(data()),
                options = list(`actions-box` = TRUE),
                multiple = T,
                selected = colnames(data())
    )
  })
YBS
  • 19,324
  • 2
  • 9
  • 27
  • Thanks very much! I couldn't think that it was the problem! – emr2 Aug 09 '21 at 13:18
  • One question, if for example data1() has 2 extra columns (which you cannot find in data() ), how would it be? because in that case I would be interested in using data1() in the `pickerInput` but... the error appears again... – emr2 Aug 09 '21 at 13:28
  • I would have to see how you are creating those two extra columns. Then you can move that creation to data(). Perhaps you can post it as a new question with [MRE](https://stackoverflow.com/help/minimal-reproducible-example) – YBS Aug 09 '21 at 13:39
  • I posted it as a new question here https://stackoverflow.com/questions/68738147/output-from-1-pickerinput-in-shiny-automatically-updates-when-i-do-changes-in-th Thanks! – emr2 Aug 11 '21 at 08:26