You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shiny应用用户输入向量传入字符串函数及PostgreSQL查询需求

Passing Shiny Checkbox Input to PostgreSQL Query Function

Got it, let's walk through how to make this work smoothly in your Shiny app. Here's what you need to do to pass the user-selected libraries from your checkboxGroupInput to your get_query function:

1. Handle the Reactive Input Safely

First, since input$libraries is a reactive value (it changes when the user selects/deselects checkboxes), you’ll need to access it inside a reactive context—like a reactive() function, observeEvent(), or a render function.

Start by using req(input$libraries) to ensure the input isn’t empty before running your query (this prevents errors when the app first loads):

req(input$libraries) # Blocks execution until the user makes a selection

2. Format the Input for SQL

Your get_query function expects a string, so you need to convert the character vector from input$libraries into a format that works with a SQL IN clause. The exact format depends on whether your libraryid is a string or numeric type:

If libraryid is a string:

Wrap each ID in single quotes and separate with commas:

selected_libs <- input$libraries
libs_str <- paste0("'", paste(selected_libs, collapse = "','"), "'")
# Example output: 'lib_001','lib_002','lib_003'

If libraryid is numeric:

Just collapse the vector into a comma-separated string:

selected_libs <- input$libraries
libs_str <- paste(selected_libs, collapse = ",")
# Example output: 1,2,3

3. Build the Query and Call get_query

Now embed your formatted string into your SQL query, then pass it to get_query. Here’s a complete example using a reactive function to fetch and store results:

# Reactive function to fetch part counts from PostgreSQL
library_part_counts <- reactive({
  req(input$libraries)
  
  selected_libs <- input$libraries
  # Adjust this based on your libraryid data type
  libs_str <- paste0("'", paste(selected_libs, collapse = "','"), "'")
  
  # Build your SQL query
  sql_query <- paste0(
    "SELECT l.name, COUNT(p.part_id) AS part_count
     FROM parts p
     JOIN libraries l ON p.libraryid = l.libraryid
     WHERE p.libraryid IN (", libs_str, ")
     GROUP BY l.name"
  )
  
  # Execute the query with your get_query function
  get_query(sql_query)
})

# Example: Render results in a table
output$part_count_table <- renderTable({
  library_part_counts()
})

A Quick Note on Security

If your libraryid values are controlled (i.e., they come from your LibraryIDs data frame, not free user input), the above approach is safe. But if you ever need to handle free-form user input, use parameterized queries instead of string concatenation to avoid SQL injection. If your get_query function supports parameter passing (many PostgreSQL wrappers like DBI do), you could adjust it like this:

# Parameterized query example (if get_query accepts params)
sql_query <- "SELECT l.name, COUNT(p.part_id) AS part_count
              FROM parts p
              JOIN libraries l ON p.libraryid = l.libraryid
              WHERE p.libraryid IN ($1)
              GROUP BY l.name"

query_result <- get_query(sql_query, params = list(input$libraries))

内容的提问来源于stack exchange,提问作者Beeba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:26:47