Issues in creating data frame from several user inputs in Shiny and saving them for each user to export as csv or excel format

I am trying to create a web-form from shiny app that will take certain user inputs and save it in a dataframe and append that for each user in the dataframe. Finally this could be exported in csv or excel format.

I am new in R / RShiny and have basic understanding of R, dplyr, and now learning Shiny and facing challenges.

So far I have tried this code:

UI

library(shiny)
library(shinydashboard)

# Define UI for application
ui <- fluidPage(
    headerPanel(title = "Reactive text widget"),
    sidebarLayout(
        sidebarPanel(
          textInput("fullname","Enter your complete name"),
          selectInput("city", "Choose your City", 
                      choices = c("London","Chester","Manchester")),
          textInput("phone", "Enter your Mobile Number"),
          radioButtons("status", "Choose your martial status", 
                       choices = c("Single","Married","Divorced"), inline = T),
          selectInput("activity", "Choose your fitness activity", 
                       choices = c("Gym","Yoga","Dance","Sports"),multiple =T),
          sliderInput("age", "Enter your age (in years)", 
                      min = 1, max = 150, value = 35, step = 1), br(),
          sliderInput("weight", "Enter your weight (in Kgs)", 
                      min = 1, max = 180, value = 70, step = 1),
          sliderInput("targetweight", "Enter your desired weight range (in Kgs)", 
                      min = 40, max = 150, step = 1, value = c(50,65)),
          submitButton("Submit Form")

        ),
    
    mainPanel(
      textOutput("full_name"),
      textOutput("current_city"),
      textOutput("mobile_no"),
      textOutput("martial_status"),
      textOutput("fitness_Activity"), 
      textOutput("person_age"), 
      textOutput("weight_kgs"),
      textOutput("desired_weight"),
      tableOutput("my_table")

    )

  )
)

Server

# Define server logic 
server <- function(input, output, session) {
  
  output$full_name <- {(
    renderText(input$fullname)
  )}
  
  output$current_city <- {(
    renderText(input$city)
  )}
  
  output$mobile_no <- {(
    renderText(input$phone)
  )}
  
  
  output$martial_status <- {(
    renderText(input$status)
  )}
  
  output$fitness_Activity <- {(
    renderText(input$activity)
  )}
  
  output$person_age <- {(
    renderText(input$age)
  )}
  
  output$weight_kgs <- {(
    renderText(input$weight)
  )}
  
  output$desired_weight <- {(
    renderText(input$targetweight)
  )}
  

  # Creating Data Frame from User inputs ------------------------------------    
  observe({    
    Name <- input$fullname
    City <- input$city
    Phone <- input$phone
    Martial_Status <- input$status
    Fitness_Activity <- input$activity
    Age <- as.integer(input$age)
    Weight <- as.numeric(input$weight)
    Desired_Weight <- input$targetweight
      
    df_user <- as.data.frame(cbind(Name, City, Phone, Martial_Status, Fitness_Activity,
                                  Age, Weight, Desired_Weight))
    })
    
    observeEvent({
      output$my_table <- renderPrint(df_user())
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

Will also appreciate if someone can share good resources to learn Rshiny from.

Thanks !!

if df_user is meant to contain the information of the current state of the app inputs, then I think that rather than observe, you want an reactive expression.
https://shiny.rstudio.com/tutorial/written-tutorial/lesson6/

thanks @nirgrahamuk . Now it's working with reactive but I am getting two rows in the data frame. Any idea why that is happening and how can I fix it ?

Here is the updated section of the server code:

  df_user <- reactive({
    
    Name <- input$fullname
    City <- input$city
    Phone <- input$phone
    Martial_Status <- input$status
    Fitness_Activity <- input$activity
    Age <- as.integer(input$age)
    Weight <- as.numeric(input$weight)
    Desired_Weight <- input$targetweight
      
    as.data.frame(cbind(Name, City, Phone, Martial_Status, Fitness_Activity,
                                  Age, Weight, Desired_Weight))
    })
    
    output$my_table <- renderDataTable({
      print(df_user())
    })
   Name   City  Phone Martial_Status Fitness_Activity Age Weight Desired_Weight
1 jsnow London 847548         Single              Gym  35     70             50
2 jsnow London 847548         Single              Gym  35     70             65

you are cbinding so probably the multiplicity of the activity input is causing a row for every choice selected ?

I think its partially correct, because when multiple = T and then if I select two different choices then I am getting distinct row for each choice.

But when multiple = F even then I am getting double rows for same choice.

Can this be related to submit button?

Got it, there is selection input "Desired_weight" at the bottom which has range and for each value it is creating a row. So getting at least 2 rows for each submission

1 Like