我有闪亮的代码:
# ui.R
fluidPage(
selectInput("ci_risk_1000M", "<=",
choices = c("a" = 1, "b" = 2, "c" = 3, "d" = 4, "e" = 5, "f" = 6,
"g" = 7, "h" = 8, "i" = 9, "j" = 10, "k" = 11),
selected = 11),
tableOutput("ats")
)
# server.R
x <- 1:11
function(input, output, session) {
output$ats <- renderTable({x[x <= input$ci_risk_1000M]})
}
我想按所选输入过滤x
个值。但是,基本答案应该是从1到11的行的表,但它只显示值1,10,11。你知道为什么以及如何解决这个问题? sliderInput
不是选项,除非它可以显示为字符值,而不是整数。
答案 0 :(得分:1)
问题在于input$ci_risk_1000M
的输出,即character
类。
如果我们检查它在字符向量上的行为
x <= "11"
#[1] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE
即。只有第一个,第十个和第十一个为TRUE,因此子集返回
x[x <= "11"]
#[1] 1 10 11
另外,检查character
向量与sort
ing
sort(as.character(x))
#[1] "1" "10" "11" "2" "3" "4" "5" "6" "7" "8" "9"
我们需要将其转换为numeric/integer
output$ats <- renderTable({x[x <= as.integer(input$ci_risk_1000M)]})
-full code
library(shiny)
ui <- fluidPage(
selectInput("ci_risk_1000M", "<=",
choices = c("a" = 1, "b" = 2, "c" = 3, "d" = 4, "e" = 5, "f" = 6,
"g" = 7, "h" = 8, "i" = 9, "j" = 10, "k" = 11),
selected = 11),
tableOutput("ats")
)
# server.R
x <- 1:11
server <- function(input, output, session) {
output$ats <- renderTable({x[x <= as.integer(input$ci_risk_1000M)]})
}
shinyApp(ui, server)
-output