Passing argument to a function in shiny R

匆匆过客 提交于 2019-12-24 08:19:20

问题


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) 

回答1:


  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)


来源:https://stackoverflow.com/questions/45558198/passing-argument-to-a-function-in-shiny-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!