【问题标题】:How to change input values in order to filter data in shiny?如何更改输入值以过滤闪亮的数据?
【发布时间】:2016-01-09 15:35:03
【问题描述】:

我需要详细说明我的标题。用户上传数据,然后应通过选择按钮过滤位置。但是,selectbutton 的选择是“New York”、“Washington”、“Los Angeles”,但是位置的名称是缩写。所以,我需要更改我的选择按钮输入,以便在我停止的地方过滤我的数据。

NO;Location
1.00;DC
2.00;DC
3.00;LA
4.00;NY
5.00;NY
6.00;LA
7.00;NY
8.00;DC
9.00;DC
10.00;NY
11.00;NY
12.00;LA
13.00;LA
14.00;DC
15.00;DC
16.00;DC
17.00;NY

updateselectinput 对我没有帮助。因为它完全改变了我不想要的侧边栏面板中的输入。

ui.R

 library(shiny)
  shinyUI(pageWithSidebar(
  sidebarPanel( 

wellPanel(fileInput('file1', 'Choose File', accept=c('text/csv',    'text/comma-separated-values,text/plain', '.csv'), multiple = TRUE),

          selectInput(inputId = "location",label = "Choose Location",
                      choices = c('All','New York', 'Washington', 'Los Angeles'), selected = "All")
          )),
mainPanel(
          tabsetPanel( id="tabs", tabPanel("Data",value="panel1",tableOutput("filteredtable"))
                       ))    ))

服务器.R

library(shiny)

shinyServer(function(input, output) {
   output$filteredtable <- renderTable({
    if (is.null(input$file1))
  return(NULL)
else {
  output$filetype <- renderText({
    ifelse(input$file1$type %in% c('text/csv', 
                                   'text/comma-separated-values',
                                   'text/plain', 
                                   '.csv'), 
           '', 
           HTML('<script type="text/javascript">alert("Please upload .csv    file!");</script>'))
  })}
read.csv(input$file1$datapath, header=TRUE, sep=";",
         quote='')

`uploadedfile1` <- read.csv(input$file1$datapath, sep=";")
datafiltered <- uploadedfile1

reactive({input$location <- c('All','NY', 'DC', 'LA')}) ## THIS PART IS PROBLEM!
if (input$location != "All"){

  datafiltered <- datafiltered[datafiltered$Location == input$location,]
}
datafiltered
 })})

【问题讨论】:

    标签: r csv shiny


    【解决方案1】:

    如果你给selectInput 传递choices 的命名列表,它将显示名称但返回值。在这种情况下,

    # ui.R
    library(shiny)
    shinyUI(pageWithSidebar(
        sidebarPanel( 
    
            wellPanel(fileInput('file1', 'Choose File', accept=c('text/csv',    'text/comma-separated-values,text/plain', '.csv'), multiple = TRUE),
    
                      selectInput(inputId = "location",label = "Choose Location",
                                  choices = ('All' = 'All',
                                             'New York' = 'NY', 
                                             'Washington' = 'DC', 
                                             'Los Angeles' = 'LA'), 
                                  selected = "All")
            )),
        mainPanel(
            tabsetPanel( id="tabs", tabPanel("Data",value="panel1",tableOutput("filteredtable"))
            ))    ))
    

    如果用户选择“华盛顿”,则 server.R 中的input$location 将是'DC'。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      • 2020-08-18
      • 1970-01-01
      • 2021-02-24
      • 1970-01-01
      • 2020-06-03
      • 2018-05-16
      相关资源
      最近更新 更多