简体   繁体   English

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

[英]R Shiny How to use renderPlot to build barplot while retrieving data from mysqlDB based on user selection

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 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.我能够使用 renderTable 获取数据并将其显示为表格,但我无法创建 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.我也在尝试以 dataframe 的形式将数据从服务器获取到 UI,目前,我将其作为 dataTable 获取。

PS: This is my first time learning R Shiny. PS:这是我第一次学习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.并且 output 将创建一个 False (用于数据框)并使用这 3 个列名称 name、age、gender 创建一个 Table。

I'm trying to plot a simple bar plot based on name and age by grouping the genders.我正在尝试 plot 一个简单的酒吧 plot 根据姓名和年龄分组性别。 name on X-Axis, Age on Y-Axis, and all females grouped, all males grouped together. X 轴为姓名,Y 轴为年龄,所有女性分组,所有男性分组。

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.此代码给了我以下错误:错误:需要 finit xlim 值 shiny。

First you need to render your chart to output$protein_data not to output$data as it doesn't exist anymore on your second script.首先,您需要将图表渲染为output$protein_data而不是output$data ,因为它在您的第二个脚本中不再存在。

Then change the barplot arguments to barplot(age~name, data = df)然后将条形图 arguments 更改为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)

Storing your query in a table and converting that table into dataframe.将查询存储在表中并将该表转换为 dataframe。 Use renderplotly and plotly method to plot barchart.对 plot 条形图使用 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服务器

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM