如何用闪亮标记书签上传文件?

时间:2018-12-29 17:07:32

标签: r shiny bookmarks

我已经使用出现在闪亮的应用程序rstudio.com上的示例来上传文件:https://shiny.rstudio.com/articles/upload.html

我已经修改了代码,插入了bookmmarkButton,enableBookmarking和onbookmark以及onRestore函数,但是它对我不起作用。代码有什么问题吗?

library(shiny)

ui <- fluidPage(

    # App title ----
  titlePanel("Uploading Files"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(
      bookmarkButton(),  
      # Input: Select a file ----
      fileInput("file1", "Choose CSV File",
                multiple = TRUE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")),

      # Horizontal line ----
      tags$hr(),

      # Input: Checkbox if file has header ----
      checkboxInput("header", "Header", TRUE),

      # Input: Select separator ----
      radioButtons("sep", "Separator",
                   choices = c(Comma = ",",
                               Semicolon = ";",
                               Tab = "\t"),
                   selected = ","),

      # Input: Select quotes ----
      radioButtons("quote", "Quote",
                   choices = c(None = "",
                               "Double Quote" = '"',
                               "Single Quote" = "'"),
                   selected = '"'),

      # Horizontal line ----
      tags$hr(),

      # Input: Select number of rows to display ----
      radioButtons("disp", "Display",
                   choices = c(Head = "head",
                               All = "all"),
                   selected = "head")

    ),

    # Main panel for displaying outputs ----
    mainPanel(

      # Output: Data file ----
      tableOutput("contents")

    )

  )
)

# Define server logic to read selected file ----
server <- function(input, output) {




  output$contents <- renderTable({

    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, head of that data file by default,
    # or all rows if selected, will be shown.

    req(input$file1)

    df <<- read.csv(input$file1$datapath,
                   header = input$header,
                   sep = input$sep,
                   quote = input$quote)

    if(input$disp == "head") {
      return(head(df))
    }
    else {
      return(df)
    }


  })

  onBookmark(function(state){
    state$values$data <- df

  })

  onRestore(function(state){

    df <- state$values$data

  })

}

enableBookmarking(store="server")
shinyApp(ui, server)

1 个答案:

答案 0 :(得分:1)

enableBookmarking documentation指定了需要执行的操作:

  

要使状态恢复正常工作,UI必须是一个能够   接受一个论点,请求。在大多数Shiny应用程序中,UI并非   功能;它的格式可能为fluidPage(....)。将其转换为   函数就像将其包装在函数中一样简单,例如   function(request) { fluidPage(....) }

它现在似乎可以正常工作,它的功能包装了fluidPage代码中的ui

library(shiny)

ui <- function(request){
  fluidPage(

  # App title ----
  titlePanel("Uploading Files"),

  # Sidebar layout with input and output definitions ----


    sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(
      bookmarkButton(),  
      # Input: Select a file ----
      fileInput("file1", "Choose CSV File",
                multiple = TRUE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")),

      # Horizontal line ----
      tags$hr(),

      # Input: Checkbox if file has header ----
      checkboxInput("header", "Header", TRUE),

      # Input: Select separator ----
      radioButtons("sep", "Separator",
                   choices = c(Comma = ",",
                               Semicolon = ";",
                               Tab = "\t"),
                   selected = ","),

      # Input: Select quotes ----
      radioButtons("quote", "Quote",
                   choices = c(None = "",
                               "Double Quote" = '"',
                               "Single Quote" = "'"),
                   selected = '"'),

      # Horizontal line ----
      tags$hr(),

      # Input: Select number of rows to display ----
      radioButtons("disp", "Display",
                   choices = c(Head = "head",
                               All = "all"),
                   selected = "head")

    ),

    # Main panel for displaying outputs ----
    mainPanel(

      # Output: Data file ----
      tableOutput("contents")

    )

  )
)
}
# Define server logic to read selected file ----
server <- function(input, output) {

  output$contents <- renderTable({

    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, head of that data file by default,
    # or all rows if selected, will be shown.

    req(input$file1)
    df <<- read.csv(input$file1$datapath,
                    header = input$header,
                    sep = input$sep,
                    quote = input$quote)

    if(input$disp == "head") {
      return(head(df))
    }
    else {
      return(df)
    }
  })

  onBookmark(function(state){
    state$values$data <- df

  })

  onRestore(function(state){
    df <- state$values$data
  })

}

enableBookmarking(store="server")
shinyApp(ui, server)