1

I want to use the data.table package in a Shiny App to improve speed. However, it's not clear to me how to select the right columns based on the user input.

The following example works for the case when the data is in the data.frame format but not when it is in data.table format.

# load packages
library(data.table)
# global  ----------------------------------------
library(shiny)
# use cars dataset
data(cars)
# create datatable from cars data
cars <- as.data.table(cars)

# user interface ---------------------------------
ui <- fluidPage(

  sidebarLayout(
    selectInput(inputId = 'col',    label = 'column', choices = names(cars)),
    numericInput(inputId = 'filter',label = 'filter', value = 5)
  ),
    mainPanel(plotOutput("plot"))

)

# server ----------------------------------------
server <- function(input, output) {
   output$plot <- renderPlot({
     # filter example
      d1 <- cars[speed>input$filter,]
      x    <- d1[[input$col]]
      hist(x)
   })
}

# run app  --------------------------------------
shinyApp(ui = ui, server = server)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
JdP
  • 714
  • 7
  • 14

0 Answers0