R Shiny 如何在根据用户选择从 mysqlDB 检索数据时使用 renderPlot 构建条形图
R Shiny How to use renderPlot to build barplot while retrieving data from mysqlDB based on user selection
我在数据库中有一个数据集,根据用户的选择,我想在 r shiny 中使用渲染图构建一个条形图。我能够使用 renderTable 获取数据并将其显示为 table,但我无法创建 renderPlot。我也在尝试以数据帧的形式将数据从服务器获取到 UI,目前,我正在获取数据 Table.
P.S:这是我第一次学习R Shiny。只是想了解这种语言的动态。
这是我的代码。
library(DT)
library(shiny)
library(DBI)
# the user interface
ui <- fluidPage(
titlePanel(strong ("Welcome to User Details")),
sidebarPanel(
selectInput("selectedName", label = h5("Select the User name here"),
choices = list("A", "B", "C", "D"),
selected = "A"),
submitButton("Submit")
),
mainPanel(
is.data.frame("data"),
tableOutput("data")
)
)
# server
server <- function(input, output){
output$data <- renderTable({
conn <- dbConnect(
drv = RMySQL::MySQL(),
dbname = "mydb",
host = "localhost",
port = 3306,
username = "root",
password = "pwd")
on.exit(dbDisconnect(conn), add = TRUE)
dbGetQuery(conn, paste0(
"Select name, age, gender from table1 Where userShortName = '", intput$selectedName, "';")
})
}
# app launch
shinyApp(ui = ui, server = server)
并且输出将创建一个 False(对于数据框)并使用这 3 个列名名称、年龄、性别创建一个 Table。
我正在尝试通过对性别进行分组,根据姓名和年龄绘制一个简单的条形图。 X 轴上的姓名,Y 轴上的年龄,所有女性分组,所有男性分组在一起。
更新:
尝试使用 renderplot 而不是 renderTable
library(DT)
library(shiny)
library(DBI)
ui <- fluidPage(
titlePanel(strong ("Welcome to User Details")),
sidebarPanel(
selectInput("selectedName", label = h5("Select the User name here"),
choices = list("A", "B", "C", "D"),
selected = "A"),
submitButton("Submit")
),
mainPanel(
textOutput("sample_name"),
plotOutput(outputId = "protein_data")
))
# server
server <- function(input, output){
conn <- dbConnect(
drv = RMySQL::MySQL(),
dbname = "mydb",
host = "localhost",
port = 3306,
username = "root",
password = "pwd")
output$data <- renderPlot({
table <- dbGetQuery(conn, statement = "Select name, age, gender from table1 Where userShortName = '", intput$selectedName, "';")
df <- as.data.frame(unclass(table(table$name,
table$age)))
barplot(x=table$name, y=table$age)
})
}
# app launch
shinyApp(ui = ui, server = server)
此代码出现以下错误:错误:需要闪亮的 finit xlim 值。
首先,您需要将图表呈现为 output$protein_data
而不是 output$data
,因为它在您的第二个脚本中已不存在。
然后将条形图参数更改为 barplot(age~name, data = df)
library(DT)
library(shiny)
library(DBI)
ui <- fluidPage(
titlePanel(strong ("Welcome to User Details")),
sidebarPanel(
selectInput("selectedName", label = h5("Select the User name here"),
choices = list("A", "B", "C", "D"),
selected = "A"),
submitButton("Submit")
),
mainPanel(
textOutput("sample_name"),
plotOutput(outputId = "protein_data")
))
# server
server <- function(input, output){
conn <- dbConnect(
drv = RMySQL::MySQL(),
dbname = "mydb",
host = "localhost",
port = 3306,
username = "root",
password = "pwd")
output$protein_data <- renderPlot({
table <- dbGetQuery(conn, statement = "Select name, age, gender from table1 Where userShortName = '", intput$selectedName, "';")
df <- as.data.frame(unclass(table(table$name,
table$age)))
barplot(age~name, data = df)
})
}
# app launch
shinyApp(ui = ui, server = server)
将您的查询存储在 table 中并将该 table 转换为数据框。使用 renderplotly 和 plotly 方法绘制条形图。
library(DT)
library(shiny)
library(DBI)
library(plotly) # use plotly library here
# the user interface
ui <- fluidPage(
titlePanel(strong ("Welcome to User Details")),
sidebarPanel(
selectInput("selectedName", label = h5("Select the User name here"),
choices = list("A", "B", "C", "D"),
selected = "A"),
submitButton("Submit")
),
mainPanel(
is.data.frame("data"),
tableOutput("data")
)
)
服务器
server <- function(input, output){
output$data <- renderPlotly({
conn <- dbConnect(
drv = RMySQL::MySQL(),
dbname = "mydb",
host = "localhost",
port = 3306,
username = "root",
password = "pwd")
on.exit(dbDisconnect(conn), add = TRUE)
table <- dbGetQuery(conn, paste0(
"Select name, age, gender from table1 Where userShortName = '", input$selectedName, "';")
# converting table into dataframes, easy to plot
table1 <- as.data.frame(table)
# Barplot using plotly
plot_ly(data=table1,x=~name, y=~age, type="bar")%>%
layout(
title = "Title of Barplot",
xaxis = list(title="Name"),
yaxis = list(title="Age")
)
})
}
# app launch
shinyApp(ui = ui, server = server)
我在数据库中有一个数据集,根据用户的选择,我想在 r shiny 中使用渲染图构建一个条形图。我能够使用 renderTable 获取数据并将其显示为 table,但我无法创建 renderPlot。我也在尝试以数据帧的形式将数据从服务器获取到 UI,目前,我正在获取数据 Table.
P.S:这是我第一次学习R Shiny。只是想了解这种语言的动态。
这是我的代码。
library(DT)
library(shiny)
library(DBI)
# the user interface
ui <- fluidPage(
titlePanel(strong ("Welcome to User Details")),
sidebarPanel(
selectInput("selectedName", label = h5("Select the User name here"),
choices = list("A", "B", "C", "D"),
selected = "A"),
submitButton("Submit")
),
mainPanel(
is.data.frame("data"),
tableOutput("data")
)
)
# server
server <- function(input, output){
output$data <- renderTable({
conn <- dbConnect(
drv = RMySQL::MySQL(),
dbname = "mydb",
host = "localhost",
port = 3306,
username = "root",
password = "pwd")
on.exit(dbDisconnect(conn), add = TRUE)
dbGetQuery(conn, paste0(
"Select name, age, gender from table1 Where userShortName = '", intput$selectedName, "';")
})
}
# app launch
shinyApp(ui = ui, server = server)
并且输出将创建一个 False(对于数据框)并使用这 3 个列名名称、年龄、性别创建一个 Table。
我正在尝试通过对性别进行分组,根据姓名和年龄绘制一个简单的条形图。 X 轴上的姓名,Y 轴上的年龄,所有女性分组,所有男性分组在一起。
更新:
尝试使用 renderplot 而不是 renderTable
library(DT)
library(shiny)
library(DBI)
ui <- fluidPage(
titlePanel(strong ("Welcome to User Details")),
sidebarPanel(
selectInput("selectedName", label = h5("Select the User name here"),
choices = list("A", "B", "C", "D"),
selected = "A"),
submitButton("Submit")
),
mainPanel(
textOutput("sample_name"),
plotOutput(outputId = "protein_data")
))
# server
server <- function(input, output){
conn <- dbConnect(
drv = RMySQL::MySQL(),
dbname = "mydb",
host = "localhost",
port = 3306,
username = "root",
password = "pwd")
output$data <- renderPlot({
table <- dbGetQuery(conn, statement = "Select name, age, gender from table1 Where userShortName = '", intput$selectedName, "';")
df <- as.data.frame(unclass(table(table$name,
table$age)))
barplot(x=table$name, y=table$age)
})
}
# app launch
shinyApp(ui = ui, server = server)
此代码出现以下错误:错误:需要闪亮的 finit xlim 值。
首先,您需要将图表呈现为 output$protein_data
而不是 output$data
,因为它在您的第二个脚本中已不存在。
然后将条形图参数更改为 barplot(age~name, data = df)
library(DT)
library(shiny)
library(DBI)
ui <- fluidPage(
titlePanel(strong ("Welcome to User Details")),
sidebarPanel(
selectInput("selectedName", label = h5("Select the User name here"),
choices = list("A", "B", "C", "D"),
selected = "A"),
submitButton("Submit")
),
mainPanel(
textOutput("sample_name"),
plotOutput(outputId = "protein_data")
))
# server
server <- function(input, output){
conn <- dbConnect(
drv = RMySQL::MySQL(),
dbname = "mydb",
host = "localhost",
port = 3306,
username = "root",
password = "pwd")
output$protein_data <- renderPlot({
table <- dbGetQuery(conn, statement = "Select name, age, gender from table1 Where userShortName = '", intput$selectedName, "';")
df <- as.data.frame(unclass(table(table$name,
table$age)))
barplot(age~name, data = df)
})
}
# app launch
shinyApp(ui = ui, server = server)
将您的查询存储在 table 中并将该 table 转换为数据框。使用 renderplotly 和 plotly 方法绘制条形图。
library(DT)
library(shiny)
library(DBI)
library(plotly) # use plotly library here
# the user interface
ui <- fluidPage(
titlePanel(strong ("Welcome to User Details")),
sidebarPanel(
selectInput("selectedName", label = h5("Select the User name here"),
choices = list("A", "B", "C", "D"),
selected = "A"),
submitButton("Submit")
),
mainPanel(
is.data.frame("data"),
tableOutput("data")
)
)
服务器
server <- function(input, output){
output$data <- renderPlotly({
conn <- dbConnect(
drv = RMySQL::MySQL(),
dbname = "mydb",
host = "localhost",
port = 3306,
username = "root",
password = "pwd")
on.exit(dbDisconnect(conn), add = TRUE)
table <- dbGetQuery(conn, paste0(
"Select name, age, gender from table1 Where userShortName = '", input$selectedName, "';")
# converting table into dataframes, easy to plot
table1 <- as.data.frame(table)
# Barplot using plotly
plot_ly(data=table1,x=~name, y=~age, type="bar")%>%
layout(
title = "Title of Barplot",
xaxis = list(title="Name"),
yaxis = list(title="Age")
)
})
}
# app launch
shinyApp(ui = ui, server = server)