在Ui.R我有一些输入选择,其中Location是一,如果选择“All”,我希望能省略相同的,
selectInput('Location', 'Location', choices = c("All", unique(sampleData$Location)), selected = "All"),
我尝试使用反应性if else,但是有一个错误说 -
“评估错误:只能对数字,逻辑或复杂类型进行操作。”
我是新手,我可以用这种方式使用inpLocation吗? 任何帮助表示赞赏。
这是我的整个代码 -
library(plotly)
library(shiny)
load("sample_Data.rdata")
nms <- names(sample_Data)
ui <- (pageWithSidebar(
headerPanel("Demo"),
sidebarPanel(
selectInput('sex', 'Sex', choices = unique(sample_Data$sex), selected = "F"),
selectInput('Location', 'Location', choices = c("All", unique(sample_Data$Location)), selected = "All"),
selectInput('color1', 'Color1', choices = c('None', nms), selected = "region"),
selectInput('color2', 'Color2', choices = c('None', nms), selected = "species")
),
mainPanel(
fluidRow(
column(12, plotlyOutput("p1"))
),
fluidRow(
column(12, plotlyOutput("p2"))
)
)
))
server <- function(input, output, session) {
nms <- row.names(sample_Data)
dataset <- reactive({
inpLocation <- reactive({
if(input$Location == "All"){
sample_Data$Location
}else{
input$Location
}})
sample_Data %>%
filter(sex %in% input$sex, inpLocation())
})
output$p1 <- renderPlotly({
p<-qplot(year,N,data=dataset(),color=species)
if (input$color1 != 'None') p <- p + aes_string(color=input$color1)
p<-ggplotly(p)
p
})
output$p2 <- renderPlotly({
p<-qplot(region,N,data=dataset(),color=species)
if (input$color2 != 'None') p <- p + aes_string(color=input$color2)
p <- ggplotly(p)
p
})
}
shinyApp(ui, server, options = list(display.mode = "showcase"))
答案 0 :(得分:1)
您不需要第一个reactive
电话内的第二个reactive
。在reactive
外部,只要input$Location
发生更改,它就会更新。
dataset <- reactive({
if(input$Location == "All"){
inpLocation <- sample_Data$Location
}else{
inpLocation <- input$Location
}
sample_Data %>%
filter(sex %in% input$sex,
Location %in% inpLocation)
})