我想使用R Shiny将CSV数据上传到MySQL表并显示

发布于 2025-01-25 17:21:38 字数 2403 浏览 3 评论 0原文

对一个漫长的问题表示歉意。我编写了以下R脚本,以将我定期收到的一些CSV文件上传到MySQL数据库。示例文件和架构结构简化了。

test.csv
v1,v2,v3
33,78,44
4,49,15
87,132,98

数据库架构称为 Shiny ,该表被称为 tab1 ,列为列,如下所示:
ID-主钥匙,自动收入
v1 -varchar(10)
v2 -varchar(10)
V3 -VARCHAR(10)

第一个代码段中的基本r脚本工作正常,并将CSV文件中的数据附加到架构表中,并将自动收入ID编号添加到SQL表中。

我希望一个闪亮的应用程序可以执行相同的操作(上传并附加到MySQL表),然后显示仅在DT表中上传的数据。第二件作品中的闪亮代码除以下问题外:

  1. 数据上传到架构表,但ID列填充了V1数据,带有V2数据的V1列,带V3数据的V2列,V3 Data和V3列为NULL
  2. 除了“ true”的值“ true”的

任何想法或链接具有可能解决方案的任何想法或链接的值之外,将不显示出可能的解决方案。

有效的基本r脚本如下:

library(RMySQL)

#Read in data to a data frame
data <- read.csv(test.csv", header = TRUE, sep = ",")

# input database access values
user = 'user'
password = 'password'
host = 'host'
dbname='shiny'

#connect to database
con <- dbConnect(MySQL(),
                 user = user,
                 password = password,
                 host = host,
                 dbname = shiny)

# write to database
dbWriteTable(conn = con, name = 'tab1', value = data, append = TRUE, header = TRUE, row.names=FALSE)

# remove data and inputs and disconnect
rm(data)
rm(dbname)
rm(host)
rm(password)
rm(user)
rm(con)
lapply(dbListConnections(MySQL()), dbDisconnect)

闪亮的代码IM尝试如下:

# Shiny app for SQL append
library(shiny)
library(DT)
library(RMySQL)

# database access information
user = 'root'
password = 'rustymarmot'
host = 'localhost'
dbname='shiny'

#connect to database
con <- dbConnect(MySQL(),
                 user = user,
                 password = password,
                 host = host,
                 dbname = shiny)

## USER INTERFACE
ui <- fluidPage(
  
  ## App title
  titlePanel("SQL Upload"),
  
  ## Sidebar layout 
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", label = "File input", accept = c(".xlsx", ".xls", ".csv", ".ods"))
    ),
    ## End Sidebar layout
    mainPanel(
      tableOutput("contents")
    )
  )
)

# SERVER
server = shinyServer(function(input,output){
  output$contents = renderTable({
    inFile <- input$file1
    
    if (is.null(inFile))
      return(NULL)
    
    read.csv(inFile$datapath, header = TRUE)
    dbWriteTable(conn = con, name = 'tab1', value = inFile$datapath, append = TRUE, header = TRUE, row.names=FALSE)
  })
})

shinyApp(ui, server)

Apologies for the long question. I wrote the following R script to upload/append some csv files i get on a regular basis to a MySQL database. The examples files and schema structure are simplified.

test.csv
v1,v2,v3
33,78,44
4,49,15
87,132,98

The database schema is called shiny and the table is named tab1 with columns as follows:
id - primary key, auto-increment
v1 - varchar(10)
v2 - varchar(10)
v3 - varchar(10)

The basic R script in the first code snippet works fine and appends the data in the CSV file to the schema table and an auto-incrementing id number is added to the SQL table.

I would like a shiny app to do the same (upload and append to a MySQL table) and then display the data just uploaded in a DT table. The Shiny code in the second snippet sort of works except for the following issues:

  1. The data gets uploaded to the schema table but the id column is populated with v1 data, v1 column with v2 data, v2 column with v3 data and v3 column is NULL
  2. Data not displayed in a DT table except for the value “TRUE”

Any ideas or links to pages with possible solutions would be greatly appreciated.

The basic R script that works is as follows:

library(RMySQL)

#Read in data to a data frame
data <- read.csv(test.csv", header = TRUE, sep = ",")

# input database access values
user = 'user'
password = 'password'
host = 'host'
dbname='shiny'

#connect to database
con <- dbConnect(MySQL(),
                 user = user,
                 password = password,
                 host = host,
                 dbname = shiny)

# write to database
dbWriteTable(conn = con, name = 'tab1', value = data, append = TRUE, header = TRUE, row.names=FALSE)

# remove data and inputs and disconnect
rm(data)
rm(dbname)
rm(host)
rm(password)
rm(user)
rm(con)
lapply(dbListConnections(MySQL()), dbDisconnect)

The shiny code im trying is as follows:

# Shiny app for SQL append
library(shiny)
library(DT)
library(RMySQL)

# database access information
user = 'root'
password = 'rustymarmot'
host = 'localhost'
dbname='shiny'

#connect to database
con <- dbConnect(MySQL(),
                 user = user,
                 password = password,
                 host = host,
                 dbname = shiny)

## USER INTERFACE
ui <- fluidPage(
  
  ## App title
  titlePanel("SQL Upload"),
  
  ## Sidebar layout 
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", label = "File input", accept = c(".xlsx", ".xls", ".csv", ".ods"))
    ),
    ## End Sidebar layout
    mainPanel(
      tableOutput("contents")
    )
  )
)

# SERVER
server = shinyServer(function(input,output){
  output$contents = renderTable({
    inFile <- input$file1
    
    if (is.null(inFile))
      return(NULL)
    
    read.csv(inFile$datapath, header = TRUE)
    dbWriteTable(conn = con, name = 'tab1', value = inFile$datapath, append = TRUE, header = TRUE, row.names=FALSE)
  })
})

shinyApp(ui, server)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄想挽回 2025-02-01 17:21:38

几乎不需要更改。这样使用,您可以正确显示和上传数据。

data <- read.csv(inFile$datapath, header = TRUE)
dbWriteTable(conn = con, name = 'tab1', value = data, append = TRUE, header = TRUE, row.names=FALSE)
data

little changes needed. Use like this and you have both the data displayed and uploaded correctly.

data <- read.csv(inFile$datapath, header = TRUE)
dbWriteTable(conn = con, name = 'tab1', value = data, append = TRUE, header = TRUE, row.names=FALSE)
data
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文