我想将数据保存在数据库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行。有人可以帮助我如何将我在应用程序中键入的名称放入数据库。
答案 0 :(得分:0)
为此,我已将在 selectInput 中键入的数据转换为数据帧,并将数据转储到数据库中。为了转储到数据库中,我使用了 pool 库并将 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)