首页 > 解决方案 > How to manipulate tables / dataframe from input in shiny?

问题描述

I'm quite new in shiny, and I'm struggling with reactive objects and dataframe in shiny.

I'd like to make an app which allows the user to load a file (like .csv , .rds etc) which will be the "base" of all the process after. The idea is that once the file is uploaded, some panels will display subsets of the table uploaded or new tables made of calculations based on the uploaded table.

Actually the difficulty is about the synthax, especialy how to select rows and columns from the table. I'm searching for an equivalent of df[c(1,2),] or df[,c(1,2)] or df$variable_name in shiny.

Here is my code, I just want to display the 1st and 2nd column of the input file in order to see if the processing that I've done is ok :

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

Does anyone can explain me how to manipulate uploaded tables through shiny ?

Thanks in advance

标签: rshiny

解决方案


@denis:这是一个带有 .csv csv 文件的可复制示例

我想知道的是如何操作和子集输入 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)
#}

推荐阅读