问题
I have the shiny
app below in which I pass database password via shiny
app and then I want to connect it with rmd
document in order to further process this con
to produce plots. The issue is that I can get a succesfull connection outside of the shiny environment but when I try to do this via shiny
and rmd
I get:
Error : nanodbc/nanodbc.cpp:1021: 01S00: [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'bpsrawdata'. [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute
I believe that it does not read the string I give as password via the shiny
app properly.
app.r
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyjs)
library(knitr)
mytitle <- paste0("Life, Death & Statins")
dbHeader <- dashboardHeaderPlus(
titleWidth = "0px"
)
shinyApp(
ui = dashboardPagePlus(
header = dbHeader,
sidebar = dashboardSidebar(width = "0px"
),
body = dashboardBody(
useShinyjs(),
tags$script(HTML("$('body').addClass('fixed');")),
tags$head(tags$style(".skin-blue .main-header .logo { padding: 0px;}")),
passwordInput("pwd",label = "",value = "",width = "100%" ),
actionButton('button', "Continue"),
uiOutput('markdown')
)
),
server<-shinyServer(function(input, output,session) {
hide(selector = "body > div > header > nav > a")
output$markdown <- renderUI({input$button
isolate(HTML(markdown::markdownToHTML(knit('res.rmd', quiet = TRUE),fragment.only=TRUE)))
})
}
)
)
res.rmd
---
title: "res"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r pressure, echo=FALSE}
library(odbc)
svr<- readRegistry("SOFTWARE\\WOW6432Node\\ODBC\\ODBC.INI\\BPSRawData",
"HLM", maxdepth = 1)$Server[1]
sid<- readRegistry("SOFTWARE\\WOW6432Node\\Best Practice Software\\Best Practice\\General",
"HLM", maxdepth = 1)$SiteID[1]
con<- try(dbConnect(odbc(),
driver = "SQL Server",
database = "bpspatients",
server = svr,
port = 1433,
UID = "bpsrawdata",
PWD = input$pwd))
# define how many seconds are in a year
yrs<-31556952
# record the practice name
practicename<-dbGetQuery(con,"select practicename from practice")[1,1]
sqlo<- "select count(extractionoptout)
from patients where extractionoptout=1
and getdate()>dateadd(year,75,dob)"
# at least once after the age of 75
tm<-data.table(dbGetQuery(con,sqlm))
```
来源:https://stackoverflow.com/questions/65617837/achieve-a-database-connection-to-rmd-document-via-shiny-app