通过闪亮保存对 sqlite db 的更改 [英] Save changes to sqlite db via shiny

查看:31
本文介绍了通过闪亮保存对 sqlite db 的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以运行连接到 sqlite 数据库的 Shiny 应用程序,并且可以进行更改并保存到该数据库?我的问题类似于这个问题 - R 脚本不会在 sqlite db 中编写,如果我在 Shiny 中运行脚本 但没有被接受的答案,所以不确定它是否有效.

Is it possible to run a Shiny app that connects to a sqlite database, and that can make changes and save to that database? My question is similar to this questions - R script do not write in sqlite db if I run the script in shiny but there was no accepted answer, so not sure if it works or not.

推荐答案

是的.这是可能的,这是一个例子:

Yes. It is possible and here is an example:

创建一个简单的数据库:

Create a simple db:

library(RSQLite)
con <- dbConnect(SQLite(), dbname="sample.sqlite")
dbWriteTable(con, "test", data.frame(value1 = letters[1:4], value2 = letters[5:8]))
dbDisconnect(con)

闪亮应用:

library(shiny)
library(RSQLite)

runApp(list(
  ui = bootstrapPage(
    textInput("value1", label = "Value 1"),
    textInput("value2", label = "Value 2"),
    actionButton("action", label = "Write to DB"),
    hr(),
    tableOutput("table")
  ),
  server = function(input, output){
    data <- eventReactive(input$action, {
    con <- dbConnect(SQLite(), dbname="sample.sqlite")
    dbWriteTable(con, "test", data.frame(value1 = input$value1, value2 = input$value2, stringsAsFactors = FALSE), append = TRUE)
    data <- dbReadTable(con, "test")
    dbDisconnect(con)
    return(data)
  })
  output$table <- renderTable(data())
}))

这篇关于通过闪亮保存对 sqlite db 的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