使用应用程序时保存对闪亮数据表所做的更改

时间:2019-06-04 06:18:42

标签: r shiny

我正在构建一个闪亮的App,其中有一个数据表,其中包含各种选择输入和复选框输入,用户可以使用选择输入和复选框在单元格中选择值,有没有办法我可以存储此数据在用户做出所有选择之后。

我有一个数据框,在其中添加了3个新列,每个单元格都有selectinputs和复选框。用户可以使用它们选择值。

shinyServer(function(input, output, session) {

observeEvent(input$datafile_1,{

mapping_file <<- read.csv(input$datafile_1$datapath,stringsAsFactors = FALSE)

output$mytable1 <- renderDataTable({data.table(mapping_file)},
    options = list(
      dom='t',
      ordering=F,
      lengthChange = TRUE,
      pageLength=20,
      rownames= FALSE,
      initComplete = JS(
        "function(settings, json) {",
        "$(this.api().table().header()).css({'background-color': '#00338d', 'color': '#fff'});",
        "}"),
      autowidth = TRUE
),rownames= FALSE)

DF <- data.frame(Fields = colnames(df_master_file),stringsAsFactors = FALSE)

# helper function for making checkbox
shinyInput = function(FUN, len, id, ...) { 
  inputs = character(len) 
  for (i in seq_len(len)) { 
    inputs[i] = as.character(FUN(paste0(id, i), label = NULL, ...)) 
  } 
  inputs 
}

output$mytable2 = DT::renderDataTable({
  data.frame(DF,Mapping=shinyInput(selectInput,nrow(DF),"mapping_",
                                   choices=c("",colnames(mapping_file)),width="200px"),
                Filter=shinyInput(checkboxInput,nrow(DF),"filter_",
                                  width="50px"),
                Color=shinyInput(checkboxInput,nrow(DF),"color_",
                               width="50px")
             )
}, selection='none',server = FALSE, escape = FALSE, options = list( 
  dom = 't',
  paging=TRUE,
  preDrawCallback = JS('function() { 
  Shiny.unbindAll(this.api().table().node()); }'), 
  drawCallback = JS('function() { 
  Shiny.bindAll(this.api().table().node()); } '),
  ordering=F,
  lengthChange = TRUE,
  pageLength=37,
  rownames= FALSE,
  initComplete = JS(
    "function(settings, json) {",
    "$(this.api().table().header()).css({'background-color': '#00338d', 'color': '#fff'});",
    "}")
),rownames= FALSE )

})

我要在用户在数据表上做出选择后保存数据,以便我可以进一步将其用于分析。

1 个答案:

答案 0 :(得分:0)

我认为这是您想要的东西,但我不确定100%。

bookmarkButton()
shinyApp(ui = ui, server = server, enableBookmarking = "url")

这两个功能允许您存储应用程序的“状态”,从而可以与其他用户共享选择内容以供以后分析。

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

   # Application title
   titlePanel("Old Faithful Geyser Data"),

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30),
         bookmarkButton()
      ),

      # Show a plot of the generated distribution
      mainPanel(
         plotOutput("distPlot")
      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {

   output$distPlot <- renderPlot({
      # generate bins based on input$bins from ui.R
      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)

      # draw the histogram with the specified number of bins
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
   })
}

# Run the application 
shinyApp(ui = ui, server = server, enableBookmarking = "url")