Extracting the data from a radioButton

I have an app that has multiple tabs, with a different plot and radioButton on each tab.
I want to be able to take the selection the user makes on the radioButton and extract that data to add it to a database.
I'm not exactly sure how to do this.
Would I just make a data.frame in the app of the inputs for the radioButtons?
How would I extract this data from the app to add it to a database?

Here is how I have the tabs code:

Let me know if you need to see any other code.
.

..

1 Like

Here is an example

library(shiny)
library(DBI)
# library(RSQLite)

mydb <- dbConnect(RSQLite::SQLite(), "my-db.sqlite")

ui <- fluidPage(
  radioButtons("rb", "buttons_to_log",
    choices = letters[1:3]
  )
)

server <- function(input, output, session) {
  observeEvent(input$rb, {
    if (!DBI::dbExistsTable(mydb, "button_log")) {
      DBI::dbCreateTable(
        conn = mydb,
        name = "button_log",
        fields = c(
          when = "text",
          what = "text")
      )
    }
    DBI::dbAppendTable(
      conn = mydb,
      name = "button_log",
      value = data.frame(
        when = as.character(Sys.time()),
        what = input$rb)
    )
  })
}

shinyApp(ui, server)

example of reading the values

mydb <- dbConnect(RSQLite::SQLite(),"my-db.sqlite")
(my_results <- dbGetQuery(mydb, "SELECT * FROM button_log"))
# when what
# 1 2020-10-23 12:55:23    a
# 2 2020-10-23 12:55:24    b
# 3 2020-10-23 12:55:25    c
# 4 2020-10-23 12:55:26    a
# 5 2020-10-23 12:55:27    c
# 6 2020-10-23 12:55:27    b
# 7 2020-10-23 12:55:28    c
dbDisconnect(mydb)
(unlink("my-db.sqlite"))
1 Like

@nirgrahamuk thank you for the response. I have some additional problems. I have multiple radioButtons. I think I have formatted it correctly in the attached code, but I don't get any inputs into by table when I run my app.

   # Databse connection
   observeEvent({input$radio1
                 input$radio2
                 input$radio3
                 input$radio4
                 input$radio5
                 input$radio6
                 input$radio7
                 input$radio8
                 input$radio9
                 input$radio10
               }, 
               {
       if (!DBI::dbExistsTable(con, "radioButton_logB")) {
           DBI::dbCreateTable(
               conn = con,
               name = "radioButton_logB",
               fields = c(
                   sample_id = "int",
                   tech_result = "char", 
                   tech_credential = "char"
               )
           )
       }
       DBI::dbAppendTable(
           conn = con, 
           name = "radioButton_logB",
           value = data.frame(
               sample_id = c(sampleID_list[1],
                             sampleID_list[2],
                             sampleID_list[3],
                             sampleID_list[4],
                             sampleID_list[5],
                             sampleID_list[6],
                             sampleID_list[7],
                             sampleID_list[8],
                             sampleID_list[9],
                             sampleID_list[10]),
               tech_result = c(input$radio1,
                        input$radio2,
                        input$radio3,
                        input$radio4,
                        input$radio5,
                        input$radio6,
                        input$radio7,
                        input$radio8,
                        input$radio9,
                        input$radio10,),
               tech_credential = verbatimTextOutput("auth")
           )
       )
       
   })

I have tried changing the field values from "text" to "char" and "int" for the dbCreateTable, but nothing has worked to actually get the inputs to be entered into the database.

Any help would be great.

observeEvent(event,expressions) is for observing single events.
otherwise use observe(expression - that can contain many events)

1 Like

hi @nirgrahamuk

i am getting error

mydb <- dbConnect(RSQLite::SQLite(),"my-db.sqlite")

(my_results <- dbGetQuery(mydb, "SELECT * FROM button_log"))
Error: no such table: button_log

which code did you write to make button_log ?

1 Like

library(shiny)
library(DBI)

mydb <- dbConnect(RSQLite::SQLite(), "my-db.sqlite")

Define UI for application that draws a histogram

