0

I have a question regarding R shiny and the observ function. Is it possible to save the selected factors and the state of the work? For Example I created a programm which can choose colnames from the input data. After using bookmark and reopening the programm with the link in the browser the input data are loaded but the select factors of the colnames are reset. But I want to save the chosen colnames. Has anyone an idea? Thank you for your help!

    ui <- function(request) {
  fluidPage(
    sidebarLayout(
      sidebarPanel(
        radioButtons(
          "fileType_Input",
          label = h5(""),
          choices = list(".csv" = 1, ".xlsx" = 2),
          selected = 1,
          inline = TRUE
        ),

        fileInput('file1', ''  ),
        selectInput("letters", label=NULL, factors, multiple = TRUE),
        bookmarkButton()
      ),
      mainPanel(
        tableOutput("contents")
      )
    )
  )
}
server <- function(input, output,session) {

  myData <- reactive({
    inFile <- input$file1

    # Get the upload file
    if (is.null(inFile)) {
      return(NULL) }

    if (input$fileType_Input == "1") {
      read.csv2(inFile$datapath,
                header = TRUE,
                stringsAsFactors = FALSE)

    } else {
      read_excel(inFile$datapath)
    }

  })


  observe({
    if(is.null(input$letters)){
      data <- myData()
      if(is.null(data)){

      }else{
        factors <- colnames(data)
        t$choices <- input$letters  # append(u$choices,input$letters2)
        updateSelectInput(session, "letters",
                          choices = factors #[!factors2 %in% u$choices)]
        )}
    }
  })

  #Display all input Data
  output$contents <- renderTable(digits = NULL,{
    df <-myData()
    df
  })

}
enableBookmarking("server")
shinyApp(ui, server)
Zorro
  • 91
  • 1
  • 8
  • Your example is not really accurate. See [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) link if you want more answers. – vladli Apr 10 '18 at 14:42

1 Answers1

1

You can save all needed inputs in a file, and then reapply them with functions like updateRadioButtons() and others.

Saving it to the file could look like this:

observeEvent(input$someRadioButton, {
  states <- list()
  states$someRadioButton <- input$someRadioButton 
  #you can save all the needed inputs like this
  ...
  save(states, file = paste0(getwd(), "/myfile"))
})
vladli
  • 1,454
  • 2
  • 16
  • 40
  • Thank you for your help. I found a page: https://stackoverflow.com/questions/36230972/r-shiny-save-to-server?answertab=active#tab-top but I didnt understand the Note that /home/user is a folder that your shiny app has write permission to. My Problem ist that now the App says: Warning in readChar(con, 5L, useBytes = TRUE) : cannot open compressed file '/home/user', probable reason 'No such file or directory' or Warning in readChar(con, 5L, useBytes = TRUE) : cannot open compressed file '/home/user', probable reason 'Permission denied' Could you help me here too? – Zorro Apr 11 '18 at 11:30
  • Ask a new question for a new problem. Pathing is probably wrong. Error says you're trying to open `/home/user`, and there is no such path at all – vladli Apr 12 '18 at 10:15