I have a dataset in DB, and based on user selection, I'd like to build a bar plot using render plot in r shiny. I was able to get the data and display it as a table using renderTable, but I was not able to create renderPlot. I'm also trying to get the data from server to UI in the form of dataframe, currently, I'm getting it as dataTable.
P.S: This is my first time learning R Shiny. Just trying to understand the dynamics of this language.
Here is my code.
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)
And output will create a False (for dataframe) and create a Table with these 3 column names name, age, gender.
I'm trying to plot a simple bar plot based on name and age by grouping the genders. name on X-Axis, Age on Y-Axis, and all females grouped, all males grouped together.
Update:
Trying with renderplot instead of 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)
This code gives me the following error: Error: need finit xlim values shiny.
Storing your query in a table and converting that table into dataframe. Use renderplotly and plotly method to plot barchart.
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)