【问题标题】:operator is invalid for atomic vectors Error with imported data from SQL to shiny运算符对原子向量无效 将数据从 SQL 导入到闪亮时出错
【发布时间】:2018-05-17 16:22:03
【问题描述】:

我尝试使用 (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 时出现错误(Orders$OtherLangDescription 中的错误: $ 运算符对原子向量无效)

【问题讨论】:

    标签: r shiny rstudio


    【解决方案1】:

    我通过将 sqlExecute 替换为 sqlQuery

    解决了这个错误

    【讨论】:

      猜你喜欢
      • 2019-04-29
      • 1970-01-01
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 1970-01-01
      • 2020-06-21
      相关资源
      最近更新 更多