How to save multiple user Input values in csv file in R shiny?

I have an application containing many user Inputs. I want to save them in a csv file.

ui<- fluidPage(titlePanel("Application"),
               fluidRow(column( width = 8,
                                          textInput("message", label = "",placeholder = "Type your message here."),
                                          actionButton("send", "Send"), heading = "Smart Advisor", status = "primary")
               ))

server<- function(input, output, session)
{
  clearInput<- function()
  {
    updateTextInput(session,"message", value = "")
  }
  
  observeEvent(input$send,{
    #Case 1
    insertUI(
      selector = "#message",
      where = "beforeBegin",
      ui=div(class="registration",
               div(class="bubble",
                 wellPanel(
                   p("Please enter your Name")
                 )
             )))
   
    
    if(grepl("^[a-zA-Z][a-zA-Z ]+[a-zA-Z]$",input$message, perl=T))
    {
      insertUI(
        selector = "#message",
        where = "beforeBegin",
        ui=div(class="registration",
               div(class="bubble",
                   wellPanel(
                     p(input$message),
                     p("Please enter your Number")
                   )
                   
               )))
      clearInput()
    }
    
  })
}
shinyApp(ui,server)

Your question is unclear, and the code is unrelated. Have you tried the openxlsx package? The function openxlsx::writeDataTable() can write a datalframe in an excel file. Inputting text data using a shiny app is a separate question and reading a csv file is a separate question. Please separate them out if you want replies.

I have updated the heading.
I want to store the name and Mobile number in a csv file.

when should that behaviour happen ?
should it overwrite, or create multiple csvs ?

It shall create another row for the input by overwriting it.

ok, so every time, it adds a row.
on every changed input event ?

Absolutely.. It shall do that.

library(shiny)
ui <- fluidPage(
  titlePanel("Application"),
  fluidRow(column(
    width = 8,
    downloadButton("mydownload"),
    textInput("message", label = "", placeholder = "Type your message here."),
    actionButton("send", "Send"), heading = "Smart Advisor", status = "primary"
  ))
)


# initialise a csv
mytempfile <- paste0(tempfile(), ".csv")
# write a header

write("event_no,message", file = mytempfile, append = FALSE)

server <- function(input, output, session) {
  clearInput <- function() {
    updateTextInput(session, "message", value = "")
  }


  observeEvent(input$send, {
    write(paste0(input$send, ",", input$message),
      file = mytempfile, append = TRUE
    )

    # Case 1
    insertUI(
      selector = "#message",
      where = "beforeBegin",
      ui = div(
        class = "registration",
        div(
          class = "bubble",
          wellPanel(
            p("Please enter your Name")
          )
        )
      )
    )


    if (grepl("^[a-zA-Z][a-zA-Z ]+[a-zA-Z]$", input$message, perl = T)) {
      insertUI(
        selector = "#message",
        where = "beforeBegin",
        ui = div(
          class = "registration",
          div(
            class = "bubble",
            wellPanel(
              p(input$message),
              p("Please enter your Number")
            )
          )
        )
      )
      clearInput()
    }
  })

  output$mydownload <- downloadHandler(
    filename = function() {
      paste("data-", Sys.Date(), ".csv", sep = "")
    },
    content = function(con) {
      file.copy(mytempfile, con)
    }
  )
}
shinyApp(ui, server)

what if I don't want the user to see my download handler? Also, I want to save everything when a condition is met. Like in reprex- when I have inserted the mobile number then it shall store the data and the UI shall close.

I show you how to write to the csv file continuously. you can remove the download button and downloadhandler.
you can have your own local permanent file.
you can arrange for things to happen when a condition is met, just you said before you wanted on every behaviour.
Really I think I've given you enough to attempt your own solution to your own issue.
Come back if you get stuck.

Thanks, I will do that!
Just one thing, in my textbox("message") I want to pass only numbers upto one decimal only. I am trying with-
round( grepl("^[3-9][0-9]+$", input$message ), 1).
But it's not working.

imput$message is a textInput (not a numericInput which is possible in shiny), therefore it will be of character/string type, so would need to convert to a numeric type to apply rounding.
look at the readr package.

parse_number(" 546.1 ") %>% round(digits=1)

if the number can't be parsed it will return NA

Understood. Is it possible with the grepl only ?

there's probably a way for grepl to do it, but I avoid regex as much as possible.
You can start a fresh thread about gprel'ing numbers out of strings to 1dp. i expect its complicated because you might have to match whole numbers as well as decimals.

Ok. so for condition-
if(round( grepl("^[3-9][0-9]+$", input$message ), 1)), what should I opt. I mean the optimal solution?

I recommend making a new thread for a new problem.
If you want people talented at regex to notice the question, then it should not shiny inthe title and tags, and you already marked a solution here so...

The round is never going to work unless its provided a number, the most you can hope the regex grepl could do is return the part of the string that has a number in it.

Thank a ton! I ll post a new thread :slight_smile: :slight_smile:

Hi Nir,
can you guide me how to save data with different session and not events?

you'll have to say more about it.

In my application, I have 8 details(like a registration form i.e name, phone number, address) to be stored in an excel/csv file. All are in If else condition, so in a session when I fill the first detail it shall store in the dataframe of a csv file. Like this all details to be filled in the csv file during run time and not after clicking an action button or submit button.