Search code examples
rshinydatatablereactivereadxl

Shiny R: Excel file input to datatable output (with inputs in the datatable)


I am rather new to Shiny and I want to have a table with the data from a file input in which in the first column the values can be selected from certain options. This works for data which doesn't come from the file input.

In the UI part I take a file Input and an output:

fileinput('file1','Data')
DT::dataTableOutput("ruless")

Then the following works:

 values <- reactiveValues(data = NULL)
 values$data <- as.data.frame(
    cbind(c("a", "d", "b", "c", "e", "f"),
          c(1463, 159, 54, 52, 52, 220),
          c(0.7315, 0.0795, 0.027, 0.026, 0.026, 0.11)
    )
  )
  
  shinyInput = function(FUN, len, id, ...) {
    inputs = character(len)
    for (i in seq_len(len)) {
      inputs[i] = as.character(FUN(paste0(id, i), label = NULL, choices = c("Cylinders" = "cyl",
                                                                  "Transmission" = "am")))
    }
    inputs
  }
  
  output$ruless <- DT::renderDataTable({
    datatable(
      data.frame(Choose=shinyInput(selectInput,nrow(values$data),"cbox_"), values$data)
      )
    )
  })

But now I wanna do the same with the data from the file input. I tried the following:

data3 <- reactive({
    inFile <- input$file1
    if (is.null(inFile)) { return(NULL) }    
    dataFile <- read_excel(inFile$datapath,sheet=1)
    return(dataFile)
  })

And then using data3 instead of values$data or also trying values$data <- as.data.frame(data3) However I always get error messages:

Error in as.data.frame.default(data3): cannot coerce class ‘c("reactiveExpr", "reactive", "function")’ to a data.frame

What can I do?


Solution

  • Is it simply that you need to refer to data3 as data3() when you try to use it, as it's a reactive dataset?