2016-04-12 88 views
0

我有一個小問題。我製作了一個上傳文件的程序,並有兩個選項卡:(SHINY R)我需要改變列選擇從滑塊到列名選擇框

  • 第一個選項卡(表格)顯示文件中的所有列。

  • 第二個標籤(表2)示出了經由滑塊

server.R所選列:

shinyServer(function(input,output){ 
    data <- reactive({ 
     file1 <- input$file 
     if(is.null(file1)){return()} 
     read.table(file=file1$datapath, sep=input$sep, header = input$header) 
    }) 

    output$table <- renderTable({ 
     if(is.null(data())){return()} 
     data()  
    }) 

    output$table2 <- renderTable({ 
     if(is.null(data())){return()} 
     data()[c(input$slider1)]  
    }) 

    output$tb <- renderUI({ 
     if(is.null(data())) 
      h5("Wgraj Plik jeśli chcesz cokolwiek zrobić") 
     else 
      tabsetPanel(tabPanel("dane", tableOutput("table")),tabPanel("wybrana kolumna", tableOutput("table2"))) 
    }) 
}) 

ui.R

# ui.R 
shinyUI(fluidPage(
    titlePanel("Aplikacja testowa numer 5 Praca z plikiem"), 

    sidebarLayout(
     sidebarPanel(
      fileInput("file", label = h3("Wgraj Plik")), 
      checkboxInput(inputId = 'header', label = 'Pierwszy wers to etykiety', value = FALSE), 
      radioButtons(inputId = 'sep', label = 'Co jest separatorem', choices = c(Przecinek=',',Średnik=';',Tabulator='\t', Spacja=''), selected = ','), 
      sliderInput("slider1", label = h3("Slider"), min = 1, max = 20, value = 1) 
     ), 
     mainPanel(
      uiOutput("tb") 
     ) 
    ) 
)) 

我需要改變所述滑塊成一個輸入框從上傳文件的列標籤中讀取選項。

我試圖自己通過renderUI做一些事情,但我不知道如何讓它讀取列的標籤。

回答

0

這會適合你嗎?

首先渲染一個空的checkboxGroupInput,並在數據提交後立即更新其選擇。

代碼:

library(shiny) 

ui <- shinyUI(fluidPage(
    titlePanel("Aplikacja testowa numer 5 Praca z plikiem"), 

    sidebarLayout(
    sidebarPanel(
     fileInput("file", label = h3("Wgraj Plik")), 
     checkboxInput(inputId = 'header', label = 'Pierwszy wers to etykiety', value = FALSE), 
     radioButtons(inputId = 'sep', label = 'Co jest separatorem', choices = c("Przecinek"=',',"Średnik"=';',"Tabulator"='\t', "Spacja"=''), selected = ','), 

     checkboxGroupInput("choices1", label = h3("Column names"), choices = NULL) 
    ), 

    mainPanel(
     uiOutput("tb") 
    ) 
) 
)) 

server <- function(input, output, session){ 

    data <- reactive({ 
    file1 <- input$file 
    if(is.null(file1)){return()} 
    dataSet <- read.table(file=file1$datapath, sep=input$sep, header = input$header) 
    # Change made here: use the dataSet to modify selection choices. 
    updateCheckboxGroupInput(session, "choices1", choices = colnames(dataSet)) 

    dataSet 
    }) 

    output$table <- renderTable({ 
    if(is.null(data())){return()} 
    data()  
    }) 

    output$table2 <- renderTable({ 

    # Changes made here: 1) If no box is ticked, don't try to render. 
    # 2) Take list of colnames to select columns. (Multiple work.) 
    if(is.null(data()) || is.null(input$choices1)){return()} 
    data()[input$choices1]  
    }) 

    output$tb <- renderUI({ 
    if(is.null(data())) 
     h5("Wgraj Plik jeśli chcesz cokolwiek zrobić") 
    else 
     tabsetPanel(tabPanel("dane", tableOutput("table")),tabPanel("wybrana kolumna", tableOutput("table2"))) 
    }) 
} 

shinyApp(ui, server) 
+0

非常感謝你。 –