0

I am building a Shiny app that displays a different rhandsontable for each radioButton choice ("A", "B"). This app includes a callModule of two variables ("Petal.Width", "Species"). The problem is that the table doesnt update when you change the selectizeGroupUI choices. Example: filtering by Species = "versicolor" and viewing both tables, the tables look like:

introducir la descripción de la imagen aquí

when we change the Species filter, the table we are currently viewing updates, but when we change the radioButton choice, this new table doesnt update.

introducir la descripción de la imagen aquí

This is the code:


library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(rhandsontable)
library(DT)


data1 <- iris %>% mutate(Petal.Width = if_else(iris$Petal.Width>0.4, ">0.4", "<=0.4")) %>% 
  select("Petal.Width", "Species", "Petal.Length")

data1 <- data1[c(1:3,44,51:53,102:105),]

data2 <- iris %>% mutate(Petal.Width = if_else(iris$Petal.Width>0.4, ">0.4", "<=0.4")) %>% 
  select("Petal.Width", "Species", "Sepal.Length")

data2 <- data2[c(1:3,44,51:53,102:105),]

ui <- dashboardPage(
  skin = "black",
  dashboardHeader(
    title = "Aranceles",
    titleWidth = 200,
    uiOutput("logoutbtn")
  ),
  
  dashboardSidebar(collapsed = TRUE),
  
  dashboardBody(
    
    fluidRow(
      tabBox(
        height = "2100px",
        width = 12,
        selected = "Tab1",
        
        # Pestaña Propuesta incrementos pre pagas####        
        tabPanel("Tab1",
                 
                 fluidRow(column(
                   width = 6,
                   selectizeGroupUI(
                     id = "my-filters1",
                     inline = TRUE,
                     params = list(
                       Petal.Width= list(
                         inputId = "Petal.Width",
                         title = "Petal.Width"
                       ),
                       Species = list(
                         inputId = "Species",
                         title = "Species"
                     )))),
                 
                 fluidRow(
                   basicPage(
                     radioButtons(
                       inputId = "value",
                       choices = list("A", "B"),
                       label = "label"
                     ))),
                 
                 fluidRow(basicPage(uiOutput('table')))
      ))
  )
)))

