代码之家  ›  专栏  ›  技术社区  ›  Poppin

将参数传递给R中的函数

  •  1
  • Poppin  · 技术社区  · 7 年前

    我试图将简单网络的节点值作为参数传递给Shining R中的函数。但是,我遇到了以下错误:

    有人能帮助解决这个问题吗?谢谢

    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 回复  |  直到 7 年前
        1
  •  0
  •   CJ Yetman    7 年前
    1. 取出 deparse substitute sprintf 命令,并在要在生成的SQL语句中匹配的值周围添加单引号

    2. 摆脱 get 函数,因为您不想“获取”对象

    例如

    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)