简体   繁体   English

在 R 闪亮仪表板中输入 2 个表

[英]Input 2 tables in R shiny dashboard

我想有一个规定输入 2 个表,即训练和测试,以便在 R 闪亮仪表板中进行进一步分析。

Here you go.给你。 Assuming your files are in a format that you can read with read.table(), this example will be a working start for you.假设你的文件是你可以用 read.table() 读取的格式,这个例子对你来说将是一个工作的开始。

You can use fileInput() to input files, this can be any format from .csv, .txt or .RData or any other file type you need.您可以使用fileInput()输入文件,这可以是 .csv、.txt 或 .RData 中的任何格式或您需要的任何其他文件类型。

You use fileInput("Table1",...) in the UI.您在 UI 中使用fileInput("Table1",...) And then in the server() function of your app you can access the input files by input$Table1 .然后在您的应用程序的 server() 函数中,您可以通过input$Table1访问输入文件。 Specifically, the file is uploaded and saved at a temporary location that you can access with input$Table1$datapath .具体来说,文件被上传并保存在一个临时位置,您可以使用input$Table1$datapath访问该位置。

With this file location you should be able to do whatever you want to do with the data that is now uploaded to your shiny App.有了这个文件位置,你应该能够对现在上传到闪亮应用程序的数据做任何你想做的事情。 In the example below I only read the data using read.table() and then show the data in two datatables in the UI.在下面的示例中,我仅使用read.table()读取数据,然后在 UI 的两个数据表中显示数据。

## app.R ##
library(shiny)
library(shinydashboard)

# A minimal dashboard page
#   Sidebar for file inputs
#   Main body to show 2 datatables
ui <- dashboardPage(
  dashboardHeader(title = "Two tables for further analysis"),
  dashboardSidebar(
    h2("fileInput() to upload files"),
    fileInput("Table1", "Input file for train data"),
    fileInput("Table2", "Input file for test data")
  ),
  # A body for two datatables to show the data input
  dashboardBody(
    fluidPage(
      fluidRow(
        column(width=6,
               DT::dataTableOutput("Train")),
        column(width=6,
               DT::dataTableOutput("Test"))
  )
)
))

server <- function(input, output) {
  output$Train <- DT::renderDataTable({
    # NULL if no input given
    if (is.null(input$Table1)) return(NULL)

    # Else... read table (input$Table1 is a list of componenent - the 'datapath' field is the temp file location)
    read.table(input$Table1$datapath) # Assuming you have a file to read with read.table()
  })

  # Using output$Test - we can use datatableOutput("Test") to show the datatable in the UI
  output$Test <- DT::renderDataTable({
    # NULL if no input given
    # 
    if (is.null(input$Table2)) return(NULL)
    # Else... read table (input$Table1 is a list of componenent - the 'datapath' field is the temp file location)
    read.table(input$Table2$datapath) # Assuming you have a file to read with read.table()
  })

  # You can of course read the data for your own purpose
  #   Not for a datatable, but to do follow-up analysis
  #   
  #   Further, you can also upload .RData files for example - or any other file.
}

# Run the shiny app
shinyApp(ui, server)

Please be a little more specific next time and give an example of what you have already tried.下次请更具体一点,并举例说明您已经尝试过的内容。 I have no clue what type of data format you are trying to work with... Hope this helps!我不知道您要使用什么类型的数据格式...希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM