首页 > 解决方案 > 将textinput函数中写入的数据保存到mysql数据库中

问题描述

我想将数据保存在数据库 mysql 中,我将在文本输入按钮中写入。当我单击保存方案按钮时,它应该将该名称保存在我的数据库表中。

我的代码是:

library("shiny")
library("shinydashboard")
library(DBI)
pool <- dbPool(drv = RMySQL::MySQL(),dbname = "demo",host = 
"db.cr7lkdht.us-east-2.rds.amazonaws.com",username = "krtik",password 
= "123456", port = 3306)


ui <- fluidPage(


  actionButton("create_scenario", "Create Scenario"),
  actionButton("load_scenario","load scenario"),
  uiOutput("input"),
 uiOutput("inputs")
)

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

  observeEvent(input$create_scenario,{

    output$input <- renderUI({
      mainPanel(     
        textInput("txtInput","Enter Scenario Name"),
    textOutput("sname"),
                  actionButton("save","save scenario")
   )

})
output$sname <- renderText({
  input$txtInput
})



observeEvent(input$save,{
  conn <- poolCheckout(pool)
  dbSendQuery(conn,"insert into scenario(name) values ('output$sname');")

})


})

  observeEvent(input$load_scenario,{

output$inputs <- renderUI({
  # textInput("txtInput","Enter Scenario Name","Enter name as scenario  
  #(number of senario created +1)")
  dashboardPage(
    dashboardHeader(title = "Basic dashboard"),
    dashboardSidebar(),
    dashboardBody(
      # Boxes need to be put in a row (or column)
      fluidRow(
        box(plotOutput("plot1", height = 250)),

        box(
          title = "Controls",
          sliderInput("slider", "Number of observations:", 1, 100, 50)
        ))))


} )
histdata <- rnorm(500)
output$plot1 <- renderPlot({
  data <- histdata[seq_len(input$slider)]
  hist(data)
})


  })
}
shinyApp(ui, server) 

我的数据库看起来像这样

 select * from scenario;
+----------------------+-------+
| name                 | s_key |
+----------------------+-------+
| a                    |     1 |
| b                    |     2 |
| sname                |     3 |
| renderText({
 in |     4 |
 +----------------------+-------+

我已设置s_key为 auto_increment。请忽略 renderText 行。任何人都可以帮助我如何将我在我的应用程序中输入的名称放入我的数据库中。

标签: mysqlrshinyamazon-rds

解决方案


为此,我已将在selectInput中键入的数据转换为数据框并将数据转储到数据库中。为了转储到数据库中,我使用了库并将s_key设置为auto_increment。这是代码

library("shiny")
library("shinydashboard")
library("pool")
library(DBI)
 pool <- dbPool(drv = RMySQL::MySQL(),dbname = "demo",host = "db.crjwjdht.us- 
east-2.rds.amazonaws.com",username = "ka",password = "12345", port = 3306)

    mychoices = dbGetQuery(pool,"select x from scenario;")
 #mychoices = as.character(mychoices)
 ui <- fluidPage(


  actionButton("create_scenario", "Create Scenario"),
 actionButton("load_scenario","load scenario"),
selectInput('options', label="mtcars column variables", choices=myChoices, multiple = 
  TRUE),
 verbatimTextOutput("selected"), # to display the selected choices by user

 uiOutput("input"),
      uiOutput("inputs"),
  uiOutput("inputss")
)

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

     observeEvent(input$create_scenario,{

      output$input <- renderUI({
      mainPanel(     
        textInput("txtInput","Enter scenario name"),
           textOutput("sname"),
                         actionButton("save","save_scenario")
      )

   })


   output$sname <- renderText({
     input$txtInput
   })



   observeEvent(input$save,{
    #  conn <- poolCheckout(pool)
    #  dbSendQuery(conn,"insert into scenario (name) values (", output$sname <- 
 renderText({
     #  input$txtInput
      #}),");")
     dd <- data.frame(x = input$txtInput,row.names = FALSE)
      print(dd)
     dbWriteTable(pool,"scenario",dd,append = TRUE)
    # values$dd <- rbind(values$dd,data.frame(Enter = input$txtInput))
   })

    })
#  mychoices = dbGetQuery(pool,"select * from scenario;")
 # mychoices = dbGetQuery(pool,"select x from scenario;")
 # print(mychoices)



   # print(mychoices)
 observe({
   updateSelectInput(
      session, 'options', choices = mychoices
     #selected = if(input$all) myChoices
   )
 })

  output$selected <- renderText({
   paste(input$options, collapse = ",")
  })


  observeEvent(input$load_scenario,{

    output$inputs <- renderUI({
      # textInput("txtInput","Enter Scenario Name","Enter name as scenario  
      #(number of senario created +1)")
     dashboardPage(
        dashboardHeader(title = "Basic dashboard"),
    dashboardSidebar(),
    dashboardBody(
      # Boxes need to be put in a row (or column)
      fluidRow(
        box(plotOutput("plot1", height = 250)),

        box(
          title = "Controls",
          sliderInput("slider", "Number of observations:", 1, 100, 50)
        ))))


} )
histdata <- rnorm(500)
output$plot1 <- renderPlot({
  data <- histdata[seq_len(input$slider)]
  hist(data)
    })


  })
}
shinyApp(ui, server)

推荐阅读