【问题标题】:Update data from table database SQL Server through Shiny app通过 Shiny 应用程序从表数据库 SQL Server 更新数据
【发布时间】:2019-07-02 10:34:42
【问题描述】:

我想开发一个闪亮的应用程序,我刚刚开始使用 R(所以你可以想象......)。

现在我设法将我闪亮的应用程序连接到 Microsoft SQL Server(通过 DBI 库)并显示我在这个数据库上的 3 个表之一。

目标是允许用户单击行并更改其上的数据。在这种情况下,只有一列仅采用 1 或 0 作为值。我设法创建了按钮,但我无法让应用程序让我点击行并更改数据。

这是我目前的代码:

# Load libraries
library(shiny)
library(shinydashboard)
library(RODBC)
library(odbc)
library(pool)
library(DBI)


# connect database
con = DBI::dbConnect(odbc::odbc(), 
                     Driver = "SQL Server", 
                     Server = "MyServer", 
                     Database = "test", 
                     Trusted_Connection = "True")


# Start the dashboard
ui = dashboardPage(
  dashboardHeader(title = 'My_first_App'),
  
  dashboardSidebar(
    sidebarMenu(
      menuItem("Tables", tabName = "tables", icon = icon("th")),
      menuItem("Perfomance Dashboard", tabName = "dashboard", icon = icon("dashboard"))
    )
  ),
  
  dashboardBody(
    tabItems(
      # item for tables(Table_1 in this case)
      tabItem(tabName = "tables",
              fluidRow(
                box(tableOutput("tbl")),

                # Tick box to allow the user to change data
                box(title = "Column_to_change",
                    checkboxInput("Column_to_change", "Column_to_change", FALSE)),
                
                #action buttons
                actionButton("submit", "Submit"),
                actionButton("new", "New")
              ))
    )
  )
)


server = function(input, output) {
  
  # We get the Table_1 to show all of it
  output$tbl = renderTable({
    sql = "SELECT * FROM Table_1;"
    query = sqlInterpolate(con, sql, id = input$ID)
    dbGetQuery(con, query)
    
  })
  
  
}

shinyApp(ui, server)

【问题讨论】:

  • (1) 要么您正在简化您的 sql 语句,要么您不需要 sqlInterpolate。我建议使用DBI::dbBind 而不是sqlInterpolate,但出于某种原因,我认为它更安全(我可能错了)。 (2) 常规表格不可编辑,上次我检查过。也许你想要DT? (3) 我强烈建议您考虑更多的反应性,以reactiveVal 或类似的方式处理数据,并根据该 RV 进行渲染。您将需要交互性,这是在 renderTable 中使用 dbGetQuery 无法做到的。
  • 感谢您的帮助,但我认为我做不到。我对 R 很陌生。

标签: r sql-server shiny shiny-reactivity


【解决方案1】:

您可以使用 DTEdit 包在 shinyApp 中创建可编辑的高质量和查看数据表。语法非常简单且用户友好。

https://github.com/jbryer/DTedit

my.insert.callback <- function(data, row) {
    mydata <- rbind(data, mydata)
    return(mydata)
}

my.update.callback <- function(data, olddata, row) {
    mydata[row,] <- data[1,]
    return(mydata)
}

my.delete.callback <- function(data, row) {
    mydata[row,] <- NULL
    return(mydata)
}


DTedit::dtedit(input, output,
       name = 'mycontacts',
       thedata = mydata,
       edit.cols = c('name', 'email', 'useR', 'notes'),
       edit.label.cols = c('Name', 'Email Address', 'Are they an R user?', 'Additional notes'),
       input.types = c(notes='textAreaInput'),
       view.cols = c('name', 'email', 'useR'),
       callback.update = my.update.callback,
       callback.insert = my.insert.callback,
       callback.delete = my.delete.callback)

【讨论】:

    猜你喜欢
    • 2015-09-01
    • 1970-01-01
    • 2016-08-20
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 2011-12-25
    • 1970-01-01
    相关资源
    最近更新 更多