server <- function(input, output, session) {
  
  data_reac1<- callModule(
    module = selectizeGroupServer,
    id = "my-filters1",
    data = data1,
    vars = c("Petal.Width","Species")
  )
  
  data_reac2<- callModule(
    module = selectizeGroupServer,
    id = "my-filters1",
    data = data2,
    vars = c("Petal.Width","Species")
  )
  
  
  rv_data1 <- reactiveValues(table_rv1 = NULL)
  
  observeEvent((input$value == "A" & 
                  ((is.null(input[["my-filters1-Species"]]) & is.null(input[["my-filters1-Petal.Width"]])) | 
                     (!is.null(input[["my-filters1-Species"]]) & !is.null(input[["my-filters1-Petal.Width"]])) |
                     (!is.null(input[["my-filters1-Species"]]) & is.null(input[["my-filters1-Petal.Width"]])) |
                     (is.null(input[["my-filters1-Species"]]) & !is.null(input[["my-filters1-Petal.Width"]])))) || 
                 (input$value == "B" & 
                    ((is.null(input[["my-filters1-Species"]]) & is.null(input[["my-filters1-Petal.Width"]])) | 
                       (!is.null(input[["my-filters1-Species"]]) & !is.null(input[["my-filters1-Petal.Width"]])) |
                       (!is.null(input[["my-filters1-Species"]]) & is.null(input[["my-filters1-Petal.Width"]])) | 
                       (is.null(input[["my-filters1-Species"]]) & !is.null(input[["my-filters1-Petal.Width"]])))),{
                         data_reac1.1 <- data_reac1() %>%
                           mutate("new_var" = 0) 
                         rv_data1$table_rv1<- data_reac1.1})
  
  observe({
    if (!is.null(input$table1)){
      mytable_rv1 <- as.data.frame(hot_to_r(input$table1))
      mytable_rv1[,4] <- (mytable_rv1[,3]*2)
      rv_data1$table_rv1 <- mytable_rv1}
  })
  
  table_1.1 <- reactive({
    rhandsontable(rv_data1$table_rv1)
  })
  
  
  rv_data2 <- reactiveValues(table_rv2 = NULL)
  
  observeEvent((input$value == "A" & 
                  ((is.null(input[["my-filters1-Species"]]) & is.null(input[["my-filters1-Petal.Width"]])) | 
                     (!is.null(input[["my-filters1-Species"]]) & !is.null(input[["my-filters1-Petal.Width"]])) |
                     (!is.null(input[["my-filters1-Species"]]) & is.null(input[["my-filters1-Petal.Width"]])) |
                     (is.null(input[["my-filters1-Species"]]) & !is.null(input[["my-filters1-Petal.Width"]])))) || 
                 (input$value == "B" & 
                    ((is.null(input[["my-filters1-Species"]]) & is.null(input[["my-filters1-Petal.Width"]])) | 
                       (!is.null(input[["my-filters1-Species"]]) & !is.null(input[["my-filters1-Petal.Width"]])) |
                       (!is.null(input[["my-filters1-Species"]]) & is.null(input[["my-filters1-Petal.Width"]])) | 
                       (is.null(input[["my-filters1-Species"]]) & !is.null(input[["my-filters1-Petal.Width"]])))),{
                         data_reac2.1 <- data_reac2() %>%
                           mutate("new_var" = 0) 
                         rv_data2$table_rv2<- data_reac2.1})
  
  observe({
    if (!is.null(input$table2)){
      mytable_rv2 <- as.data.frame(hot_to_r(input$table2))
      mytable_rv2[,4] <- (mytable_rv2[,3]*2)
      rv_data2$table_rv2 <- mytable_rv2}
  })
  
  table_2.1 <- reactive({
    rhandsontable(rv_data2$table_rv2)
  })
  
  output$table1 <- renderRHandsontable({
    table_1.1() 
  })
  
  output$table2 <- renderRHandsontable({
    table_2.1()
  })
  
  
  output$table <- renderUI({
    if(input$value == "A"){rHandsontableOutput("table1")}
    else{rHandsontableOutput("table2")}})
  
 }

shinyApp(ui = ui, server=server)

We tried considering in observeEvent every possible input combination.

                  ((is.null(input[["my-filters1-Species"]]) & is.null(input[["my-filters1-Petal.Width"]])) | 
                     (!is.null(input[["my-filters1-Species"]]) & !is.null(input[["my-filters1-Petal.Width"]])) |
                     (!is.null(input[["my-filters1-Species"]]) & is.null(input[["my-filters1-Petal.Width"]])) |
                     (is.null(input[["my-filters1-Species"]]) & !is.null(input[["my-filters1-Petal.Width"]])))) || 
                 (input$value == "B" & 
                    ((is.null(input[["my-filters1-Species"]]) & is.null(input[["my-filters1-Petal.Width"]])) | 
                       (!is.null(input[["my-filters1-Species"]]) & !is.null(input[["my-filters1-Petal.Width"]])) |
                       (!is.null(input[["my-filters1-Species"]]) & is.null(input[["my-filters1-Petal.Width"]])) | 
                       (is.null(input[["my-filters1-Species"]]) & !is.null(input[["my-filters1-Petal.Width"]])))```
Renzo
  • 13
  • 3
  • Is it really your wish to show different tables on changing the `value` input? I think that it would be much more straightforward to filter from the same dataframe. – David Jorquera May 19 '23 at 19:09
  • Thank you for answering @David. Yes, it´s what we want to do because in the actual code the table from input "A" has one formula and the table from input "B" has a different formula. Choices "A" and "B" are not categories from a variable, they indicate what formula to use, that´s why we can´t filter from the same dataframe. – Renzo May 22 '23 at 16:22

0 Answers0