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

时间:2017-08-08 02:02:39

标签: r shiny networkd3

我试图将简单网络的节点值作为参数传递给Shiny R中的函数。但是,我收到此错误:

rsqlite_send_query出错:没有这样的列:输入$ id

任何人都可以帮忙解决这个问题吗?感谢。

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 个答案:

答案 0 :(得分:0)

  1. deparse命令中取出substitutesprintf,并在要生成的SQL语句中围绕要匹配的值添加单引号

  2. 摆脱get函数,因为你没有试图“获取”一个对象

  3. 例如......

    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)
    

    然而,如果您想要显示与给定选择相关联的值,我强烈建议将复杂性大幅降低到像这样的

    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)