agf1997
agf1997

Reputation: 2898

Adding to a dataframe in shiny

I'm trying to add two new columns to a dataframe in shiny. I'm currently getting the error invalid 'envir' argument of type 'closure' but I'm not sure why.

My server.R code is :

server <- function(input, output) {

  inFile <- reactive({input$file1})

  data <- reactive({
    if (is.null(inFile())) return(NULL)
    read.xlsx2(inFile()$datapath,
               1,
               sheetName=NULL,
               colIndex = c(1:7),
               header = TRUE)
  })

  z_scores <- reactive({
    if (is.null(inFile())) return(NULL)
    with(data, ave(as.numeric(data$Raw.Score), data$Reader.Name, FUN=scale))
  })

  percentile <- reactive({
    if (is.null(inFile())) return(NULL)
    format( pnorm(data$z_scores) * 100, scientific = FALSE)
  })

  processedData <- reactive({
    if (is.null(inFile())) return(NULL)
    cbind(
      data(),
      z_score = z_scores(),
      percentile = percentile()
    )
  })

  output$view <- renderDataTable(
    processedData(),
    options = list(pageLength = 10)
  )

}

my ui.R code is :

ui <- shinyUI( fluidPage(
    sidebarLayout(
      sidebarPanel(
           fileInput("file1", "Choose XLSX File", accept = ".xlsx"),
           checkboxInput("header", "Header", TRUE)
      ),
      mainPanel(
        dataTableOutput("view")
      )
    )
) )

What do I need to do to avoid this error? I'm not even sure what it's trying to tell me.

Thanks

Upvotes: 1

Views: 512

Answers (1)

Gregor de Cillia
Gregor de Cillia

Reputation: 7635

The following code works with this dataset

myDat <- data.frame(
  z_scores = rnorm(10), Raw.Score = rnorm(10), 
  Reader.Name = sample(letters,10) )
file <- paste("inputfile", "xlsx", sep=".")
write.xlsx(myDat, file = file)

It seems you used the reactive value data wrong. If you want to access the current value of a reactive, you need to use data(), or data()$column_name. I also strongly advise you to rethink your variable naming. data is a function from utils that loads datasets from libraries. Overwriting this function can lead to very curious behavior sometimes.

server <- function(input, output) {

  inFile <- reactive({input$file1})

  data <- reactive({
    if (is.null(inFile())) return(NULL)
    read.xlsx2(inFile()$datapath, 1, sheetName = NULL, 
               colIndex = c(1:7), header = TRUE)
  })

  z_scores <- reactive({
    if (is.null(inFile())) return(NULL)
    with(data(), ave(as.numeric(data()$Raw.Score), data()$Reader.Name, FUN = scale))
  })

  percentile <- reactive({
    if (is.null(inFile())) return(NULL)
    format( pnorm(as.numeric(data()$z_scores)) * 100, scientific = FALSE)
  })

  processedData <- reactive({
    if (is.null(inFile())) return(NULL)
    cbind(
      data(),
      z_score = z_scores(),
      percentile = percentile()
    )
  })

  output$view <- renderDataTable(
    { processedData() },
    options = list(pageLength = 10)
  )

}

ui <- shinyUI( fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose XLSX File", accept = ".xlsx"),
      checkboxInput("header", "Header", TRUE)
    ),
    mainPanel(dataTableOutput("view"))
  )
))

shinyApp(ui, server)

Upvotes: 1

Related Questions