如何在 R shiny 中渲染本地数据集

How to render the local datasets in R shiny

我有以下显示 CSV 结果的 R Shiny 代码。目前,它仅适用于 R 的默认数据集。我想在我的本地机器上以相同的方式显示数据集的 CSV 结果。

CSV 示例:Study1.csv、Study2.csv、Study3.csv

有人可以解释一下如何在 R shiny 中完成这个吗?

ui <- fluidPage(
  
  # App title ----
  titlePanel("Downloading Data"),
  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
      
      # Input: Choose dataset ----
      selectInput("dataset", "Choose a dataset:",
                  choices = c("rock", "pressure", "cars")),
      
      # Button
      downloadButton("downloadData", "Download")
      
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      tableOutput("table")
      
    )
    
  )
)

server <- function(input, output) {
  
  # Reactive value for selected dataset ----
  datasetInput <- reactive({
    switch(input$dataset,
           "rock" = rock,
           "pressure" = pressure,
           "cars" = cars)
  })
  
  # Table of selected dataset ----
  output$table <- renderTable({
    datasetInput()
  })
  
  # Downloadable csv of selected dataset ----
  output$downloadData <- downloadHandler(
    filename = function() {
      paste(input$dataset, ".csv", sep = "")
    },
    content = function(file) {
      write.csv(datasetInput(), file, row.names = FALSE)
    }
  )
  
}

shinyApp(ui, server)

我添加了一个数据集:

dataset1 <- structure(list(This = c(1L, 7L, 3L), is_the = c(5L, 8L, 2L), 
    header = c(9L, 5L, 4L)), class = "data.frame", row.names = c(NA, 
-3L))

您还可以通过

读取csv并将其用作数据集1
dataset1 <- read.csv("your path",sep="your seperator")

您的 UI 添加了数据集:

dataset1 <- structure(list(This = c(1L, 7L, 3L), is_the = c(5L, 8L, 2L), 
        header = c(9L, 5L, 4L)), class = "data.frame", row.names = c(NA, 
    -3L))
ui <- fluidPage(
              
              # App title ----
              titlePanel("Downloading Data"),
              
              # Sidebar layout with input and output definitions ----
              sidebarLayout(
                        
                        # Sidebar panel for inputs ----
                        sidebarPanel(
                                  
                                  # Input: Choose dataset ----
                                  selectInput("dataset", "Choose a dataset:",
                                              choices = c("rock", "pressure", "cars", "dataset1")),
                                  
                                  # Button
                                  downloadButton("downloadData", "Download")
                                  
                        ),
                        
                        # Main panel for displaying outputs ----
                        mainPanel(
                                  
                                  tableOutput("table")
                                  
                        )
                        
              )
    )

你的服务器添加了dataset1

server <- function(input, output) {
          
          # Reactive value for selected dataset ----
          datasetInput <- reactive({
                    switch(input$dataset,
                           "rock" = rock,
                           "pressure" = pressure,
                           "cars" = cars,
                           "dataset1" = dataset1)
          })
          
          # Table of selected dataset ----
          output$table <- renderTable({
                    datasetInput()
          })
          
          # Downloadable csv of selected dataset ----
          output$downloadData <- downloadHandler(
                    filename = function() {
                              paste(input$dataset, ".csv", sep = "")
                    },
                    content = function(file) {
                              write.csv(datasetInput(), file, row.names = FALSE)
                    }
          )
          
}

shinyApp(ui, server)