R Shiny 如何在根据用户选择从 mysqlDB 检索数据时使用 renderPlot 构建条形图

Posted

技术标签:

【中文标题】R Shiny 如何在根据用户选择从 mysqlDB 检索数据时使用 renderPlot 构建条形图【英文标题】:R Shiny How to use renderPlot to build barplot while retrieving data from mysqlDB based on user selection 【发布时间】:2020-11-24 01:50:30 【问题描述】:

我在 DB 中有一个数据集,根据用户选择,我想使用 r shiny 中的渲染图构建条形图。我能够使用 renderTable 获取数据并将其显示为表格,但我无法创建 renderPlot。我也在尝试以数据框的形式将数据从服务器获取到 UI,目前,我将其作为 dataTable 获取。

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 个列名称 name、age、gender 创建一个 Table。

我正在尝试通过对性别进行分组来根据姓名和年龄绘制一个简单的条形图。 X 轴为姓名,Y 轴为年龄,所有女性分组,所有男性分组。

更新:

尝试使用渲染图而不是渲染表

    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 值闪亮。

【问题讨论】:

【参考方案1】:

将您的查询存储在表中并将该表转换为数据框。使用 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)

【讨论】:

【参考方案2】:

首先,您需要将图表呈现为 output$protein_data 而不是 output$data,因为它在您的第二个脚本中不再存在。

然后将 barplot 参数更改为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)

【讨论】:

以上是关于R Shiny 如何在根据用户选择从 mysqlDB 检索数据时使用 renderPlot 构建条形图的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Shiny 中的下拉框中根据变量选择动态创建直方图

如何在Shiny R中丢弃DT :: datatable上的用户编辑

根据文件选择保存和加载用户选择 - R Shiny

根据用户的输入创建列联表 - R Shiny

R-Shiny 中的 circlepackeR - 根据用户输入创建圆形包装图

如何从 R Shiny 的选择菜单中拆分选定的列?