1

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

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks ... still getting an error but it seems to be related to my data. arguments imply differing number of rows: 11764, 0 How do you go about debugging shiny apps in Rstudio? If I place a stop on like 26 which seems to be the offending line, I don't get any info about the variables at that point in the environment pane.
I personally use print statemets for debugging. It seems either z_scores() or percentile() returns NULL (for your dataset). I'm afraid without your actual data, there is nothing more I can suggest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.