-1

Im trying to obtain results like a data.frame from a reactive object, but without sucess. The object is something like:

 textInput("artist", "Artist/Band", value = "The Beatles", width = NULL, placeholder = NULL)

 artist <- reactive({searchArtist(input$artist)})

Error message in this case: R shiny ERROR: object of type 'closure' is not subsettable

Where, for example, we have:

artist <- searchArtist("Regina+spektor")
str(artist)
'data.frame':   1 obs. of  6 variables:
 $ id        : chr "3z6Gk257P9jNcZbBXJNX5i"
 $ name      : chr "Regina Spektor"
 $ popularity: int 65
 $ genres    :List of 1
 ..$ : chr  "acoustic pop" "anti-folk" "folk-pop" "indie folk" ...
 $ type      : chr "artist"
 $ followers : num 691496

I have also this code, which works:

 output$table <- renderDataTable({
        inFile <- searchArtist(input$artist)
                if (is.null(inFile))
        inFile[2:6]
  })

dataTableOutput("table")

And the function searchArtistis from package Rspotify.

Anyone?

  • Can you show what a potential input looks like? It probably depends on the `+` sign as input to `searchArtist` – timfaber Apr 22 '17 at 22:48
  • The input is made by: textInput("artist", "Artist/Band", value = "The Beatles", width = NULL, placeholder = NULL) – Bruna Wundervald Apr 22 '17 at 22:52
  • It actually works fine when I type "Beatles", "the beatles", "THE BEATLES". I think this is not the problem. – Bruna Wundervald Apr 22 '17 at 22:53
  • What is the error message? Looks like it should work to me. – Mike Wise Apr 22 '17 at 22:53
  • When i try to extract some column of the supposed data.frame, i get: object of type 'closure' is not subsettable @Mike Wise – Bruna Wundervald Apr 22 '17 at 22:54
  • That is a common message when the object you are trying to extract is not defined there, but by coincidence there is a function defined elsewhere with the same name (think "closure==function"). There are lots and lots of functions defined in base R which can be a problem. – Mike Wise Apr 22 '17 at 22:56
  • You need to post more code - best would be a standalone example illustrating the problem. Hard to tell what is going on this way. – Mike Wise Apr 22 '17 at 22:57
  • Agreed on having sample code: it is in SO guidelines. I assume that you `always` call the reactive as `artist()`. If the above is true, it is possible that the response of your call `sometimes` is not a data frame. E.g. do you get a `df` also when the singer doesn't exist? Is it possible that in these cases the reposes is not a tidy `df` but just a string: `error!`? Maybe the package `rspotify` is well written and the above doesn't happen, but I saw it plenty of times. Your code does not have anything to protect you from that. You could check for `NULL`, `nrow > x` or `is.data.frame(x)` – Enzo Apr 22 '17 at 23:12
  • I have written a little more. I cant post the whole code because it envolves my spotify credentials. – Bruna Wundervald Apr 22 '17 at 23:17
  • Not even with the first example, "The Beatles", the code is running. I have also this code, which `works`: output$table <- renderDataTable({ inFile <- searchArtist(input$artist) if (is.null(inFile)) inFile[2:6] }) – Bruna Wundervald Apr 22 '17 at 23:17
  • I guess it has to do with the output you are generating. This ref explains your issue well I think: http://stackoverflow.com/questions/24779604/r-and-shiny-using-output-of-a-reactive-function. To use it here, try defining artist as a data object outside of the `reactive` definition and accordingly use this object. e.g., `data = artist()` and subsequently plot/display this table – timfaber Apr 22 '17 at 23:40

1 Answers1

1

See comment:

library("Rspotify")
library("shiny")

ui=shinyUI(fluidPage(
  sidebarPanel(
    textInput("artist", "Artist/Band", value = "The Beatles", width = NULL, placeholder = NULL)
  ),
mainPanel(
  textOutput("text")
)
  ))

server=shinyServer(function(input, output) {

artist <- reactive({
  searchArtist(input$artist)
  })

output$text = renderText({
  data <- artist()
  print(data$id)
  }) 

})

shinyApp(ui=ui,server=server)
timfaber
  • 2,060
  • 1
  • 15
  • 17