operator is invalid for atomic vectors Error with imported data from SQL to shiny

守給你的承諾、 提交于 2020-01-07 09:20:25

问题


I try to get my raw data from SQL Server using (RODBCext) and then deploy my results to my shinyApp this is my code:

    #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)

i want to get my data from SQL directly without exporting to excel But i have an error when i run my shinyapp (Error in Orders$OtherLangDescription : $ operator is invalid for atomic vectors)


回答1:


I Solved this error by replacing sqlExecute with sqlQuery



来源:https://stackoverflow.com/questions/47629356/operator-is-invalid-for-atomic-vectors-error-with-imported-data-from-sql-to-shin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!