基于URL参数的初始数据帧加载
对R和Shiny来说都是新手,正在开发我的第一个小应用程序。下面的代码按照我的要求工作,但现在我需要扩展它,以便在加载它时,URL中有一个参数,类似于在第一次查询中用于设置wqmdata数据帧的参数。我已经知道如何使用parseQueryString函数从服务器代码块的URL中获取该参数,但是我不知道如何在wqmdata数据帧的初始数据加载中使用它?该数据框用于填充基于特定站点的UI中的一组内容,即每个站点可以有不同的监测水质参数列表 关于我要做什么的一些背景知识。最终,pl45_wqm_data.csv文件将替换为调用SQL server数据库以获取应用程序的数据。该数据库有数千个监测站,有数百万个观测值,所以我显然只想带回最初通话中需要的数据。其想法是有一个可以从ArcGIS门户应用程序调用的URL,这样用户就可以使用交互式地图和一堆其他数据来查找监测站,然后单击该站启动R Shining应用程序,以可视化该站的监测数据 有什么想法可以试试吗基于URL参数的初始数据帧加载,r,shiny,R,Shiny,对R和Shiny来说都是新手,正在开发我的第一个小应用程序。下面的代码按照我的要求工作,但现在我需要扩展它,以便在加载它时,URL中有一个参数,类似于在第一次查询中用于设置wqmdata数据帧的参数。我已经知道如何使用parseQueryString函数从服务器代码块的URL中获取该参数,但是我不知道如何在wqmdata数据帧的初始数据加载中使用它?该数据框用于填充基于特定站点的UI中的一组内容,即每个站点可以有不同的监测水质参数列表 关于我要做什么的一些背景知识。最终,pl45_wqm_dat
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)
或多或少地弄明白了。我想当页面第一次加载时会出现“警告:错误:美学必须是长度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)
或多或少地弄明白了。我想当页面第一次加载时会出现“警告:错误:美学必须是长度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)
如果错误源于缺少绘图数据,请尝试在渲染器中使用reqGraphRecords或其他数据帧如果错误源于缺少绘图数据,请尝试在渲染器中使用reqGraphRecords或其他数据帧