(SHINY R)我需要从滑块更改列选择以选择具有列名称的框

时间:2016-04-12 10:45:49

标签: r shiny

我有一个小问题。我制作了一个上传文件的程序,有两个标签:

  • 第一个标签(表格)显示文件中的所有列。

  • 第二个标签(表格2)显示通过滑块选择的列

item.Elements

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") ) ) )) 自己做一些事情,但我不知道如何让它读取列标签。

1 个答案:

答案 0 :(得分: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)
相关问题