R闪亮的数据输出的持久存储

时间:2016-10-18 11:19:20

标签: r shiny multiple-files persistent-storage

我正在开发一个应用程序(这是一个精简版本),用户可以上传.csv文件并执行一些自动计算(在这种情况下只是一个简单的线性模型来获得{{1价值)。由于将会有大量r.squared个电子表格(每个都可以有一个唯一的名称),我想知道是否可以将所有.csv值存储在一个摘要中table(第二个选项卡),一旦所有文件都通过,就可以导出为单个r.squared文件(我很熟悉操作按钮来完成最后一部分。

我有两个工作流程选项:

  1. 多个文件(或一次可以加载文件夹)
  2. 每个文件都单独加载
  3. ui.R

    .csv

    server.R

    library(shiny) 
    ui <- fluidPage(
         navbarPage("Menu",inverse=TRUE,fluid=TRUE,collapsible=TRUE,selected = "Data  input", 
             tabPanel("Data input",
                      sidebarLayout(
               sidebarPanel(
                 fileInput('file1', 'Choose CSV File', 
                           accept=c('text/csv', 
                                    'text/comma-separated-values,text/plain', 
                                    '.csv')),
                 tags$hr(),
                 checkboxInput('header', 'Header', TRUE),
                 radioButtons('sep', 'Separator',
                              c(Comma=',',
                                Semicolon=';',
                                Tab='\t'),
                              ','),
                 radioButtons('quote', 'Quote',
                              c(None='',
                                'Double Quote'='"',
                                'Single Quote'="'"),
                              '"')
               ),
               mainPanel(
                 tableOutput('contents') 
               )
             )),
    
    
             tabPanel("Summary",fluidRow(
               column(6,
                      tableOutput("summary")))) 
    
             ))
    

1 个答案:

答案 0 :(得分:0)

I would use reactiveValues for this stuff. It works like a list, but is reactive, so you can change it throughout the session. Here is an example where I use it to store R squared values which are calculated throughout the session.

It is basically the code you posted above. In the server, I included a reactiveValues object storage and a observeEvent which just appends storage$Rsquared by the R squared whenever mdata() is reevaluated.

This is just a basic example. By adding conditions in the observeEvent you can make it more sophisticated. Or maybe you want to collect more than just a numeric, then you would put another kind of data structure in the reactiveValue.

ui

ui <- fluidPage(
  navbarPage("Menu",inverse=TRUE,fluid=TRUE,collapsible=TRUE,selected = "Data  input",
    tabPanel("Data input",
      sidebarLayout(
        sidebarPanel(
          fileInput('file1', 'Choose CSV File', accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv')),
          tags$hr(),
          checkboxInput('header', 'Header', TRUE),
          radioButtons('sep', 'Separator', c(Comma=',', Semicolon=';', Tab='\t'),','),
          radioButtons('quote', 'Quote', c(None='', 'Double Quote'='"', 'Single Quote'="'"),'"')
        ),
        mainPanel(
          tableOutput('contents')
        )
      )
    ),
    tabPanel( "Summary", fluidRow(column(6, tableOutput("summary"))))
))

server

server <- function(input, output)
{
  storage <- reactiveValues(Rsquared = numeric())

  mydata <- reactive({
    inFile <- input$file1
    if(is.null(inFile)) return(NULL)
    data<-read.csv(inFile$datapath, header = input$header, sep = input$sep, quote = input$quote)
    data
  })

  output$contents <- renderTable({
    mydata()
  })

  observeEvent(mdata(),{
    m <- lm(mydata()[,1]~mydata()[,2])
    storage$Rsquared <- append(storage$Rsquared, summary(m)$r.squared)
  })

  output$summary <- renderTable(storage$Rsquared)
}

Then, you could add a download functionality for the user to save all the R squared values that he created. The download function would use storage$Rsquared and write it into a .csv for example.