2017-09-15 5 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.frame I는 프레젠테이션 용도 홍채 데이터 세트를 사용
  • data()[, as.numeric(input$table_no)] 사용 :

    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) 
      

      는 또한, 당신이 재현 예를 만드는 방법을 체크 아웃하십시오 : How to make a great R reproducible example?