我有一个shinydashboard(在这里使用shinydashboard包),我从用户输入他的用户名和密码,使用它连接到Redshift,使用他的凭据执行Redshift查询然后想要显示查询的输出作为DataTable。
不知何故,当我这样做时,查询的输出不会显示,并且在没有抛出任何错误的情况下失败。
以下是代码的摘录:
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)
注意:如果我使用renderTable函数和tableOutput函数,代码就可以工作,并显示表格。但是我想使用dataTableOutput函数和renderDataTable输出。
答案 0 :(得分:0)
语法看起来很好,我的猜测是你的ltst_qtr_data被动变量为空。
确保此行有效:
ltst_qtr_data <- reactive({RPostgres::dbGetQuery(conn(),given_sql_query) })