Error when creating shiny dashboard and using datamods library

364 Views Asked by At

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.

1

There are 1 best solutions below

1
On BEST ANSWER

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.