Write the inputs to csv file or excel files

Is there a way to write the inputs generated to a csv/excel file. For example the inputs selected are rendered by show_inputs . But I also need to write this to a file. The main goal is to understand what all inputs are selected by users. The values should go on appending to the csv/excel file so that at the end of the end, i can see what all inputs are there. Can we achieve this?

library(shiny)

ui <- basicPage(
  
  fluidRow(
    column(
      width = 6,
      textInput('a', 'Text A',"a1"),
      textInput('b', 'Text B',"b1"),
      textInput('c', 'Text A',"c1"),
      textInput('d', 'Text B',"d1"),
      textInput('e', 'Text A',"e1"),
      textInput('f', 'Text B',"f1"),
      uiOutput('f1')
    ),
    column(
      width = 6,
      tags$p(tags$span(id = "valueA", "")),
      tags$script(
        "$(document).on('shiny:inputchanged', function(event) {
          if (event.name === 'a') {
            $('#valueA').text(event.value);
          }
        });
        "
      )
      ,tableOutput('show_inputs')
    )
  )
)

server <- shinyServer(function(input, output, session){
  
  output$f1 <- renderUI({
    if(input$a == "a2"){
      textInput('z', 'Text B',"z1")
    } else {
      NULL
    }
  })
  
  AllInputs <- reactive({
    x <- reactiveValuesToList(input)
  })
  
  
  
  output$show_inputs <- renderTable({
    AllInputs()
  })
})
shinyApp(ui = ui, server = server)

Modifying ouput$show_inputs to add a call to write_lines() will append the chosen inputs to a file.

 output$show_inputs <- renderTable({
    AllInputs()
    write_lines(paste(AllInputs(),collapse=","),file = "ShinyInputs.csv", append = TRUE)
  })

Thanks. But I got this error

Warning: Error in writeLines: unused arguments (file = "ShinyInputs.csv", append = TRUE)

Actually it is write.table . :slight_smile:

Looks good. But when I run, i get the below output

"x"
"1" "a1,e1,b1,c1,d1,f1"
"x"
"1" "a1,e1,er,c1,d1,f1"
"x"
"1" "a1,e1,er,er,d1,f1"
"x"
"1" "a1,e1,b1,c1,d1,f1"
"x"
"1" "a1,e1,b1,c1,d1,z"

But this is little confusing since the we cannot identify what the titles for this. Can we add titles to this. For example Text A, Text B and so on . So that we can see which has what input

Here is the whole app.R file. Notice I used the function write_lines().

library(shiny)

ui <- basicPage(
  
  fluidRow(
    column(
      width = 6,
      textInput('a', 'Text A',"a1"),
      textInput('b', 'Text B',"b1"),
      textInput('c', 'Text A',"c1"),
      textInput('d', 'Text B',"d1"),
      textInput('e', 'Text A',"e1"),
      textInput('f', 'Text B',"f1"),
      uiOutput('f1')
    ),
    column(
      width = 6,
      tags$p(tags$span(id = "valueA", "")),
      tags$script(
        "$(document).on('shiny:inputchanged', function(event) {
          if (event.name === 'a') {
            $('#valueA').text(event.value);
          }
        });
        "
      )
      ,tableOutput('show_inputs')
    )
  )
)

server <- shinyServer(function(input, output, session){
  
  output$f1 <- renderUI({
    if(input$a == "a2"){
      textInput('z', 'Text B',"z1")
    } else {
      NULL
    }
  })
  
  AllInputs <- reactive({
    x <- reactiveValuesToList(input)
  })
  
  
  
  output$show_inputs <- renderTable({
    AllInputs()
    write_lines(paste(AllInputs(),collapse=","),file = "ShinyInputs.csv",append = TRUE)
  })
})
shinyApp(ui = ui, server = server)

The following will write a set of column headers with every append.

 output$show_inputs <- renderTable({
    Text <- c(paste(c("T1","T2","T3","T4","T5","T6"),collapse=","),
              paste(AllInputs(),collapse=","))
    write_lines(Text,file = "ShinyInputs.csv",append = TRUE)
    AllInputs()
  })

If you want the header written only once, you could check for the existence of the file and only write the header if you need to make a new file.

Oh I see. Not sure why I get this error (different error as well)

library(shiny)
library(brio)

ui <- basicPage(
  
  fluidRow(
    column(
      width = 6,
      textInput('a', 'Text A',"a1"),
      textInput('b', 'Text B',"b1"),
      textInput('c', 'Text A',"c1"),
      textInput('d', 'Text B',"d1"),
      textInput('e', 'Text A',"e1"),
      textInput('f', 'Text B',"f1"),
      uiOutput('f1')
    ),
    column(
      width = 6,
      tags$p(tags$span(id = "valueA", "")),
      tags$script(
        "$(document).on('shiny:inputchanged', function(event) {
          if (event.name === 'a') {
            $('#valueA').text(event.value);
          }
        });
        "
      )
      ,tableOutput('show_inputs')
    )
  )
)

server <- shinyServer(function(input, output, session){
  
  output$f1 <- renderUI({
    if(input$a == "a2"){
      textInput('z', 'Text B',"z1")
    } else {
      NULL
    }
  })
  
  AllInputs <- reactive({
    x <- reactiveValuesToList(input)
  })
  
  
  
  output$show_inputs <- renderTable({
    AllInputs()
    write_lines(paste(AllInputs(),collapse=","),file = "ShinyInputs.csv",append = TRUE)
  })
})
shinyApp(ui = ui, server = server)
Warning: Error in write_lines: unused arguments (file = "ShinyInputs.csv", append = TRUE)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.