【问题标题】:Uploading csv to SQL table using R shiny使用 R 闪亮将 csv 上传到 SQL 表
【发布时间】:2018-06-20 22:35:18
【问题描述】:

我一直在摸索着想弄清楚这一点。

所以我已经连接到数据库,但是当我按下操作按钮时,表格没有发生任何事情。

CSV 正在转换为数据框。

用户界面

library(shiny)
library(RJDBC)
library(dbtools)
library(jsonlite)
library(shinyjs)
library(DBI) 

# App title ----
  titlePanel("Uploading Files"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Select a file ----
      fileInput("file1", "Choose CSV File",
                multiple = TRUE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")),
      tags$head(
        tags$style(HTML(
          '#Uploadbutton{background-color:cyan}'
        ))
      ),

      actionButton("Uploadbutton","Upload"),
      p("Upload Members if data looks ok")

    ),

    # Main panel for displaying outputs ----
    mainPanel(

      # Output: Data file ----
      tableOutput("contents")
    )

  )
)

服务器

server <- function(input, output) {

  output$contents <- renderTable({

    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, head of that data file by default,
    # or all rows if selected, will be shown.

    req(input$file1)

    data <- read.csv(input$file1$datapath,header=TRUE)

    if(input$disp == "head") {
      return(head(data))
    }
    else {
      return(data)
    }
    data <- data.frame()
    data <<- read.csv(input$file1$datapath,header=TRUE)

    testdata <- read.csv("data",sep=",",row.names=1)

  observeEvent(input$Uploadbutton, {
       insert_into("data", "ANALYTICS.TEST_DATASTORE", con=lol, rows_per_statement=1)

        })

               }

  )

【问题讨论】:

  • insert_into() 是您自己的用户定义方法还是来自 R 库?
  • 这是一个名为 dbtools 的包。数据库在 db2 上,是否有替代包可供使用。
  • 认为它是一个被修改的内部包(员工离开公司),还有什么其他包做同样的工作?
  • 请在实际帖子中包含library 行。请参阅 编辑 链接。然后删除你上面的评论。还有什么套餐?查看您用于插入过程的库的文档,但您需要遍历数据框行!对于 DB2,将需要 RJDBC。
  • 但是,虽然我无法确定 insert_into,但请检查参数。没有你在哪里拉入数据框对象,data(未引用)。看看如何在这个方法中定义源表和目标表。

标签: sql r database dataframe shiny


【解决方案1】:

您好,我认为您正在寻找类似的东西

DBI::dbWriteTable(con=lol, name = "ANALYTICS.TEST_DATASTORE",value = dta(),append = TRUE)

我也会对服务器函数进行一些不同的构造,这样我们就不需要使用全局变量了

server <- function(input, output) {

  dta <- reactive({
    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, head of that data file by default,
    # or all rows if selected, will be shown.

    req(input$file1)

    data <- read.csv(input$file1$datapath,header=TRUE)

    if(input$disp == "head") {
      return(head(data))
    }
    else {
      return(data)
    }

  })

  output$contents <- renderTable({
    dta()
  })

  observeEvent(input$Uploadbutton, {
    DBI::dbWriteTable(con=lol, name = "ANALYTICS.TEST_DATASTORE",value = dta(),append = TRUE)
  })
}

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    这将为您上传文件。然后,将数据发送到 SQL Server。

    library(shiny)
    
    # Define UI for data upload app ----
    ui <- fluidPage(
    
      # App title ----
      titlePanel("Uploading Files"),
    
      # Sidebar layout with input and output definitions ----
      sidebarLayout(
    
        # Sidebar panel for inputs ----
        sidebarPanel(
    
          # Input: Select a file ----
          fileInput("file1", "Choose CSV File",
                    multiple = TRUE,
                    accept = c("text/csv",
                             "text/comma-separated-values,text/plain",
                             ".csv")),
    
          # Horizontal line ----
          tags$hr(),
    
          # Input: Checkbox if file has header ----
          checkboxInput("header", "Header", TRUE),
    
          # Input: Select separator ----
          radioButtons("sep", "Separator",
                       choices = c(Comma = ",",
                                   Semicolon = ";",
                                   Tab = "\t"),
                       selected = ","),
    
          # Input: Select quotes ----
          radioButtons("quote", "Quote",
                       choices = c(None = "",
                                   "Double Quote" = '"',
                                   "Single Quote" = "'"),
                       selected = '"'),
    
          # Horizontal line ----
          tags$hr(),
    
          # Input: Select number of rows to display ----
          radioButtons("disp", "Display",
                       choices = c(Head = "head",
                                   All = "all"),
                       selected = "head")
    
        ),
    
        # Main panel for displaying outputs ----
        mainPanel(
    
          # Output: Data file ----
          tableOutput("contents")
    
        )
    
      )
    )
    
    # Define server logic to read selected file ----
    server <- function(input, output) {
    
      output$contents <- renderTable({
    
        # input$file1 will be NULL initially. After the user selects
        # and uploads a file, head of that data file by default,
        # or all rows if selected, will be shown.
    
        req(input$file1)
    
        df <- read.csv(input$file1$datapath,
                 header = input$header,
                 sep = input$sep,
                 quote = input$quote)
    
        if(input$disp == "head") {
          return(head(df))
        }
        else {
          return(df)
        }
    
      })
    
    }
    # Run the app ----
    shinyApp(ui, server)
    

    【讨论】:

      【解决方案3】:

      感谢所有帮助他们弄清楚如何做到这一点的人,这是服务器端。

      服务器

      输出$内容

      # input$file1 最初将为 NULL。用户选择后 # 并上传一个文件,默认为该数据文件的头部, # 或所有行(如果选择)将显示。

      请求(输入$file1)

      数据

      返回(数据)

      })

      observeEvent(input$Uploadbutton,

      {insert_into(read.csv(input$file1$datapath),"ANALYTICS.TEST_DATASTORE")},once=TRUE )

      }

      【讨论】:

        猜你喜欢
        • 2017-02-28
        • 2017-11-15
        • 2021-12-16
        • 1970-01-01
        • 1970-01-01
        • 2015-10-14
        • 2015-05-16
        • 2015-10-13
        • 2014-09-12
        相关资源
        最近更新 更多