简体   繁体   English

在R Shiny中保存数据

[英]Save data in R Shiny

how is it possible to save the data the user is inserting, that I only print: 如何保存用户要插入的数据,我只打印:

print(c(input$buttonNext,randomNumber,input_radio))

This should not be just printed, but it should be available to me later. 这不应该只是打印而已,以后应该可以使用。 My plan is, that people evaluate my plots and then I see for each user the buttons he clicked for each plot. 我的计划是,人们评估我的地块,然后为每个用户看到他为每个地块单击的按钮。

ui <- fluidPage(

  actionButton("buttonNext", "Next"),

  radioButtons("radio", "Your Decision:",
               choices = c("No Decision" = 'NoDec', "Yes" = 'yes', "No" = 'no'),
               selected = 'NoDec'),

  plotOutput("TimeSeriesPlot")
)


server <- function(input,output,session) {

  clickNext <- reactive({
    isolate(input_radio <- input$radio)
    randomNumber <- input$buttonNext
    print(c(input$buttonNext,randomNumber,input_radio))
    return(randomNumber)
  })

  observeEvent(input$buttonNext,
               {
                 updateRadioButtons(session,'radio',selected = -1)
               })

  output$TimeSeriesPlot <- renderPlot({ 
    i <- clickNext()
    plot(i)
  })

}
shinyApp(server = server, ui = ui)

The approach is the typical for any similar web app, you can: 对于任何类似的Web应用程序来说,这种方法都是典型的,您可以:

  • Save data as files in a dedicated folder, either in csv format or rds (R compressed format), probably using a combination of user name, time, session etc. in the file name. 将数据以csv格式或rds(R压缩格式)保存为专用文件夹中的文件,可能使用文件名中用户名,时间,会话等的组合。
  • Use some kind of a database connection (anything like SQLite, mysql, PostgreSQL would do. Here you can decide on best design (eg single table for all users, table for each users etc.) depending on application. 使用某种数据库连接(可以执行SQLite,mysql,PostgreSQL之类的任何操作。在这里,您可以根据应用程序决定最佳设计(例如,针对所有用户的单个表,针对每个用户的表等)。

Ultimately the decision will depend on the complexity of the application, the amount of data, concerns about data integrity, portability, environment etc. 最终,决定将取决于应用程序的复杂性,数据量,对数据完整性,可移植性,环境等的关注。

Exploiting some of the many nice features of R, you could sore as per your request, the parameters specified by the user, but you could even store the actual resulting plot. 利用R的许多出色功能,您可以根据您的要求(用户指定的参数)烦恼,但您甚至可以存储实际的结果图。

Ideally this would require a blob field in a db, but in principle you could build a list of lists, where one of the entries are the plot parameters, and the other is the actual plot (then save everything in a single rds file. 理想情况下,这需要在db中有一个blob字段,但是原则上您可以构建一个列表列表,其中一个条目是绘图参数,另一个是实际绘图(然后将所有内容保存在一个rds文件中。

This is a sample implementation. 这是一个示例实现。 You can save data in an RDS file, or use the alternative implementation (commented out here) that allows you to save the data once when you kill the app. 您可以将数据保存在RDS文件中,或使用替代实现(在此处注释),使您在终止应用程序时可以一次保存数据。

library(shiny)

# this is not strictly necessary, but is the way I prefer to define an emply data.frame
resultData <- data.frame(times=character(), inputs=numeric(), random=numeric(),
                     decision=character())

ui <- fluidPage(

  actionButton("buttonNext", "Next"),

  radioButtons("radio", "Your Decision:",
           choices = c("No Decision" = 'NoDec', "Yes" = 'yes', "No" = 'no'),
           selected = 'NoDec'),

  plotOutput("TimeSeriesPlot")
)

server <- function(input,output,session) {

  clickNext <- reactive({
    isolate(input_radio <- input$radio)
    randomNumber <- input$buttonNext
    print(c(input$buttonNext,randomNumber,input$radio))
    return(randomNumber)
  })

  observeEvent(input$buttonNext,
           {
             updateRadioButtons(session,'radio',selected = -1)
           })

  output$TimeSeriesPlot <- renderPlot({

    i <- clickNext()
    a <- plot(i)
    results <-   data.frame(times=Sys.time(),inputs=input$buttonNext,random=clickNext(),
  decision=input$radio)

    # with <<- you  make sure the variable is updated outside of the scope of the function
    resultData <<- rbind(resultData,results)
    saveRDS(resultData, file = "data/resultData.rds")
  })

  # alterantive way to save data, when the app is ended
  # session$onSessionEnded({
  #   saveRDS(resultData, file = "data/resultData.rds")
  #   stopApp
  # })
}
shinyApp(server = server, ui = ui)

# analyse <- readRDS(file = "data/resultData.rds")

Please let me know if you'd like me to expand any of the above. 如果您希望我扩大以上任何一项,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM