这是我的代码。
我有两个问题。第一个是y轴上的比例尺,不能自动运行,第二个输入$ dateRange不能用于控制。
y轴上的比例尺该怎么做?它显示所有数据,而不是序列,但我要像50100150200那样进行序列和自动缩放。
我想使用输入日期范围来控制数据显示在图表ggplot上,但我想在sql查询中修复input $ dateRange,该怎么办?
我使用解决方案'“,{object},”',但是它有错误和无法完成的解决方案子集。
library(shiny)
library(shinydashboard)
library(DT)
library(DBI)
library(dbplyr)
library(dplyr)
library(ggplot2)
#connection to database
conn <- dbConnect(
drv = RPostgres::Postgres(),
dbname = "imed_mfu",
host = "61.19.253.36",
user = "imedapp",
password = "app_imed@#",
port = 5434)
#sql query for select check group
que <- dbGetQuery(conn, paste0(
"SELECT weight,height, pressure_min, measure_date FROM vital_sign_opd ;"
))
ui <- dashboardPage(
dashboardHeader(title ="First Design"),
dashboardSidebar(
sidebarMenu( id = 'dataset',
#menu
menuItem("Vital Sign OPD", tabName = "vitalSignOpd", icon = icon("th")),
menuItem("Patient", tabName = "patient", icon = icon("th"))
)
),
dashboardBody(
tabItems(
# First tab content
tabItem(tabName = "vitalSignOpd",
fluidRow(
column(6,#column(4,
box(
title = "Health",width = 100,
checkboxGroupInput("checkGroup", "Choose types to display",
choices = list("Weight" = 1, "Height" = 2,
"Temperature" = 3, "Pulse" = 4),
selected = 1)
)
),
column(6,#column(4,
box(
title = "Date",width = 100,
dateRangeInput('dateRange',
label = 'Measure date range input: yyyy-mm-dd',
start = min(que$measure_date), end = max(que$measure_date)
)
)
)#,
#column(4,box(
# title = "Time",width = 100,
# dateRangeInput('timeRange',
# label = 'Time range input: hh-mm-ss',
# start = Sys.time() , end = Sys.time()
# )
# )
#)
),
fluidRow(
column(12, box(width = 100,
title = "Visualization",
conditionalPanel("input.checkGroup == 1",
plotOutput("testPlot") # plotOutput("scatterPlot", height = 300)
),
conditionalPanel("input.checkGroup == 2",
plotOutput("heightPlot")
)
)
)
),
fluidRow(
column(12, box(width = 100,
title = "Dataset",
downloadButton("downloadData1", "Download"),
conditionalPanel(
'input.dataset === "vitalSignOpd"',
DT::dataTableOutput("vitalSignOPDTable")
)
)
)
)
),
# Second tab content
tabItem(tabName = "patient",
fluidRow(
column(4,
box(
title = "Gender",width = 100,
checkboxGroupInput("gendeGroup", "Choose gender to display",
choices = list("Male" = 1, "Female" = 2),
selected = 1)
)
),
column(4,
box(
title = "Blood Types",width = 100,
checkboxGroupInput("bloodGroup", "Choose blood types to display",
choices = list("A" = 1, "B" = 2,"AB" = 3, "O" = 4),
selected = 1)
)
)
),
fluidRow(
column(12, box(width = 100,
title = "Dataset",
# Button
downloadButton("downloadData2", "Download"),
conditionalPanel(
'input.dataset === "patient"',
DT::dataTableOutput("patientTable")
))
)
),
#CSS for download button
tags$style(type='text/css', "#downloadData1 { margin-bottom: 20px;}"),
tags$style(type='text/css', "#downloadData2 { margin-bottom: 20px;}")
)
)
)
)
server <- function(input, output) {
output$testPlot <- renderPlot({
#dataframe
keep <- data.frame(date = as.Date(que$measure_date), weight = que$weight, que$pressure_min)
# plot graph
ggplot(keep, aes(x=date,y=weight)) + geom_point(colour="#75AADB")
#+ geom_line(aes(y=que$pressure_min), colour="green")
#plot(as.Date(que$measure_date, format = "%F"),que$weight,
# ylab="Value",
# xlab="Date",col = "#75AADB", type = "l")
})
output$heightPlot <- renderPlot({
#datafram
keep2 <- data.frame(date = as.Date(que$measure_date), height = que$height, que$pressure_min)
# plot graph
ggplot(keep2, aes(x=date)) + geom_point(aes(y=height) , colour="#75AADB")
})
#data from table
output$vitalSignOPDTable = DT::renderDataTable({
#on.exit(dbDisconnect(conn), add = TRUE)
dbGetQuery(conn, paste0(
"SELECT vital_sign_opd_id, measure_date, measure_time, weight, height, pressure_max, pressure_min, temperature, pulse FROM vital_sign_opd ;"
))
})
output$patientTable = DT::renderDataTable({
#on.exit(dbDisconnect(conn), add = TRUE)
dbGetQuery(conn, paste0(
"SELECT patient_id, blood_group, fix_gender_id, fix_marriage_id FROM patient;"
))
})
# Downloadable csv of selected dataset ----
output$downloadData1 <- downloadHandler(
filename = function() {
paste("vital_sign_opd", ".csv", sep = "")
},
content = function(file) {
write.csv(dbGetQuery(conn, paste0(
"SELECT vital_sign_opd_id, measure_date, measure_time, weight, height, pressure_max, pressure_min, temperature, pulse FROM vital_sign_opd ;"
)), file, row.names = FALSE)
}
)
output$downloadData2 <- downloadHandler(
filename = function() {
paste("patientData", ".csv", sep = "")
},
content = function(file) {
write.csv(dbGetQuery(conn, paste0(
"SELECT patient_id, blood_group, fix_gender_id, fix_marriage_id FROM patient;"
)), file, row.names = FALSE)
}
)
}
shinyApp(ui, server)