2017-09-15 9 views
1

私は作成されたデータフレーム(df)を持っていて、光沢のあるRの最初の行に数字を入れています。データの各可変要素のインデックスを見たいと思いますフレームをドロップダウンリストに表示します。言い換えれば、インデックスを使用して列名ではなく要素を選択したいと考えています。私はこれが奇妙に聞こえるかもしれないことを知っているが、私は本当にこれで助けが必要です。私のサンプルコードは以下の通りです:ドロップダウンリストからデータを選択する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) 
selectInputの値が文字列がそうであることをあなたが代わりにあなたはR.で列インデックスによって光沢差のみをサブセットするのと同じ方法であるカラム名のインデックスを使用することができます
+0

あなたは[再現性の例](HTTPを提供することができます。 com/questions/5963269/how-to-make-a-great-r-reproducible-example)は何ですか? – jsb

答えて

0

as.numeric()を使用する必要があります。

シンプルなワークフロー:私は、プレゼンテーションの目的のために、虹彩データセットを使用1:ncol(data())

  • サブセットdata()[, as.numeric(input$table_no)]
  • を使用してdata.frame

    1. 列数を使用して列インデックスとselectInputを移入します。それはまた、反作用的な値で動作します。

      例:また

      library(shiny) 
      
      ui <- fluidPage(
          selectInput("table_no", "", choices = 1:ncol(iris)), 
          tableOutput("tbl") 
      ) 
      
      server <- function(input, output, session) { 
          output$tbl <- renderTable({ 
          index <- as.numeric(input$table_no) 
          colname <- colnames(iris)[index] 
      
          out <- data.frame(iris[, index]) 
          colnames(out) <- colname 
      
          out 
          }) 
      } 
      
      shinyApp(ui, server) 
      

      サムエルが指摘したように、あなたが再現可能な例を作成する方法をチェックアウトすることを確認してください:// stackoverflowの:How to make a great R reproducible example?

    関連する問題