我尝试使用(RODBCext)从SQL Server获取原始数据,然后将我的结果部署到shinyApp 这是我的代码:
#loading Packages
library(RODBC)
library(shiny)
library(rsconnect)
library(dplyr)
library(RODBCext)
#global code
#connect to DB
Test <- odbcDriverConnect("driver={SQL Server};server=***********;database=*******;uid=*****;pwd=******")
#build query
Orders<- sqlExecute(Test,"
SELECT
WHWorkOrderHeaderId
,OtherLangDescription
FROM Warehouse.WHWorkOrderDetails
INNER JOIN Warehouse.WHWorkOrderHeader AS WHH
ON Warehouse.WHWorkOrderDetails.WHWorkOrderHeaderId = WHH.ID
INNER JOIN Warehouse.StockItems
ON Warehouse.WHWorkOrderDetails.StockItemId = Warehouse.StockItems.Id
WHERE Type = 'IO'
ORDER BY OtherLangDescription ASC")
#close the connection
odbcClose(Test)
#return results
Orders$OtherLangDescription <- as.factor(Orders$OtherLangDescription)
orderList <- unique(Orders$OtherLangDescription)
ListId <- lapply(orderList, function(x) subset(Orders, OtherLangDescription == x)$WHWorkOrderHeaderId)
Initial_Tab <- lapply(ListId, function(x) subset(Orders, WHWorkOrderHeaderId %in% x)$OtherLangDescription)
Correlation_Tab <- mapply(function(Product, ID) table(Product)/length(ID),Initial_Tab, ListId)
colnames(Correlation_Tab) <- orderList
cor_per<- round(Correlation_Tab*100,2)
DF<-data.frame(row=rownames(cor_per)[row(cor_per)],
col=colnames(cor_per)[col(cor_per)], corr=c(cor_per))
DF
#ui.R code
ui <- fluidPage(
titlePanel("Item Correlation"),
sidebarPanel(
selectInput("Item","Select Item",choices= DF$FirstItem),
# ,selectInput("Item","SelectItem",choices= DF$SecondItem),
),
mainPanel(
tableOutput("Itemcorr")
)
)
#server.R code
server <- function(input,output){
data<- reactive({
input$Item
})
output$Itemcorr <- renderTable({
DF %>% filter(FirstItem == data()) %>% arrange(desc(X.Correlation))
})
}
shinyApp(ui, server)
我想直接从SQL获取数据而不导出到excel 但是当我运行我的shinyapp时出错(订单错误$ OtherLangDescription: $运算符对原子向量无效)
答案 0 :(得分:0)
我通过将 sqlExecute 替换为 sqlQuery
来解决此错误