Achieve a database connection to rmd document via shiny app

冷暖自知 提交于 2021-02-11 12:54:17

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!