object of type 'closure' is not subsettabl

2019-05-11 18:42发布

问题:

I was coding in shiny with the rgl and shinyRGL package, trying to plot a 3D line plot by having the users insert a csv file of a specific format. But the object type closure error keeps showing up. It seems like because it can't find the function plot3d, or I may be wrong.

Here's the code:

UI

library(shiny)
library(rgl)
library(shinyRGL)

# Define UI for application that draws a histogram
shinyUI(fluidPage(
  titlePanel("title panel"),

  sidebarLayout(
    sidebarPanel(
      helpText("Please select a CSV file with the correct format."),
      tags$hr(),
      fileInput("file","Choose file to upload",accept = c(
        'text/csv',
        'text/comma-separated-values',
        'text/tab-separated-values',
        'text/plain',
        '.csv',
        '.tsv',
        label = h3("File input"))
    ),
    tags$hr(),
    checkboxInput('header', 'Header', TRUE),

    actionButton("graph","PLOT!")
    ),


mainPanel(textOutput("text1"),
          webGLOutput("Aplot")))
)
)

Server

library(shiny)
library(rgl)
library(shinyRGL)

options(shiny.maxRequestSize = 9*1024^2)
shinyServer(
  function(input, output) {


    output$text1 <- renderText({
    paste("You have selected", input$select)
  })
    output$"Aplot" <- renderWebGL({
      inFile <- reactive(input$file)
      theFrames <- eventReactive(input$graph,read.csv(inFile$datapath,
header = input$header))
plot3d(theFrames[[4]],theFrames[[5]],theFrames[[6]],xlab="x",ylab="y",zlab 
= "z", type = "l", col = ifelse(theFrames[[20]]>0.76,"red","blue"))
   })
})

Error

Warning: package hinyRGL?was built under R version 3.3.1 Warning: Error in [[: object of type 'closure' is not subsettable Stack trace (innermost first): 70: plot3d 69: func [C:\Users\Ian\workspace\Copy of Leap SDK/Test\App_1/server.R#19] 68: output$Aplot 1: runApp

回答1:

Remember this error message, since it's very typical for shiny applications.

It almost always means, that you had a reactive value, but didn't use it with parentheses.

Concerning your code, I spotted this mistake here:

inFile <- reactive(input$file)
theFrames <- eventReactive(input$graph,read.csv(inFile$datapath,
    header = input$header)) 

plot3d(theFrames[[4]],theFrames[[5]],theFrames[[6]],xlab="x",ylab="y",zlab 
    = "z", type = "l", col = ifelse(theFrames[[20]]>0.76,"red","blue"))

You use inFile like a normal variable, but it isn't. It's a reactive value and thus has to be called with inFile(). The same goes for theFrames, which you called with theFrames[[i]], but should be called with theFrames()[[i]].

So the correct version would be

inFile <- reactive(input$file)
theFrames <- eventReactive(input$graph,read.csv(inFile()$datapath,
    header = input$header)) 

plot3d(theFrames()[[4]],theFrames()[[5]],theFrames()[[6]],xlab="x",ylab="y",zlab 
    = "z", type = "l", col = ifelse(theFrames()[[20]]>0.76,"red","blue"))

Maybe some additional info about the error message: Shiny evaluates the variables only when they are needed, so the reactive theFrames, containing the error, is executed from inside the plot3d function. That is why the error message tells you something about the error being in plot3d, even if the error lies somewhere else.



回答2:

I would recommend you should look at your naming conventions. I have always seen this error when I am using variable name same as names of any function defined in packages or any function defined by me.

For example:

header = input$header
inFile = input$file

You should always restrict yourself in using these kinds of names, it will always be useful.

Thanks :)



标签: r shiny rgl