【问题标题】:R Shiny filter with two inputsR 带两个输入的闪亮滤波器
【发布时间】:2018-04-05 14:55:03
【问题描述】:

您好,我需要通过输入过滤数据的帮助。单独选择输入可以正常工作。我尝试类似

output$tbl<-renderTable({subset(data,data$country==input$countryIn|| data$code==input$code)})

但它不起作用。我希望用户选择国家,然后可以将代码写入文本输入并减少数据区域。

ui.R
library(shiny)



shinyUI(fluidPage(

titlePanel("This is my website"),  

sidebarLayout(

sidebarPanel(uiOutput(outputId="country"),textInput(inputId="code",label="writte code")),

mainPanel(tableOutput(outputId="tbl"))


))
)
server.R
library(shiny)

id<-1:6
country<-c("Germany","UK","USA","Poland","UK","UK")
code<-c(255,124,234,751,124,326)
data<-data.frame(id,country,code)


shinyServer(

function(input,output)
{
output$country<-renderUI({selectInput(inputId ="countryIn",label="Choose country",choices=unique(data$country))})
output$tbl<-renderTable({subset(data,data$country==input$countryIn)})
})

【问题讨论】:

    标签: r input shiny


    【解决方案1】:

    首先,我重构了您的代码,因为它对我来说真的很难阅读。 (我也把这两个文件合并成app.R,不过没必要)

    第二:你使用了错误的逻辑运算符。在这种情况下使用||有两个问题:

    1. 您需要 and &amp;&amp;&amp; 运算符,而不是 or |||,因为您想按 2 个条件进行过滤。

    2. 较长的形式从左到右计算,仅检查每个向量的第一个元素。这不是你想要的,你需要检查向量的所有元素是否等于过滤条件。

    所以你会写subset(data, data$country == input$countryIn &amp; data$code == input$code),但它仍然不起作用,因为textInput 的默认值是一个空字符串(""),不会产生任何结果。

    因此有两种情况:当数据仅由code 过滤时,另一种情况是两个过滤器都在起作用。您可以为此使用if 语句:

      output$tbl <- renderTable( {
        if(input$code == "") subset(data, data$country == input$countryIn)
        else subset(data, data$country == input$countryIn & data$code == input$code)
      })  
    

    请注意,您可能希望延迟验证,因为在textInput 字段中缓慢输入会刷新表格。

    完整代码:

    library(shiny)
    
    id <- 1:6
    country <- c("Germany", "UK", "USA", "Poland", "UK", "UK")
    code <- c(255,124,234,751,124,326)
    data <- data.frame(id, country, code)
    
    
    ui <- fluidPage(
      titlePanel("This is my website"),
      sidebarLayout(
        sidebarPanel(
          uiOutput(outputId="country"),
          textInput(inputId="code",label="writte code")),
        mainPanel(tableOutput(outputId="tbl"))
      )
    )
    
    server <- function(input,output) {
      output$country <- renderUI( {
        selectInput(inputId ="countryIn", label="Choose country", choices=unique(data$country))
      })
    
      output$tbl <- renderTable( {
        if(input$code == "") subset(data, data$country == input$countryIn)
        else subset(data, data$country == input$countryIn & data$code == input$code)
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      【解决方案2】:

      有几种方法可以做到这一点。但是,最简单的方法是使用if - else 子句,如下所示:

      output$tbl<-renderTable({
              if(input$code == ""){
                subset(data, input$countryIn == data$country)
              } else{
                subset(data, input$code == data$code & input$countryIn == data$country)
              }
            })
      

      【讨论】:

        猜你喜欢
        • 2018-08-09
        • 2020-08-18
        • 1970-01-01
        • 2015-01-19
        • 1970-01-01
        • 2014-08-02
        • 2018-05-16
        • 1970-01-01
        • 2020-03-19
        相关资源
        最近更新 更多