【问题标题】:Shiny: Select option automatically takes data variables闪亮:选择选项自动采用数据变量
【发布时间】:2013-09-01 11:29:16
【问题描述】:

我正在尝试在 ui 中选择选项,它应该自动将变量名称从输入数据中提取到列表中。这里我在选择选项中使用了 list(ls(input.file1),但它不起作用。

请帮帮我。

ui.R:

library(shiny)
shinyUI(pageWithSidebar(
  headerPanel( "Demand Forecast", "Flowserve"),
  sidebarPanel(
    fileInput('file1', 'Select csv file',
              accept=c('text/csv')
              ),
    checkboxInput('header', 'Header', TRUE),
    radioButtons('sep', 'Separator',
                 c(Comma=',', Semicolon=';', Tab='\t')
                 ),
    tags$hr(),
    selectInput("product", "Select Product",
                  list(ls(input.file1))

                )
))

服务器.R:

library(shiny)
shinyServer(function(input,output){

#Assigning data to a variable "data1"  
  data1 = reactive({
  inFile<-input$file1
  if(is.null(inFile))
  return(NULL)
  read.csv(inFile$datapath, header=input$header, sep=input$sep)
  })

  sub=reactive({
      subset(data1(), select=paste0(input$product))
  })

 output$contents<-renderTable({
       if (is.null(input$file1)) { return() }                            
           sub()
       })
 })

这是 csv 示例:

Product1    Product2    Product3
5             10               17
8             16               26
10            20               32
16            32               50
18            36               56
20            40               62

【问题讨论】:

  • 提供简短的 csv 样本时,获得答案的机会会更大。
  • 好的...我已经编辑了我的问题

标签: r shiny


【解决方案1】:

如果您在代码中看到ls(),那几乎总是错误的。然而,棘手的部分是从服务器设置一个 ui 项:您需要 update---- 系列函数。

这是将 csv 的名称填充到产品表中的部分。您必须添加更多标准代码来完成填充。

#server.r
library(shiny)
shinyServer(function(input,output,session){

  observe({
    inFile<-input$file1
    print(inFile)
    if(is.null(inFile))
      return(NULL)
    dt = read.csv(inFile$datapath, header=input$header, sep=input$sep)
    ## Decide later what to do with the data, here we just fill
    updateSelectInput(session, "product", choices = names(dt))
  })
})

#ui.r
library(shiny)
shinyUI(pageWithSidebar(
  headerPanel( "Demand Forecast", "Flowserve"),
  sidebarPanel(
    fileInput('file1', 'Select csv file',
              accept=c('text/csv')
    ),
    checkboxInput('header', 'Header', TRUE),
    radioButtons('sep', 'Separator',
                 c(Tab='\t', Comma=',', Semicolon=';' )
    ),
    tags$hr(),
    selectInput("product", "Select Product","")
    ),
  mainPanel(tableOutput('contents'))

  ))

【讨论】:

  • 现在,在“选择产品”选项中,它将所有产品集中在一个选项中。但我想要那些分开。
  • 不适合我;但可能我不明白你想要什么。
  • 我使用了你提供的代码。在这个应用程序的网页中,“选择选项”中只有一个选项可供选择。
    如果我选择上面的csv示例,它会选择像“Product1.Product2.Product3”这样的选项。但我想要 3 个单独的选择(“Product1”“Product2”Product3”)
  • 您使用了错误的数据文件;尝试真正使用制表符分隔的文件,事情将按要求工作。您必须自己创建它。请测试这个 Witout Shiny,以检查您的输入是否正常。
猜你喜欢
  • 2018-03-17
  • 2018-09-20
  • 1970-01-01
  • 1970-01-01
  • 2015-01-15
  • 2018-08-02
  • 1970-01-01
  • 1970-01-01
  • 2018-10-01
相关资源
最近更新 更多