0

I have a shinydashboard (using the shinydashboard package here) where I take input from the user for his username and password, connect to Redshift using it,execute a Redshift query using his credentials and then want to display the output of the query as a DataTable.

Somehow, the output of the query is not displayed when I do so and it fails without throwing any error.

Here is an excerpt from the code:

library(shinydashboard)
library(DBI)
library(shiny)
library(shinyjs)
library(RPostgreSQL)
library(stringi)
library(data.table)
library(DT)
library(dplyr)
    ui <- dashboardPage(
     dashboardHeader(title = "Basic dashboard"),
     dashboardSidebar(
       sidebarMenu(
         menuItem("Connect to RedShift", # 1, variables in this will start with menu_1_
         tabName = "connect_to_RS"
          ))),
     dashboardBody(
        tabItems(
          tabItem(tabName = "connect_to_RS",
                tabBox(
                  title = "",width = 12,
                  tabPanel(title = "Connection to Redshift", 
                  #declare 2 text inputs and submit button
                  fluidRow(
                     column(3,offset = 0,
                     textInput(inputId = "menu_1_tab_1_ltst_qtr",label = "Most Recent Quarter")),
                     column(3,offset = 0,
                                    textInput(inputId = "menu_1_tab_1_RS_user",label = "Redshift Username")),
                     column(3,offset = 0,
                     passwordInput(inputId = "menu_1_tab_1_RS_pwd",label = "Redshift Password")),
                     column(12,
                     submitButton(text = "Submit"))
                     ),
                     mainPanel(
                             dataTableOutput("menu_1_tab_1_table_1")

                           )
                  )
      )
    ))))
    server <- function(input, output) {



        conn <- reactive({
            RPostgres::dbConnect(
            drv = RPostgres::Postgres(),
            host = "xxxx",
            port = xxxx,
            dbname = 'xx',
            user = input$menu_1_tab_1_RS_user,
            password = input$menu_1_tab_1_RS_pwd)
            })
            ltst_qtr_data <- reactive({RPostgres::dbGetQuery(conn(),given_sql_query)  })
output$menu_1_tab_1_table_1 <- renderDataTable({
    expr = ltst_qtr_data()
  })
}
shinyApp(ui,server)

Note: The code does work if I user renderTable function along with tableOutput function, and the table is shown. But I want to use the dataTableOutput function along with renderDataTable output.

Gompu
  • 415
  • 1
  • 6
  • 21

1 Answers1

0

Syntax looks fine, My guess is your ltst_qtr_data reactive variable is null.

Make sure this line is working:

 ltst_qtr_data <- reactive({RPostgres::dbGetQuery(conn(),given_sql_query)  })
Victor Burnett
  • 588
  • 6
  • 10