【发布时间】:2018-03-23 13:27:01
【问题描述】:
在 Ui.R 我有一些输入选择,其中 Location 是一个,如果选择“All”,我希望能够省略相同,
selectInput('Location', 'Location', choices = c("All", unique(sampleData$Location)), selected = "All"),
我尝试使用响应式 if else,但出现错误提示 -
“评估错误:操作只能用于数字、逻辑或复杂类型。”
我是 Shiny 的新手,我可以这样使用 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"))
【问题讨论】: