Filtering data frame based on input created with a dependent renderUI with shiny modules

I am currently trying to make an application that takes in a large data frame and then filters based on a column from the uploaded data frame, before displaying a table for the user. When I try to filter based on the value of the dependent input, I receive an error that the filter value must be ‘[size of the data frame] or 1, not size 0’. I can render the output of the value, just not filter with it.

I’m new to writing modules, so there may be something very obvious that I am missing, but I have not been able to work it out yet.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
#Reprex modules
library(shiny)
library(bslib)
library(DT)
testUI<-function(id){
ns<-NS(id)
tagList(
layout_columns(selectInput(ns("carb"), "Carb", choices = seq(1:10), selected = 4),
uiOutput(ns("cyl"))),
tableOutput(ns("table")),
tableOutput(ns("table_ouput"))
# textOutput(ns("cyl_text"))
)
}
###initial filter function in UI to make dynamic dummy dataframe
fetchServer <- function(id) {
moduleServer(id, function(input, output, session) {
ns<-session$ns
return(reactive(input$carb))
})
}
##the initial data frame where the filtering column is located
testServer <- function(id, .carb) {
moduleServer(id, function(input, output, session) {
ns<-session$ns
data<-reactiveValues(data = NULL)
observe(data$data<-mtcars%>%filter(carb == .carb()))
output$table<-renderTable(data$data)
return(reactive(data$data))
})
}
##rendering the values from the filtering column in the module's UI
cylServer <- function(id, carb_data) {
moduleServer(id, function(input, output, session) {
ns<-session$ns
output$cyl<-renderUI({
selectInput(
ns("cyl"),
"Cyl",
choices = carb_data()$cyl)
})
return(reactive(input$cyl))
})
}
##attempting to filter based on the user's selection of the rendered UI element
new_tableServer <- function(id, .cyl) {
moduleServer(id, function(input, output, session) {
ns<-session$ns
data<-reactiveValues(data = NULL)
observe(data$data<-mtcars%>%filter(cyl == .cyl()))
output$table_output<-renderTable(data$data)
# output$cyl_text<-renderText(.cyl())
})
}
</code>
<code> #Reprex modules library(shiny) library(bslib) library(DT) testUI<-function(id){ ns<-NS(id) tagList( layout_columns(selectInput(ns("carb"), "Carb", choices = seq(1:10), selected = 4), uiOutput(ns("cyl"))), tableOutput(ns("table")), tableOutput(ns("table_ouput")) # textOutput(ns("cyl_text")) ) } ###initial filter function in UI to make dynamic dummy dataframe fetchServer <- function(id) { moduleServer(id, function(input, output, session) { ns<-session$ns return(reactive(input$carb)) }) } ##the initial data frame where the filtering column is located testServer <- function(id, .carb) { moduleServer(id, function(input, output, session) { ns<-session$ns data<-reactiveValues(data = NULL) observe(data$data<-mtcars%>%filter(carb == .carb())) output$table<-renderTable(data$data) return(reactive(data$data)) }) } ##rendering the values from the filtering column in the module's UI cylServer <- function(id, carb_data) { moduleServer(id, function(input, output, session) { ns<-session$ns output$cyl<-renderUI({ selectInput( ns("cyl"), "Cyl", choices = carb_data()$cyl) }) return(reactive(input$cyl)) }) } ##attempting to filter based on the user's selection of the rendered UI element new_tableServer <- function(id, .cyl) { moduleServer(id, function(input, output, session) { ns<-session$ns data<-reactiveValues(data = NULL) observe(data$data<-mtcars%>%filter(cyl == .cyl())) output$table_output<-renderTable(data$data) # output$cyl_text<-renderText(.cyl()) }) } </code>

#Reprex modules

library(shiny)
library(bslib)
library(DT)


testUI<-function(id){
  
  ns<-NS(id)
  tagList(
    layout_columns(selectInput(ns("carb"), "Carb", choices = seq(1:10), selected = 4),
                   uiOutput(ns("cyl"))),
    tableOutput(ns("table")),
    tableOutput(ns("table_ouput"))
    # textOutput(ns("cyl_text"))
    
  )
  
}

###initial filter function in UI to make dynamic dummy dataframe
fetchServer <- function(id) {
  
  moduleServer(id, function(input, output, session) {
    
    ns<-session$ns
    
     return(reactive(input$carb))
    
  })
}

##the initial data frame where the filtering column is located
testServer <- function(id, .carb) {

  moduleServer(id, function(input, output, session) {

    ns<-session$ns

    data<-reactiveValues(data = NULL)
    
    observe(data$data<-mtcars%>%filter(carb == .carb()))

    output$table<-renderTable(data$data)

    return(reactive(data$data))
      })

}

##rendering the values from the filtering column in the module's UI
cylServer <- function(id, carb_data) {
  
  moduleServer(id, function(input, output, session) {
    
    ns<-session$ns
    
    output$cyl<-renderUI({
      
      selectInput(
              ns("cyl"), 
              "Cyl", 
              choices = carb_data()$cyl)
      
      
    })
    
    return(reactive(input$cyl))
    
  })
}

##attempting to filter based on the user's selection of the rendered UI element
new_tableServer <- function(id, .cyl) {

  moduleServer(id, function(input, output, session) {

    ns<-session$ns

    data<-reactiveValues(data = NULL)

    observe(data$data<-mtcars%>%filter(cyl == .cyl()))

    output$table_output<-renderTable(data$data)
    
    # output$cyl_text<-renderText(.cyl())

    })

}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#Reprex App
ui <- page(
testUI("x1")
)
server <- function(input, output) {
carb<-fetchServer("x1")
data<-testServer("x1", carb)
cyl<-cylServer("x1", data)
new_tableServer("x1", cyl)
}
shinyApp(ui = ui, server = server)
</code>
<code>#Reprex App ui <- page( testUI("x1") ) server <- function(input, output) { carb<-fetchServer("x1") data<-testServer("x1", carb) cyl<-cylServer("x1", data) new_tableServer("x1", cyl) } shinyApp(ui = ui, server = server) </code>
#Reprex App

ui <- page(
  
    testUI("x1")

    )


server <- function(input, output) {

carb<-fetchServer("x1")
data<-testServer("x1", carb)
cyl<-cylServer("x1", data) 
new_tableServer("x1", cyl)
}


shinyApp(ui = ui, server = server)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật