动态UI,允许过滤器更新和绘制在闪亮的R



如果这是非常简单的事情,我很抱歉。。。但我似乎想不通。我创建了一个应用程序,它有一个对4个下拉菜单做出反应的绘图。前两个下拉菜单是针对x轴和y轴的(我可以很好地使用它们(。后两个是基于我的数据帧中的唯一值。我希望这两个下拉菜单相互反应,然后对情节反应。因此,基本上,我希望用户在第一个下拉菜单中选择一个参数,然后数据帧将被过滤(也进行绘图过滤(,然后第二个下拉菜单的选项也将被过滤为与第一个下拉列表的唯一匹配,反之亦然。我尝试了很多方法,但都没用。如有任何指导或帮助,我们将不胜感激!

样本数据:

df<-structure(list(stdate = structure(c(16611, 16611, 16615, 16615, 
14004, 14004, 16616, 16616, 16616, 17485, 17485, 17483, 17483, 
16678, 16678, 14000, 14000, 17211, 17211, 17210), class = "Date"), 
sttime = structure(c(37800, 37800, 35100, 35100, 42600, 42600, 
38700, 38700, 32400, 35400, 35400, 33000, 33000, 49800, 49800, 
34200, 34200, 37800, 37800, 30600), class = c("hms", "difftime"
), units = "secs"), locid = c("USGS-01388500", "USGS-01388500", 
"USGS-01464585", "USGS-01464585", "USGS-01464515", "USGS-01464515", 
"USGS-01407330", "USGS-01407330", "USGS-01466500", "USGS-01387500", 
"USGS-01387500", "USGS-01395000", "USGS-01395000", "USGS-01400860", 
"USGS-01400860", "USGS-01377000", "USGS-01377000", "USGS-01367625", 
"USGS-01367625", "USGS-01398000"), Specific_conductance = c(525, 
525, 184, 184, 226, 226, 203, 203, 41, 674, 674, 466, 466, 
312, 312, 540, 540, 844, 844, 683), HUC14 = c("HUC02030103110020", 
"HUC02030103110020", "HUC02040201100030", "HUC02040201100030", 
"HUC02040201060020", "HUC02040201060020", "HUC02030104070070", 
"HUC02030104070070", "HUC02040202030070", "HUC02030103100030", 
"HUC02030103100030", "HUC02030104050060", "HUC02030104050060", 
"HUC02030105090020", "HUC02030105090020", "HUC02030103170060", 
"HUC02030103170060", "HUC02020007010010", "HUC02020007010010", 
"HUC02030105030060"), tds = c(294L, 275L, 119L, 100L, 155L, 
116L, 155L, 115L, 43L, 403L, 382L, 286L, 274L, 177L, 173L, 
328L, 277L, 435L, 440L, 347L), Chloride = c(109, 109, 31.9, 
31.9, 33, 33, 36.4, 36.4, 3.38, 153, 153, 72.6, 72.6, 41.5, 
41.5, 105, 105, 179, 179, 161)), row.names = c(NA, -20L), class = c("tbl_df", 
"tbl", "data.frame"), .Names = c("stdate", "sttime", "locid", 
"Specific_conductance", "HUC14", "tds", "Chloride"))

我尝试过的:

library(ggplot2)
library(shiny)
library(shinydashboard)
header<-dashboardHeader()
sidebar<-dashboardSidebar()
body<-dashboardBody(

fluidRow(
box(width = 12, plotOutput("plot5"))),
fluidRow(
box(selectInput("x","x",choices = c("tds","Chloride","Specific_conductance"),selected = "Specific_conductance")),
box(selectInput("y","y",choices =c("tds","Chloride","Specific_conductance") ,selected = "tds")),
uiOutput("locid1"),
uiOutput("huc1")))
ui<- dashboardPage(
header = header,
sidebar = sidebar,
body = body
)
### Create server of app ###
server<- function(input,output,session){
output$locid1<- renderUI({
selectizeInput("locid","Select Locid",
choices = as.character(unique(df$locid)))
})
datasub<-reactive({
df[df$locid == input$locid,]
})
output$huc1<- renderUI({
selectizeInput("huc","Select HUC",
choices = unique(datasub()[,"HUC14"]),
selected = unique(datasub()[,"HUC14"])[1])
})
datasub2<-reactive({
datasub()[df$HUC14 == input$huc,]
})

output$plot5 <- renderPlot({
ggplot(data= datasub2(),aes_string(x=input$x,y=input$y))+
geom_point()+
geom_smooth(method = "lm", se = FALSE) +
ggtitle(input$locid,input$huc)
})
}
shinyApp(ui,server)

您可以使用subset来完成此操作。首先,您只选择那些locid是用户使用input$locid指定的记录,以获得反应数据帧。接下来,您将基于HUC14 == input$huc对该数据帧进行子集设置。

另外,请参阅使用unique(datasub()$HUC14)生成output$huc1值的正确方法

server<- function(input,output,session){
output$locid1<- renderUI({
selectizeInput("locid","Select Locid",
choices = as.character(unique(df$locid)))
})
datasub<-reactive({
foo <- subset(df, locid == input$locid)
return(foo)
})
output$huc1<- renderUI({
selectizeInput("huc","Select HUC",
choices = unique(datasub()$HUC14),
selected = unique(datasub()$HUC14)[1])
})
datasub2<-reactive({
foo <- subset(datasub(), HUC14 == input$huc)
return(foo)
})
output$plot5 <- renderPlot({
ggplot(data = datasub2(), aes_string(x=input$x, y=input$y))+
geom_point()+
geom_smooth(method = "lm", se = FALSE) +
ggtitle(input$locid, input$huc)
})
}

最新更新