1

I am experimenting with the datamods package in R.

The package can be found here: https://dreamrs.github.io/datamods/reference/index.html

My goal is to dynamically create interactive filters for all variables that are specified (in the vars variable). I am testing that with the mtcars dataset.

My Code:

library(shiny)
library(shinyWidgets)
library(datamods)
library(ggplot2)
library(dplyr)


ui <- fluidPage(
  tags$h2("Filter data.frame"),
  
  fluidRow(
    column(
      width = 3,
      filter_data_ui("filtering", max_height = "1000px")
    ),
    column(
      width = 9,
      DT::dataTableOutput(outputId = "table"),
      plotOutput("plot")
    )
  )
)

server <- function(input, output, session) {
  
  mtcars_1 <- mtcars %>%
    mutate(type = row.names(mtcars)) %>%
    select(type, everything())
  
  data <- reactive({
    mtcars_1
  })
  
  vars <- reactive({
    names(mtcars_1)[1:4]
  })
  
  
  res_filter <- filter_data_server(
    id = "filtering",
    data = data,
    vars = vars,
    widget_char = "picker",
    widget_num = "slider",
    widget_date = "slider",
    label_na = "Missing"
  )
  
  
  output$table <- DT::renderDT({
    res_filter$filtered()
  }, options = list(pageLength = 5))
  
  
  output$plot <- renderPlot({
    ggplot(res_filter$filtered(), aes(cyl, mpg)) +
      geom_point()
  })
  
}

if (interactive())
  shinyApp(ui, server)

I keep getting the error "Text to be written must be a length-one character vector".

When I change the definition of vars to

  vars <- reactive({
    names(mtcars_1)[2:4]
  })

the error disappars. And I get the desired output:

enter image description here

It seams that there is a problem with the type variable. I would like to see a filter for this variable as well.

Does anyone have an idea? Thank you.

  • where are `filter_data_ui` and `filter_data_server` defined? [This](https://github.com/tidyverse/reprex) would be helpful when preparing your questions in future. – Limey May 05 '21 at 06:50
  • These are functions provided by the package datamods. Please see here: https://dreamrs.github.io/datamods/reference/index.html – Mohamed Madmar May 05 '21 at 08:59
  • OK. Thank you for the clarification. In that case I cannot help you as I don't use datamods. However, looking at the `mtcars` data frame, I notice that your `type` variable will contain spaces. I would always check to see if that is an issue. Perhaps try `type=stringr::str_remove_all(row.names(mtcars), stringr::fixed(" "))`. – Limey May 05 '21 at 09:07
  • Thanks, Limey. I tried that but unfortunately it did not help. – Mohamed Madmar May 05 '21 at 14:54
  • I am seeing that my question was closed by stackoverflow. What can I do to reopen it? – Mohamed Madmar May 05 '21 at 15:01
  • Hi Mohamed, I voted to close your question as needing details because you provided code and an error, but you didn't tell us what you want your code to actually *do*. What problem are you asking us to solve? – Ian Campbell May 07 '21 at 03:05
  • Hi Ian. Thank your for your valuable feedback. I will edit my question. – Mohamed Madmar May 11 '21 at 08:43

1 Answers1

1

The filter_data_server drop_ids parameter, TRUE by default, has the following description "Drop columns containing more than 90% of unique values, or than 50 distinct values." So, your first column is being dropped for being unique. You can include your first column by setting drop_ids=FALSE.

This won't remove the warning message, but it should give you a working filter.

michael
  • 245
  • 2
  • 11
  • FYI, it appears that the error message was a datamods bug which is fixed in the most recent version – michael Oct 13 '22 at 19:45