【发布时间】: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