1

I'm trying to pass the node value of a simple network as an argument to a function in Shiny R. However, I'm getting this error:

Error in rsqlite_send_query: no such column: input$id

Can anyone help with this issue? Thanks.

library(shiny)
library(networkD3)
ui <- shinyUI(fluidPage(
fluidRow(
    column(4, simpleNetworkOutput("simple")),
    column(4, DT::dataTableOutput("table"))
)
))

server <- shinyServer(function(input, output, session) { 
  session$onSessionEnded(stopApp)
  output$simple <- renderSimpleNetwork({
  sn<-simpleNetwork(df)
  sn$x$options$clickAction = 'Shiny.onInputChange("id",d.name)'
  sn
})

 output$table <- DT::renderDataTable(DT::datatable(get(funct(input$id))))

})

shinyApp(ui = ui, server = server) 
Poppin
  • 13
  • 3
  • What are `df`, `funct`? Please add `library(DT)` – HubertL Aug 08 '17 at 02:32
  • @HubertL library(DT) is already included. df is the data frame. funct is the function I want to pass the node value to. funct <- function (n){ isp <- sprintf("Select df.`age` From df Where df.name=%s;", deparse(substitute(n))) isd <- sqldf(isp) return(isd) } – Poppin Aug 08 '17 at 02:45
  • Possible duplicate of [Extracting node information from networkd3 to a reactive variable in shiny](https://stackoverflow.com/questions/45217609/extracting-node-information-from-networkd3-to-a-reactive-variable-in-shiny) – CJ Yetman Aug 08 '17 at 06:13
  • It's recommended to include some data and all (though minimal) code to reproduce, including the loading of packages. Please have a look at https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – HubertL Aug 08 '17 at 19:17

1 Answers1

0
  1. take out the deparse and substitute from your sprintf command, and add single quotes around the value you want to match in the SQL statement you're generating

  2. get rid of the get function because you're not trying to "get" an object

for example....

library(shiny)
library(networkD3)
library(DT)
library(sqldf)

df <- read.csv(header = T, text = '
source,name,age,hair
dad,Jon X,18,brown
dad,Jon Y,22,blonde
')

funct <-
  function (n) {
    isp <- sprintf("Select df.age From df Where df.name='%s';", n)
    isd <- sqldf::sqldf(isp)
    return(isd)
  }

ui <- shinyUI(fluidPage(
  fluidRow(
    column(4, simpleNetworkOutput("simple")),
    column(4, DT::dataTableOutput("table"))
  )
))

server <- shinyServer(function(input, output, session) { 
  session$onSessionEnded(stopApp)
  output$simple <- renderSimpleNetwork({
    sn<-simpleNetwork(df)
    sn$x$options$clickAction = 'Shiny.onInputChange("id",d.name)'
    sn
  })

  output$table <- DT::renderDataTable(DT::datatable(funct(input$id)))
})

shinyApp(ui = ui, server = server)

however, if all you want is to display a value associated with a given selection, I highly suggest drastically reducing the complexity to something like this

library(shiny)
library(networkD3)

df <- read.csv(header = T, text = '
source,name,age,hair
dad,Jon X,18,brown
dad,Jon Y,22,blonde
')

ui <- shinyUI(fluidPage(
  fluidRow(
    column(4, simpleNetworkOutput("simple")),
    column(4, textOutput("text"))
  )
))

server <- shinyServer(function(input, output, session) { 
  session$onSessionEnded(stopApp)
  output$simple <- renderSimpleNetwork({
    sn <- simpleNetwork(df)
    sn$x$options$clickAction <- 'Shiny.onInputChange("id", d.name)'
    sn
  })

  output$text <- renderPrint({ df$age[df$name == input$id] })
})

shinyApp(ui = ui, server = server)
CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
  • It does. If I link the nodes to URLs, the clickAction works. It also works when I use output$table <- DT::renderDataTable(DT::datatable(get((input$id))). So, I don't see any problem with the clickAction feature. Thanks for your input, but I'm looking for a solution to the error I mentioned. I'm trying to pass the value to a function, which gives the error "no such column". – Poppin Aug 08 '17 at 10:37
  • sorry, you're right, sort of... we recently made `simpleNetwork()` an alias of `forceNetwork()`, so it gains that ability from `forceNetwork()`... see my updated answer – CJ Yetman Aug 08 '17 at 12:11
  • It works! I used get() in order to get the data frame from sqldf. Now I see where I was going wrong.Thanks a lot! – Poppin Aug 08 '17 at 12:45
  • After getting rid of deparse and substitute from the sprintf command, the code works, but it doesn't work for ids with similar names. For instance, there are two nodes in my simpleNetwork "Jon X" and "Jon Y". It seems that the input$id here considers only first word. What can possibly be done in this case? Thank you. – Poppin Aug 30 '17 at 15:46
  • Try some of the ideas here https://stackoverflow.com/questions/6502134/query-for-exact-match-of-an-string-in-sql?rq=1 – CJ Yetman Aug 30 '17 at 17:16
  • Or check the data in your df carefully? Sounds like maybe it’s not what you think it is? – CJ Yetman Aug 30 '17 at 17:35
  • I modified the examples so they have "Jon X" and "Jon Y" as the names... it still works for me. – CJ Yetman Aug 31 '17 at 06:21
  • Based on your second patch of code, I don't just want to print the information based on the selection/ click, but pass it into the queries and then call those queries to execute another network. – Poppin Aug 31 '17 at 14:00
  • Maybe you should open a new question about how to redraw a plot in shiny with new data? StackOverflow seems to discourage super long comment threads that diverge from the original question. – CJ Yetman Aug 31 '17 at 14:40
  • Sure! Thanks for the help. – Poppin Sep 01 '17 at 13:19