-1

I'm getting this error when I run below code, can anyone please tell how to overcome this error. Below is the code in which mydata is the main data set and I have created a shiny dashboard using the below code. I tried to make one of the column as URL , but its showing error as in title.

And I tried giving data()$IFX_USERNAME as in his is a very common error in shiny apps. This most typically appears when you create an object such as a list, data.frame or vector using the reactive() function – that is, your object reacts to some kind of input. If you do this, when you refer to your object afterwards, you must include parentheses.

For example, let’s say you make a reactive data.frame like so:

MyDF<-reactive({ code that makes a data.frame with a column called “X” }) If you then wish to refer to the data.frame and you call it MyDF or MyDF$X you will get the error. Instead it should be MyDF() or MyDF()$X You need to use this naming convention with any object you create using reactive(), even then its showing the same error

library("shiny")
    library("datasets")
    library("DT")
    library("shinyBS")
    library(tidyr)

    lapply( dbListConnections( dbDriver( drv = "MySQL")), dbDisconnect) 
    #connecting to database
    dbListTables(mydb)
    dbListFields(mydb, 'DL_COMMUNITY')
    rs = dbSendQuery(mydb, "select * from DL_COMMUNITY")
    mydatabase=fetch(rs)
    setDT(mydatabase)
    colnames(mydatabase)


    header <- dashboardHeader()

    ui = shinyUI(fluidPage(
      DT::dataTableOutput("mtcarsTable"),
      bsModal("mtCarsModal", "My Modal", "",dataTableOutput('mytext'), size = "large")
    ))

    on_click_js = "
    Shiny.onInputChange('mydata', '%s');
    $('#mtCarsModal').modal('show')
    "

    on_click_js1 = "
    Shiny.onInputChange('mydata', '%s');
    $('#mtcarsTable').modal('show')
    "

    convert_to_link = function(x) {
      as.character(tags$a(href = "#", onclick = sprintf(on_click_js,x), x))
    }
    convert_to_link1 = function(x) {
      as.character(tags$a(href = "#", onclick = sprintf(on_click_js1,x), x))
    }

    shinyApp(
      ui = ui,
      server = function(input, output, session) {

        mtcarsLinked <- reactive({   
          mydatabase$IFX_USERNAME <- sapply(
            mydatabase$IFX_USERNAME,convert_to_link)
          return(mydatabase)
        })

        **linked <- reactive({
          myd$TEAM_MEMBERS <- sapply(
            myd$TEAM_MEMBERS,convert_to_link1)
          return(myd)

        })**

        output$mtcarsTable <- DT::renderDataTable({
          DT::datatable(mtcarsLinked(), 
                        class = 'compact',
                        escape = FALSE, selection='none'
          )
        })
        output$mytext = DT::renderDataTable({
          #userQuery=paste("select PROJECT,COMMENT from DL_COMMUNITY where IFX_USERNAME = '",user,"'",sep="")
          #rs = dbSendQuery(mysqlCon,userQuery)
          userQuery=paste("SELECT * 
            from Heatmap.DL_PROJECT where CONCAT(',', TEAM_MEMBERS, ',')  like '%,sa,%' 
          or PROJECT_OWNER like '%,sa,%'
          or PROJECT_LEAD like '%,sa,%'")
          rs = dbSendQuery(mydb,userQuery)
          myd=fetch(rs,n=-1)
          myd<-data.frame(myd)
          myd$TEAM_MEMBERS<- as.list(strsplit(myd$TEAM_MEMBERS, ","))
          #myd<-myd %>% 
               #mutate(TEAM_MEMBERS = strsplit(as.character(TEAM_MEMBERS), ",")) %>% 
               #unnest(TEAM_MEMBERS)
          #setDT(myd)
         #hello <- input$mydata
         #myd<-mydatabase[mydatabase$IFX_USERNAME==input$mydata,]
         #myd1<-t(myd)
         DT::datatable(linked(),
                   class='compact',
                   escape = FALSE,selection = 'none')  
      })
    }
    )
Nischitha
  • 3
  • 3
  • 8
  • compare with `mean$column1` (producing the same error) – jogo Mar 15 '18 at 14:19
  • 1
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. We can't access your database so we can't run this code. Is that the exact error message you receive? – MrFlick Mar 15 '18 at 15:05
  • 1
    you have to find the line where the problem is. `object` in your case is probably not a `data.frame`, so you can't use `$a` If it's within a function, then there is probably somehting wrong with the class of one of your arguments. In the case of shiny, often the problem arise because the object just doesn't exist yet before you make some choices. If you run the code line by line not in shiny and it works, than the problem is with your shiny coding. – Bastien Mar 15 '18 at 16:15
  • Error in $: object of type 'closure' is not subsettable <- this is my actual error – Nischitha Mar 16 '18 at 03:57

1 Answers1

0

First, always use my_reactive() when you call a reactive function e.g. my_reactive.

Second, the object of type closure not subsettable usually means that the object you want to subset (here with $) cannot be found. You are not having the object not found error because you gave it a name already known to R.

As in the example of jogo, the same error occurs when trying to subset mean. mean is an object in R so it exists and R will not return object not found but it is a function and you cannot subset from it hence the error object is not subsettable.

Compare the results of the following lines of code.

mean[1]
mean <- c(1, 3)
mean[1]

Also note that R can still use mean to perform the mean of a numeric vector as it knows when to look for a function or for something else. But it is strongly advised not to do that. You should always properly name your objects with meaningful names.

maRmat
  • 363
  • 1
  • 14