单选按钮选择以在下一个输出中显示 table

Radio button selection to show table on next output

在我闪亮的应用程序中,我上传了不同类型的文件,我确实想要处理这些文件并在不同的输出部分显示结果。但是第二个输出取决于第一个输出结果。在我的第二个输出中,我使用列名过滤第一个 table。现在我遇到的问题是我正在上传的一些文件没有我正在使用的列作为第二个输出的子集,它们已被移动到第一行这意味着一旦我上传了一个包含不同列的文件我应该替换现有的列第一行他们过滤第二个输出的结果。这是我的应用程序:

library(shiny)
library(DT)


ui<- shinyUI(fluidPage(
  titlePanel("Uploading Files"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept=c('text/csv',
                         'text/comma-separated-values,text/plain',
                         '.csv')),
      # radio button to show either row or replaced column table
      radioButtons("radio", label = h3("Replace columns"),
                   choices = list("Raw table" = 1, "change columns" = 2), 
                   selected = 1)
    ),
    mainPanel(
      DT::dataTableOutput('contents'),
      DT::dataTableOutput('filtered')
    )
  )
)
)


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

  myData <- reactive({

    inFile <- input$file1
    if (is.null(inFile)) return(NULL)
    data <- read.csv(inFile$datapath, header = TRUE)
    data
  })


  output$contents <- DT::renderDataTable({
    DT::datatable(myData())       
  })

  #Replace columns  reactive event
  replaceColumns <- eventReactive(input$radio,{
    #Change row to column and delete first row
    colnames(myData()) <-myData()[1,]
    df = myData()[-1, ]
    df
  })


  data2<- reactive({
    # Select columns of the dataframe
    df1 <- select(myData(),mpg,cyl,wt)
    df1

  })

  #Output based on either raw or replaced column table
  output$filtered <- DT::renderDataTable({
    DT::datatable(data2())       
  })
}
shinyApp(ui,server)

如何使用单选按钮或是否有其他更好的方法,以便当我上传文件并且看到它有正确的列时,它会自动显示在第一个输出中,当我单击 raw table 在单选按钮中,它继续过滤并提供 过滤输出 如果上传的文件没有正确的列,我单击 更改列 以便用 table 的第一行替换列,然后过滤并显示过滤后的输出?

我希望我的应用程序的行为方式是,当我上传文件时,它会显示,如果文件有正确的列,然后我单击 raw table在单选按钮上,现在继续过滤并在第二个输出中显示过滤后的输出,但如果我看到上传的文件有错误的列,那么我点击单选按钮中的 change columns 将替换列第一行,然后在第二个输出中继续过滤结果。我的意思是。我希望第二个输出取决于我在单选按钮上 select 的内容。

在这两种情况下,第一个 DT 输出显示 table 已加载(名称好坏)。一种方法是静默修复列名:

library(shiny)
library(DT)

ui <- shinyUI(fluidPage(
  titlePanel("Uploading Files"),
  sidebarLayout(
    sidebarPanel(fileInput(
      'file1',
      'Choose CSV File',
      accept = c('text/csv',
                 'text/comma-separated-values,text/plain',
                 '.csv')
    )),
    mainPanel(
      DT::dataTableOutput('contents'),
      DT::dataTableOutput('filtered')
    )
  )
))


server <- function(input, output, session) {
  myData <- reactive({
    inFile <- input$file1
    if (is.null(inFile))
      return(NULL)
    data <- read.csv(inFile$datapath, header = TRUE)
    data
  })

  # Approach 1: fix the data silently
  # Replace column names if needed
  fixData <- reactive({
    req(input$file1)
    df <- myData()

    expectedColumns <- c("mpg", "cyl", "wt")
    if (!all(expectedColumns %in% colnames(myData()))) {
      #Change row to column and delete first row
      colnames(df) <- df[1, ]
      df = df[-1,]
    }  
    df
  })

  output$contents <- DT::renderDataTable({
    DT::datatable(myData())
  })

  data2 <- reactive({
    # Select columns of the dataframe
    df1 <- select(fixData(), mpg, cyl, wt)
    df1
  })

  #Output based on either raw or replaced column table
  output$filtered <- DT::renderDataTable({
    DT::datatable(data2())
  })
}

runApp(list(ui = ui, server = server))

另一种方法是按照您的计划使用单选按钮:

library(shiny)
library(DT)

ui<- shinyUI(fluidPage(
  titlePanel("Uploading Files"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept=c('text/csv',
                         'text/comma-separated-values,text/plain',
                         '.csv')),
      # radio button to show either row or replaced column table
      radioButtons("radio", label = h3("Replace columns"),
                   choices = list("Raw table" = 1, "Change column names" = 2), 
                   selected = 1)
    ),
    mainPanel(
      DT::dataTableOutput('contents'),
      DT::dataTableOutput('filtered')
    )
  )
)
)

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

  myData <- reactive({
    inFile <- input$file1
    if (is.null(inFile)) return(NULL)
    data <- read.csv(inFile$datapath, header = TRUE)
    data
  })

  # Approach 2: click on an radioButton to fix the data

  fixData <- reactive({
    req(input$file1)
    df <- myData()
    if (input$radio == 2) {
      #Change row to column and delete first row
      colnames(df) <- df[1,]
      df = df[-1, ]
    }
    df
  })

  output$contents <- DT::renderDataTable({
    DT::datatable(myData())       
  })

  data2<- reactive({
    # Select columns of the dataframe
    df1 <- select(fixData(),mpg,cyl,wt)
    df1
  })

  #Output based on either raw or replaced column table
  output$filtered <- DT::renderDataTable({
    DT::datatable(data2())       
  })
}
runApp(list(ui=ui,server=server))