是否可以重定向用户输入以确定第二个用户输入的“选择”(用户输入选项)?

时间:2019-04-26 14:35:41

标签: r shiny user-input

我正在创建一个闪亮的仪表板,并尝试重定向用户输入以确定第二个用户输入的“选择”(用户输入选项)。我正在加载一个csv文件,其中包含用户输入将依赖的数据:

示例:csv的一列包含一个客户编号。有时会有重复的客户编号,因为该客户处于多种状态(因此,在2个不同的行中有一个客户编号链接到两个不同的状态)

如果在第一个用户输入中选择了该客户编号,我将如何使用该输入在第二个用户输入下拉菜单中创建2个状态选项?

我已附上我的代码:

bcl <- read.csv("C:/Users/jtberg/Desktop/rand_data.csv", stringsAsFactors = FALSE)


ui <- shinyUI(fluidPage(

  titlePanel("UW Model Output"),

  sidebarLayout(

    sidebarPanel(

      selectInput("customername", "Customer Name",
                  choices = sort(unique(bcl$Customer_Name))),
      #starting second version with dependent input variable choices
      uiOutput("secondSelection")

    ),
    mainPanel(("Results"),
            textOutput("customername"),
            textOutput("secondSelection"))
  )
)
)
server <- function(input, output){
  #output$columns <- renderUI({
    #n <- input$customername
    #myspecies <- bcl[(bcl$Quintiles == n),11]
    #mydata = get(myspecies)
    #selectInput('pstate', 'Policy State', choices = names(mydata))
  output$secondSelection <- renderUI({
    selectInput("pstate", "Maybe: ", choices = as.character(bcl[bcl$Customer_Name == input$customername, "name"]))
  })

}

shinyApp(ui = ui, server = server)

部分2: 如果我想要并使用用户输入来创建动态表显示,以便在选择客户编号和状态时,该对应行的其余部分(客户编号和状态所在的位置)中的所有内容都显示在数据表,我该怎么做?

示例:              (用户输入列)

    |      Col 1     |  Col 2     |  Col 3   |    Col 4   |    Col 5 | 
    ------------------------------------------------------------------            
    |   CusNumInput | StateInput |    A     |      B     |      C   |
    ------------------------------------------------------------------

用户输入为(CusNumInput和StateInput),因此打印其中包含A,B和C的表。

1 个答案:

答案 0 :(得分:0)

是可以的。由于无法访问您的数据集,因此必须创建自己的数据集。

关键在于使过滤器的数据集依赖于先前的选择。

我还为您的第二个过滤器添加了条件功能作为奖励,您可能需要根据需要将其删除。

ui <- fluidPage(

  #
  titlePanel("UW Model Output"),

  sidebarLayout(
    sidebarPanel(
      uiOutput("firstSelection"),
      uiOutput("secondSelection")
    ),
    mainPanel(
    )
  )
)



server <- function(input, output){

  dat <- data.frame(CustomerNumber = c(1,1,1,2,2,3,4,5), State = c("AZ","FL","IL","AZ","NC","IN","WI", "CO"), stringsAsFactors = FALSE)

  output$firstSelection <- renderUI ({
       selectInput(inputId = "First", "First Filter",
                   choices = c("",paste0(sort(as.character(unique(dat$CustomerNumber)))))
    )
  })

  output$secondSelection <- renderUI ({
      Lvl2 <- dat[dat$CustomerNumber == input$First,]

      conditionalPanel(condition = "input.First != ''",
        selectInput(inputId = "Second", "Second Filter",
                  choices = paste0(sort(unique(Lvl2$State)))
                  )
        )
  })

}

shinyApp(ui, server)

已更新

library (DT)

ui <- fluidPage(

  #
  titlePanel("UW Model Output"),

  sidebarLayout(
    sidebarPanel(
      uiOutput("firstSelection"),
      uiOutput("secondSelection")
    ),
    mainPanel(
      DT::dataTableOutput("table1")
    )
  )
)



server <- function(input, output){

  dat <- data.frame(CustomerNumber = c(1,1,1,2,2), 
                    State = c("AZ","FL","IL","AZ","NC"), 
                    Col3 = c("B","D","C","E","F"),
                    Col4 = c("G","H","I","J","K"),
                    Col5 = c("L","M","N","O","P"),
                    stringsAsFactors = FALSE)

  #Fitler Selections
  output$firstSelection <- renderUI ({
    selectInput(inputId = "First", "First Filter",
                choices = c("",paste0(sort(as.character(unique(dat$CustomerNumber)))))
    )
  })

  output$secondSelection <- renderUI ({
    Lvl2 <- dat[dat$CustomerNumber == input$First,]

    conditionalPanel(condition = "input.First != ''",
                     selectInput(inputId = "Second", "Second Filter",
                                 choices = paste0(sort(unique(Lvl2$State)))
                     )
    )
  })

  #Data Table
  output$table1 <- DT::renderDataTable({
    temp <- dat[dat$CustomerNumber == input$First & dat$State == input$Second,]
    temp <- temp[-c(1:2)]


  },
  rownames = FALSE)

}

shinyApp(ui, server)