3

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

gman
  • 100,619
  • 31
  • 269
  • 393
I AN Lee
  • 31
  • 10
  • It's not that it can't find the function, it's that somewhere in those functions you're trying to subset a closure (a function), which obviously doesn't work. Try swapping out subsetted dynamic terms with static placeholders (anything that you know will let the function run) so you can figure out which term is causing the issue. – alistaire Jul 14 '16 at 03:19
  • @alistaire It seems like the issue is within my XYZ parameter "theFrames[[...]]". But I don't know what's causing it or how to fix it. – I AN Lee Jul 14 '16 at 03:31
  • What's `str(theFrames)`? – alistaire Jul 14 '16 at 03:44
  • @alistaire It is just the data frame which stores the csv file. The plot3d function access it for the XYZ coordinates for the plot. – I AN Lee Jul 14 '16 at 03:49
  • I suspect `eventReactive` is somehow not returning what you expect. – alistaire Jul 14 '16 at 04:07
  • @alistaire If i get rid of the eventReactive then my button for plotting wouldn't work. – I AN Lee Jul 14 '16 at 05:37
  • Note that `shinyRGL` is not currently being maintained. You will likely have better luck using `rglwidget` from CRAN, or (if you like to live on the bleeding edge) just `rgl` from R-forge or github. – user2554330 Jul 14 '16 at 12:48
  • @alistaire is there ever a case where the Shiny app runs fine on a localhost, but is throwing this error when deployed to an external Shiny server? That's what is currently happening right now- I'm seeing the error message on the browser screen with no stack trace or line, so it's difficult to begin knowing where to debug? – Yu Chen Oct 04 '17 at 23:51
  • @YuChen Ugh, that situation is hard to debug. It's possible you can see more of what the problem is through your browser's console, though I haven't tried. You could try clearing your local environment out to see if it's because of some variable/function you have locally that you haven't built into the code, but that's a guess. – alistaire Oct 05 '17 at 01:32

2 Answers2

5

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.

K. Rohde
  • 9,439
  • 1
  • 31
  • 51
  • I fixed the 'closure' error following what you said, thanks a lot. But now it's giving me another error massage saying: ' Warning: Error in <-: invalid (NULL) left side of assignment Stack trace (innermost first): 69: func [C:\Users\Ian\workspace\Copy of Leap SDK Test\App_1/server.R#15] 68: output$Aplot 1: runApp ' – I AN Lee Jul 15 '16 at 02:12
  • @IANLee Then you probably put too many `()`s inside your code. See the answer [here](http://stackoverflow.com/questions/35769388/reactives-invalid-null-left-side-of-assignment). – K. Rohde Jul 15 '16 at 08:12
0

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 :)

Bhushan Pant
  • 1,445
  • 2
  • 13
  • 29