新手R和闪亮,正在开发我的第一个小应用程序。下面的代码按照我想要的方式工作,但是现在我需要扩展它,以便在加载它时在URL中有一个参数(类似于http://127.0.0.1:6804/?Sta_ID=1ABIR000.76),它在第一个查询中用来设置wqmdata数据框架。我已经知道如何使用parseQueryString函数从服务器代码块中的URL中获取该参数,但是我不知道如何在wqmdata数据帧的初始数据加载中使用它?该数据帧用于填充基于特定站点的UI中的一组内容(即,每个站点可以具有不同的监测水质参数列表)。
关于我正在做的事情的一些背景知识。最终,该pl45_wqm_data.csv文件将被替换为对SQL server数据库的调用,以获取应用程序的数据。该数据库有数千个监测站,有数百万个观察值,所以我显然只想带回初始调用所需的数据。其想法是有一个网址,可以从ArcGIS门户应用程序调用,因此用户可以使用交互式地图(带有一堆其他数据)来查找监测站,然后单击该监测站以启动R Shiny应用程序,以可视化该监测站的监测数据。
有什么想法可以试试吗?
library(shiny)
library(ggplot2)
library(tidyverse)
#Get monitoring data
wqmdata <- arrange(subset(read.csv(file="~\\R\\ShinyApps\\WQMGraphURL\\pl45_wqm_data.csv"
, fileEncoding="UTF-8-BOM"),Sta_ID == "1ABIR000.76"),Parameter,Fdt_Date_Time)
# Define UI for application that allows user to select a Parameter then get a graph
ui <- fluidPage(
tags$style(type='text/css',
".selectize-input {font-size: 12px; line-height: 12px;}
.selectize-dropdown {font-size: 11px; line-height: 11px;}"),
# Give the page a title
titlePanel(paste("DEQ Water Quality Monitoring Station Data for ",unique(wqmdata$Sta_ID))),
hr(),
# Generate a row with a sidebar
sidebarLayout(
# Define the sidebar with one input
sidebarPanel(
#dropdown to select parameter to be graphed
selectInput("SelectedParameter", "Select Parameter for Graph:",
choices=unique(wqmdata$Parameter)),
hr(),
# Button
downloadButton("downloadData", "Download Station Data"),
helpText("Download file contains all monitoring data including field data parameters")
),
# Create a spot for the barplot
mainPanel(
plotOutput("ParameterPlot")
)
)
)
# Define server logic required to draw graph
server <- function(input, output, session) {
GraphRecords <- reactive({
filter(wqmdata, Parameter == input$SelectedParameter)
})
GraphRecordsRows <- reactive({nrow(filter(wqmdata, Parameter == input$SelectedParameter))})
# Fill in the spot we created for a plot
output$ParameterPlot <-
renderPlot({
# Render the graph
graph.title <- "Parameter Data Graph"
yaxis.label <- paste(unique(GraphRecords()$Parameter)," Value")
if (GraphRecordsRows() == 1)
{ ggplot(GraphRecords(), aes(Fdt_Date_Time, Parameter_Value, group = 1)) +
geom_point() +
labs(x = "Sample Date", y = yaxis.label, title = graph.title) +
theme(axis.text.x = element_text(angle = 90,vjust = 0.5, hjust = 1)) +
geom_text(aes(label = Parameter_Value), angle = 70, hjust = 0, vjust = -0.5, size = 3) }
else
{ ggplot(GraphRecords(), aes(Fdt_Date_Time, Parameter_Value, group = 1)) +
geom_point() +
geom_line()+
labs(x = "Sample Date", y = yaxis.label, title = graph.title) +
theme(axis.text.x = element_text(angle = 90,vjust = 0.5, hjust = 1)) +
geom_text(aes(label = Parameter_Value), angle = 70, hjust = 0, vjust = -0.5, size = 3) }
})
# Downloadable csv of selected dataset ----
output$downloadData <- downloadHandler(
filename = function() {
paste(unique(wqmdata$Sta_ID), "_StationData.csv", sep = "")
},
content = function(file) {
write.csv(wqmdata, file, row.names = FALSE)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)发布于 2020-07-25 02:30:45
或多或少弄明白了。当页面第一次加载时,我收到一个“警告:错误在:美学必须是长度为1或与数据(1)相同: x,y”的错误(我猜是因为plotOutput在没有提供任何数据之前运行),但我会弄清楚如何解决这个问题。R中的反应式数据的整个想法对我来说非常陌生,但非常酷。
以下是更新后的代码。我仍然需要清理它,但这是有效的:
library(shiny)
library(ggplot2)
library(tidyverse)
# Define UI for application that allows user to select a Parameter then get a graph
ui <- fluidPage(
tags$style(type='text/css',
".selectize-input {font-size: 12px; line-height: 12px;}
.selectize-dropdown {font-size: 11px; line-height: 11px;}"),
# Give the page a title
titlePanel(textOutput("titleText")),
hr(),
# Generate a row with a sidebar
sidebarLayout(
# # Define the sidebar with one input
sidebarPanel(
#dropdown to select parameter to be graphed
selectInput("graphParameters","Select Parameter for Graph:","graphParameters"),
hr(),
# Button
downloadButton("downloadData", "Download Station Data"),
helpText("Download file contains all monitoring data including field data parameters")
),
# # Create a spot for the barplot
mainPanel(
plotOutput("ParameterPlot")
)
)
)
# Define server logic required to draw graph
server <- function(input, output, session) {
stationidParameter <- reactive({
query <- parseQueryString(session$clientData$url_search)
query[["stationid"]]
})
wqmdata <- reactive({arrange(subset(read.csv(file="~\\R\\ShinyApps\\WQMGraphURL2\\pl45_wqm_data.csv"
, fileEncoding="UTF-8-BOM"),Sta_ID == stationidParameter()),Parameter,Fdt_Date_Time)})
output$titleText <- renderText({paste("DEQ Water Quality Monitoring Station Data for ",unique(wqmdata()$Sta_ID))})
observe({updateSelectInput(session, "graphParameters", choices = unique(wqmdata()$Parameter))})
GraphRecords <- reactive({filter(wqmdata(), Parameter == input$graphParameters)})
GraphRecordsRows <- reactive({nrow(filter(wqmdata(), Parameter == input$graphParameters))})
# Fill in the spot we created for a plot
output$ParameterPlot <-
renderPlot({
# Render the graph
graph.title <- "Parameter Data Graph"
yaxis.label <- paste(unique(GraphRecords()$Parameter)," Value")
if (GraphRecordsRows() == 1)
{ ggplot(GraphRecords(), aes(Fdt_Date_Time, Parameter_Value, group = 1)) +
geom_point() +
labs(x = "Sample Date", y = yaxis.label, title = graph.title) +
theme(axis.text.x = element_text(angle = 90,vjust = 0.5, hjust = 1)) +
geom_text(aes(label = Parameter_Value), angle = 70, hjust = 0, vjust = -0.5, size = 3) }
else
{ ggplot(GraphRecords(), aes(Fdt_Date_Time, Parameter_Value, group = 1)) +
geom_point() +
geom_line()+
labs(x = "Sample Date", y = yaxis.label, title = graph.title) +
theme(axis.text.x = element_text(angle = 90,vjust = 0.5, hjust = 1)) +
geom_text(aes(label = Parameter_Value), angle = 70, hjust = 0, vjust = -0.5, size = 3) }
})
# Downloadable csv of selected dataset ----
output$downloadData <- downloadHandler(
filename = function() {
paste(unique(wqmdata()$Sta_ID), "_StationData.csv", sep = "")
},
content = function(file) {
write.csv(wqmdata(), file, row.names = FALSE)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)https://stackoverflow.com/questions/63072707
复制相似问题