简体   繁体   English

将参数传递给闪亮的R中的函数

[英]Passing argument to a function in shiny R

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: 我试图将简单网络的节点值作为参数传递给Shiny R中的函数。但是,出现此错误:

Error in rsqlite_send_query: no such column: input$id rsqlite_send_query中的错误:没有这样的列: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. 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 取出deparsesubstitutesprintf命令,周围添加值单引号你想在你生成的SQL语句来匹配

  2. get rid of the get function because you're not trying to "get" an object 摆脱get函数,因为您不是要“获取”对象

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM