从R

时间:2017-09-15 14:27:38

标签: r shiny shiny-server

我创建了一个数据框(df)并用闪亮的R中的第一行中的数字填充它。现在我希望在上传时在下拉列表中看到数据框的每个变量元素的索引一份文件。换句话说,我想使用索引来选择元素而不是列名。我知道这可能看起来很奇怪,但我真的需要帮助。我的示例代码如下:

 **ui.R**

 shinyUI(fluidPage(
 titlePanel(""),
 sidebarLayout(
 sidebarPanel(
  fileInput("file", "Upload the file", 
            accept=c('txt', 'text files', '.txt')),
  tags$hr(style="padding:0px;margin:0px"),
  selectInput(inputId = "table_no", label = "Select table", choices = "Pending Upload"),

  ),


 **server.R**
 shinyServer(function(input, output, session){ 

 data <- reactive({
 file1 <- input$file
 if(is.null(file1)){return()}
 dta <- read.csv(file1$datapath, header = TRUE, fill = TRUE)

 trial <- 1:5
 df <- data.frame(matrix(trial, ncol = length(trial), nrow = 1, byrow = TRUE), stringsAsFactors = FALSE)
 colnames(df) <- paste("Table",trial)

1 个答案:

答案 0 :(得分:1)

您可以使用索引而不是列名,就像使用R中的列索引进行子集一样。闪亮的唯一区别是selectInput的值是一个字符串,因此您必须使用{{ 1}}。

简单的工作流程:

  1. 使用列计数填充as.numeric()列索引:selectInput
  2. 使用1:ncol(data())
  3. 子集data.frame

    我使用虹膜数据集进行演示。它也可以用于反应值。

    示例:

    data()[, as.numeric(input$table_no)]

    正如Samuel指出的那样,请务必查看如何创建可重现的示例:How to make a great R reproducible example?