选择单选按钮以在下一个输出上显示表格

时间:2019-09-19 09:05:07

标签: r shiny shinydashboard shiny-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 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)

如何使用单选按钮,或者还有其他更好的方法,以便当我上载文件时看到它具有正确的列时,它会自动显示在第一个输出中,并且单击原始表在单选按钮中,它会继续进行过滤并提供过滤后的输出;如果上载的文件没有正确的列,请点击更改列,以便将列替换为第一行表格然后过滤并显示过滤后的输出?

我希望我的应用程序运行的方式是,当我上传文件时,它会显示,并且如果文件具有正确的列,那么我单击单选按钮上的原始表,该单选按钮现在可以进行过滤和在第二个输出中显示过滤后的输出,但是如果我看到上传的文件的列有错误,那么我单击单选按钮中的c hange列,它将用第一行替换列,然后在第二个中进行过滤结果输出。我的意思是。我希望第二个输出取决于我在单选按钮上选择的内容。

1 个答案:

答案 0 :(得分:0)

在两种情况下,第一个DT输出都将表显示为已加载(名称正确或错误)。一种方法是静默修复列名:

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))