【问题标题】:How to render the local datasets in R shiny如何在 R 中渲染本地数据集
【发布时间】:2021-08-08 12:11:03
【问题描述】:

我有以下显示 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)

【问题讨论】:

  • 您可以在 ui 上方加载您的数据,然后在您的服务器中使用它。
  • @pbraeutigm,由于我是 Shiny 的新手,您能帮我编码吗?
  • 是的,我添加了一个示例。希望这能澄清你的问题。

标签: r shiny rscript rshiny


【解决方案1】:

我添加了一个数据集:

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

添加了数据集的用户界面:

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)

【讨论】:

  • 当我使用这段代码时,它运行良好。我在旧代码中做了同样的事情,但是结果按钮不起作用。你能帮我解决问题吗?以下是问题的 URL:stackoverflow.com/q/67606990/15418723
猜你喜欢
  • 2021-01-09
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多