使用R shiny将csv上传到SQL表

时间:2018-01-11 14:46:05

标签: sql r database dataframe shiny

我一直在试图解决这个问题。

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

正在将CSV转换为数据框。

UI

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)

        })

               }

  )

3 个答案:

答案 0 :(得分:0)

嗨,我认为你要找的是像

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

希望这有帮助!

答案 1 :(得分:0)

这将为您上传文件。然后,将数据发送到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)

答案 2 :(得分:0)

感谢所有帮助人员想出如何做到这一点,这就是服务器端。

  

服务器&lt; - 函数(输入,输出,会话){

     

输出$ contents&lt; - DT :: renderDataTable({

     

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

     

REQ(输入$文件1)

     

data&lt; - read.csv(输入$ file1 $ datapath,header = TRUE)

     

返回(数据)

     

})

     

observeEvent(输入$ Uploadbutton,

     

{insert_into(read.csv(输入$ $的file1数据路径), “ANALYTICS.TEST_DATASTORE”)},一旦= TRUE        )

     

}