ui <- fluidPage(

# Application title
titlePanel("Old Faithful Geyser Data"),

# Sidebar with a slider input for number of bins 
sidebarLayout(
    sidebarPanel(
        sliderInput("bins",
                    "Number of bins:",
                    min = 1,
                    max = 50,
                    value = 30)
        ,textInput("fname","Enter you First Name")
        ,dateInput("dob", "Enter your Date of Birth")
        ,numericInput("age", "Enter you Age", value = 2)
        ,selectInput("city","Select your City", choices = c("rao","mohsin"))
        ,radioButtons("rb", "buttons_to_log",
                     choices = letters[1:13])
    ),

    # Show a plot of the generated distribution
    mainPanel(
       plotOutput("distPlot")
       ,htmlOutput("showallvalues")
       
    )
)

)

Define server logic required to draw a histogram

server <- function(input, output, session) {

output$distPlot <- renderPlot({
    # generate bins based on input$bins from ui.R
    x    <- faithful[, 2]
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'darkgray', border = 'white')
})


output$showallvalues <- renderText({
    paste("<h1>rao mohsin khan</h1>")
    
})

observeEvent(input$rb, {
    if (!DBI::dbExistsTable(mydb, "button_log")) {
        DBI::dbCreateTable(
            conn = mydb,
            name = "button_log",
            fields = c(
                when = "text",
                what = "text")
        )
    }
    DBI::dbAppendTable(
        conn = mydb,
        name = "button_log",
        value = data.frame(
            when = as.character(Sys.time()),
            what = input$rb)
    )
})

}

shinyApp(ui = ui, server = server)

hi @nirgrahamuk,

when i am selecting radio button value the server code is not running

observeEvent(input$rb, {
if (!DBI::dbExistsTable(mydb, "button_log")) {
DBI::dbCreateTable(
conn = mydb,
name = "button_log",
fields = c(
when = "text",
what = "text")
)
}
DBI::dbAppendTable(
conn = mydb,
name = "button_log",
value = data.frame(
when = as.character(Sys.time()),
what = input$rb)
)

when this code is not running, so the table is not creating "button_log" in sqlite db.

i want to know why this code is not running, did you mistake here

radioButtons("rb", "buttons_to_log",
choices = letters[1:13])

??

many thanks,
rao

but that code has no my_results, so does not show the error you asked for help with....

Hi @nirgrahamuk I'm still confused how to use the observe(). Do I need to restructure the contents within it? I'm not familiar with the syntax for this? I just simply switched observeEvent() to observe() but something must be going wrong. I'm also using shinymanager for a login, and after I made the change to observe() it won't let me log into the app.

    # Databse connection
    observe({input$radio1
                  input$radio2
                  input$radio3
                  input$radio4
                  input$radio5
                  input$radio6
                  input$radio7
                  input$radio8
                  input$radio9
                  input$radio10}
                
                ,{
        if (!DBI::dbExistsTable(con, "radioButton_logB")) {
            DBI::dbCreateTable(
                conn = con,
                name = "radioButton_logB",
                fields = c(
                    sample_id = "int",
                    tech_result = "char",
                    tech_credential = "char"
                )
            )
        }
        DBI::dbAppendTable(
            conn = con,
            name = "radioButton_logB",
            value = data.frame(
                sample_id = c(sampleID_list[1],
                              sampleID_list[2],
                              sampleID_list[3],
                              sampleID_list[4],
                              sampleID_list[5],
                              sampleID_list[6],
                              sampleID_list[7],
                              sampleID_list[8],
                              sampleID_list[9],
                              sampleID_list[10]),
                tech_result = c(input$radio1,
                         input$radio2,
                         input$radio3,
                         input$radio4,
                         input$radio5,
                         input$radio6,
                         input$radio7,
                         input$radio8,
                         input$radio9,
                         input$radio10),
                tech_credential = verbatimTextOutput("res_auth")
            )
        )

    })


}

Observe differs from observeEvent by not having a first param which details what event(s) are rhe trigger.
Im sorry but there's no alternative to learning shiny than studying it. The documentation for shiny online by rstudio is very good and explains observe, observeEvent etc in great detail.

Also, debugging shiny is a complex skill. I think that after covering the fundamentals you can look at this guide.

(Shiny debugging and reprex guide)

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.