【问题标题】:How to manipulate tables / dataframe from input in shiny?如何从闪亮的输入中操作表/数据框?
【发布时间】:2019-10-20 19:11:33
【问题描述】:

我在 Shiny 方面很新,我正在努力处理 Shiny 中的反应性对象和数据框。

我想制作一个允许用户加载文件(如 .csv 、 .rds 等)的应用程序,该文件将成为之后所有流程的“基础”。这个想法是,一旦文件上传,一些面板将显示上传的表格的子集或基于上传的表格计算的新表格。

实际上困难在于合成器,尤其是如何从表中选择行和列。我正在寻找 df[c(1,2),] 或 df[,c(1,2)] 或 df$variable_name 的等价物。

这是我的代码,我只想显示输入文件的第 1 列和第 2 列,以查看我所做的处理是否正常:

## Only run examples in interactive R sessions

library(sas7bdat)


ui <-fluidPage(navlistPanel(
  tabPanel("Welcome",textOutput("welcome_message"),textOutput("welcome_message_2"),img(src="logo_danone.jpg", height = 350, width = 350)),

  tabPanel("Input files", fileInput("file1", "Choose File",
                                    accept = c(
                                      "text/csv",
                                      "text/comma-separated-values,text/plain",
                                      ".csv")),
           inputPanel(
             tableOutput("contents")

           )),

  tabPanel("AUC Derivation",tableOutput("selection")),

  tabPanel("AUC Derivation plots"),

  tabPanel("Shape Analysis"),

  tabPanel("Tables"),

  tabPanel("Plots"),

  tabPanel("Clustering"),

  tabPanel("tab1",dataTableOutput("value")),

  tabPanel("tab2",plotOutput("hist"))






))

server <-function(input, output) {

  # You can access the value of the widget with input$file, e.g.

  output$welcome_message <- renderText("test")
  output$welcome_message_2 <- renderText("logo")

  output$value <- renderDataTable({
    iris
  })

  output$contents <- renderTable({
    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, it will be a data frame with 'name',
    # 'size', 'type', and 'datapath' columns. The 'datapath'
    # column will contain the local filenames where the data can
    # be found.
    inFile <- input$file1

    if (is.null(inFile))
      return(NULL)

    readRDS(inFile$datapath)
  })

  output$selection <- reactive({return(input$file1[,c(1,2)])})

  #output$selected <- renderTable({selection()})

  output$hist <- renderPlot({     # Refers  to putputs with output$<id>
    ggplot2::ggplot(data = iris, aes(y = Sepal.Length)) + geom_boxplot() # Refers to inputs with input$<id>
  })

}

  shinyApp(ui, server)
#}

谁能解释我如何通过 shiny 操作上传的表格?

提前致谢

【问题讨论】:

  • 欢迎来到 SO。尝试给出一个可重现的示例 (stackoverflow.com/help/minimal-reproducible-example),例如通过提供一段代码来创建一个小的 csv 文件,您希望将其上传到闪亮的应用程序中。这样,任何人都可以重现您给出的示例
  • @denis 我已经用 .csv 文件发布了下面的可复制示例

标签: r shiny


【解决方案1】:

@denis:这是一个可复制的 .csv 示例 csv file

我想知道的是如何操作和子集输入 csv(这是一个数据框),因为我必须通过已经编写的函数过滤、合并和使用列,并在不同的面板中显示结果

 ## Only run examples in interactive R sessions

library(sas7bdat)


ui <-fluidPage(navlistPanel(
  tabPanel("Welcome",textOutput("welcome_message"),textOutput("welcome_message_2"),img(src="logo_danone.jpg", height = 350, width = 350)),

  tabPanel("Input files", fileInput("file1", "Choose File",
                                    accept = c(
                                      "text/csv",
                                      "text/comma-separated-values,text/plain",
                                      ".csv")),
           inputPanel(
             tableOutput("contents")

           )),

  tabPanel("AUC Derivation",tableOutput("selection")),

  tabPanel("AUC Derivation plots"),

  tabPanel("Shape Analysis"),

  tabPanel("Tables"),

  tabPanel("Plots"),

  tabPanel("Clustering"),

  tabPanel("tab1",dataTableOutput("value")),

  tabPanel("tab2",plotOutput("hist"))






))

server <-function(input, output) {

  # You can access the value of the widget with input$file, e.g.

  output$welcome_message <- renderText("test")
  output$welcome_message_2 <- renderText("logo")

  output$value <- renderDataTable({
    iris
  })

  output$contents <- renderTable({
    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, it will be a data frame with 'name',
    # 'size', 'type', and 'datapath' columns. The 'datapath'
    # column will contain the local filenames where the data can
    # be found.
    inFile <- input$file1

    if (is.null(inFile))
      return(NULL)

    read.csv(inFile$datapath, sep = ";")
  })

  output$selection <- reactive({return(input$file1[,c(1,2)])})

  #output$selected <- renderTable({selection()})

  test <- reactive({input$file1()})

  output$hist <- renderPlot({     # Refers  to putputs with output$<id>
    ggplot2::ggplot(data = iris, aes(y = Sepal.Length)) + geom_boxplot() # Refers to inputs with input$<id>
  })

}

  shinyApp(ui, server)
#}

【讨论】:

    猜你喜欢
    • 2021-08-10
    • 2020-04-22
    • 1970-01-01
    • 2014-04-11
    • 2021-02-09
    • 2019-08-19
    • 1970-01-01
    • 2018-01-03
    • 2017-12-19
    相关资源
    最近更新 更多