R 闪亮:根据输入选择输入子集
编辑:感谢您的帮助,我的代码存在多个问题,但主要问题是我缺少一条观察语句,以下解决了问题:R 闪亮:根据输入选择输入子集,r,ggplot2,shiny,R,Ggplot2,Shiny,编辑:感谢您的帮助,我的代码存在多个问题,但主要问题是我缺少一条观察语句,以下解决了问题: get_ddf <- reactive({ filter(poskick, Name == input$player) }) observe({ updateSelectInput(session, 'fixture', choices =levels(droplevels(get_ddf()$Event)) ) }) 我的代码 用户界面: 库(闪亮)
get_ddf <- reactive({
filter(poskick, Name == input$player)
})
observe({
updateSelectInput(session, 'fixture', choices =levels(droplevels(get_ddf()$Event)) )
})
我的代码
用户界面:
库(闪亮)
图书馆(GG2)
poskick您的代码中有一些问题,比如使用Fixtu,它没有引用任何内容。另外,我认为levels()可能比attributes()更好地获得因子变量中的唯一值
我发现,当您希望一个小部件中的输入控制另一个小部件中的输入时,在server.R文件中使用renderUI是很有帮助的。然后,您可以输入返回语句,以防止小部件在知道提供什么选项之前出现。为此,我添加了一个“选择一个”选项,使下一个小部件甚至不显示。如果您可以将selectInput默认值设置为NULL,那会更好,但这不是一个选项
以下是我所做的:
服务器.R:
library(shiny)
library(ggplot2)
poskick<-read.csv('poskicks.csv')
shinyServer(function(input, output) {
output$Box1 = renderUI(selectInput('player',
'Player',
c(levels(poskick$Name),"pick one"),
"pick one")
)
output$Box2 = renderUI(
if (is.null(input$player) || input$player == "pick one"){return()
}else selectInput('fixture',
'Match',
c(levels(poskick$Event[which(poskick$Name == input$player)]),"pick one"),
"pick one")
)
subdata1 = reactive(poskick[which(poskick$Name == input$player),])
subdata2 = reactive(subdata1()[which(subdata1()$Event == input$fixture),])
output$plot <- renderPlot({
if (is.null(input$player) || is.null(input$fixture)){return()
} else if(input$player == "pick one" || input$fixture == "pick one") { return()
} else p <- ggplot(data = subdata2()) + geom_segment(aes(x = x_coord, y = y_coord, xend = x_coord_end, yend = y_coord_end))
print(p) })
})
你有什么问题?您的具体问题是什么?updateSelectInput()
函数允许您根据服务器上的逻辑更新选项。有关示例代码,请参见。
library(shiny)
library(ggplot2)
poskick<-read.csv('poskicks.csv')
shinyUI(pageWithSidebar(
headerPanel("position map"),
sidebarPanel(
selectInput('player', 'Player', choices= attributes(poskick$Name)),
selectInput('fixture', 'Match', choices= attributes(firstsub()$Fixtu))
),
mainPanel(
plotOutput('plot')
)
))
library(shiny)
library(ggplot2)
poskick<-read.csv('poskicks.csv')
shinyServer(function(input, output) {
firstsub <- reactive({
subset(poskick, poskick$Name %in% input$player)
})
secondsub <- reactive({
subset(poskick, poskick$Fixtu %in% input$fixture & poskick$Name %in% input$player )
})
output$plot <- renderPlot({
p <- ggplot(data = secondsub()) + geom_segment(aes(x = x_coord, y = y_coord, xend = x_coord_end, yend = y_coord_end))
print(p) }, height=700)
})
library(shiny)
library(ggplot2)
poskick<-read.csv('poskicks.csv')
shinyServer(function(input, output) {
output$Box1 = renderUI(selectInput('player',
'Player',
c(levels(poskick$Name),"pick one"),
"pick one")
)
output$Box2 = renderUI(
if (is.null(input$player) || input$player == "pick one"){return()
}else selectInput('fixture',
'Match',
c(levels(poskick$Event[which(poskick$Name == input$player)]),"pick one"),
"pick one")
)
subdata1 = reactive(poskick[which(poskick$Name == input$player),])
subdata2 = reactive(subdata1()[which(subdata1()$Event == input$fixture),])
output$plot <- renderPlot({
if (is.null(input$player) || is.null(input$fixture)){return()
} else if(input$player == "pick one" || input$fixture == "pick one") { return()
} else p <- ggplot(data = subdata2()) + geom_segment(aes(x = x_coord, y = y_coord, xend = x_coord_end, yend = y_coord_end))
print(p) })
})
library(shiny)
library(ggplot2)
shinyUI(pageWithSidebar(
headerPanel("position map"),
sidebarPanel(uiOutput("Box1"),uiOutput("Box2")),
mainPanel(plotOutput('plot')
)
))