正如标题所说,我正试图让Shiny显示一个SelectBox,这样我就可以从SQL Server表中动态选择记录,并根据CATEGORY的选择,显示表中的所有内容。下面的脚本必须关闭。我正在使SelectBox工作,但是当我做出选择时,什么也没有显示。
library(shiny)
library(RODBCext)
shinyApp(
ui =
shinyUI(
fluidPage(
uiOutput("select_category"),
tableOutput("display_data")
# plotOutput("plot_data")
)
),
server = shinyServer(function(input, output, session){
# A reactive object to get the query. This lets you use
# the data in multiple locations (plots, tables, etc) without
# having to perform the query in each output slot.
QueriedData <-
reactive({
req(input$showDrop)
ch <- odbcDriverConnect('driver={SQL Server};server=MyServer;database=Northwind;trusted_connection=true')
showList <- sqlExecute(ch,
"SELECT * FROM [NORTHWND].[dbo].[Customers] WHERE [CUSTOMERID] = ?",
data = list(AnalyteId = input$showDrop),
fetch = TRUE,
stringsAsFactors = FALSE)
odbcClose(ch)
showList
})
# The select input control. These can be managed dynamically
# from the server, and then the control send back to the UI
# using `renderUI`
output$select_category <-
renderUI({
ch <- odbcDriverConnect('driver={SQL Server};server=MyServer;database=Northwind;trusted_connection=true')
showList <- sqlExecute(ch,
"SELECT DISTINCT AnalyteId From [NORTHWND].[dbo].[Customers] ORDER BY [CUSTOMERID]",
fetch = TRUE,
stringsAsFactors = FALSE)
odbcClose(ch)
selectInput(inputId = "showDrop",
label = "Select Asset",
showList$AnalyteId)
})
# Display the data in a table
output$display_data <-
renderTable({
QueriedData()
})
# Display a plot
# output$plot_data <-
# renderPlot({
# plot(QueriedData()) # fill in the plot code you want to use.
# })
})
)
有人能帮我搞定吗?此外,您可以使表格动态选择,还是不是一个选项?
谢谢!
答案 0 :(得分:1)
您似乎错过了一些组件。一些说明:
您的UI定义无效。 UI的每个参数都应该产生某种UI元素。您定义连接并从SQL Server导出数据的行将不会按照您期望的方式行事。您应该在服务器上执行这些操作,或者应该全局定义它们。
您正在检索output$cumReturn
广告位中的数据,但您正在使用renderPlot
来执行此操作。这有点不相干。如果要渲染绘图,则应生成绘图。如果您希望显示数据,则应使用renderTable
(或类似内容)。
您还没有在UI中的任何位置显示cumReturn
输出槽,因此实际上从未调用过查询。
最后,output$cumReturn
中的查询在进入服务器时会失败。我猜你的意思是在input$showDrop
语句中使用WHERE
,但你的查询没有这样的陈述。这不会在上面的代码中输出错误,因为您从不尝试呈现cumReturn
输出,因此永远不会调用查询。
以下是您的代码的变体,应该会生成属于该类别的数据表。
library(shiny)
library(RODBCext)
shinyApp(
ui =
shinyUI(
fluidPage(
uiOutput("select_category"),
tableOutput("display_data"),
plotOutput("plot_data")
)
),
server = shinyServer(function(input, output, session){
# A reactive object to get the query. This lets you use
# the data in multiple locations (plots, tables, etc) without
# having to perform the query in each output slot.
QueriedData <-
reactive({
req(input$showDrop)
ch <- odbcDriverConnect('...')
showList <- sqlExecute(ch,
"SELECT * FROM dbo.Analyte WHERE AnalyteId = ?",
data = list(AnalyteId = input$showDrop),
fetch = TRUE,
stringsAsFactors = FALSE)
odbcClose(ch)
showList
})
# The select input control. These can be managed dynamically
# from the server, and then the control send back to the UI
# using `renderUI`
output$select_category <-
renderUI({
ch <- odbcDriverConnect('...')
showList <- sqlExecute(ch,
"SELECT DISTINCT AnalyteId FROM dbo.Analyte ORDER BY AnalyteId",
fetch = TRUE,
stringsAsFactors = FALSE)
odbcClose(ch)
selectInput(inputId = "showDrop",
label = "Select Asset",
showList$AnalyteId)
})
# Display the data in a table
output$display_data <-
renderTable({
QueriedData()
})
# Display a plot
output$plot_data <-
renderPlot({
plot(QueriedData()) # fill in the plot code you want to use.
})
})
)
答案 1 :(得分:0)
library(shiny)
library(RODBCext)
shinyApp(
ui =
shinyUI(
fluidPage(
uiOutput("select_category"),
tableOutput("display_data")
# plotOutput("plot_data")
)
),
# server needs the function; looks ok
server = shinyServer(function(input, output, session)
{
# A reactive object to get the query. This lets you use
# the data in multiple locations (plots, tables, etc) without
# having to perform the query in each output slot.
QueriedData <- reactive({
req(input$showDrop)
ch <- odbcDriverConnect("driver={SQL Server};server=SERVER;database=DB;trusted_connection=true")
showList <- sqlExecute(ch, "SELECT * FROM [DB].[dbo].[TABLE] WHERE Category = ?",
data = list(Category = input$showDrop),
fetch = TRUE,
stringsAsFactors = FALSE)
odbcClose(ch)
showList
})
# The select input control. These can be managed dynamically
# from the server, and then the control send back to the UI
# using `renderUI`
output$select_category <- renderUI({
ch <- odbcDriverConnect("driver={SQL Server};server=SERVER;database=DB;trusted_connection=true")
showList <- sqlExecute(ch, "Select Distinct Category From [DB].[dbo].[TABLE] Order by Category",
fetch = TRUE,
stringsAsFactors = FALSE)
odbcClose(ch)
selectInput(inputId = "showDrop",
label = "Select Asset",
showList$Category)
})
# Display the data in a table
output$display_data <- renderTable({
QueriedData()
})
# Display a plot
# output$plot_data <-
# renderPlot({
# plot(QueriedData()) # fill in the plot code you want to use.
# })
})
)