【问题标题】:Interactive Scatter plot in R using Plotly and ShinyR中使用Plotly和Shiny的交互式散点图
【发布时间】:2019-04-12 19:32:47
【问题描述】:

我对 Shiny 和 Plotly 比较陌生,并且有以下代码 sn-p:

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)
library(plotly)
library(odbc)
library(DBI)



# Define UI for application that draws a histogram
ui <- fluidPage(

   # Application title
   titlePanel("Demo"),

   #Sidebar with a slider input for number of bins
   sidebarLayout(
      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 0,
                     max = 100,
                     value = 70)
      ),

      # Show a plot of the generated distribution
      mainPanel(
        tabPanel("Heading", plotlyOutput("tbTable"))

      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {

  QueriedData <- reactive({

    connn <- DBI::dbConnect(odbc::odbc(),.connection_string = "XXX", uid = "AB", pwd = "CD")
    lat_rec.df <- dbGetQuery(connn, "PQR")
    dbDisconnect(connn)
    lat_rec.df1
  })  


   output$tbTable <- renderPlotly({

     plot_ly(QueriedData(),x = ~TotalCount, y = ~MyScore, type = 'scatter', mode = 'markers')

  })

}

# Run the application 
shinyApp(ui = ui, server = server)

正如您在上面看到的,我正在绘制我从数据库中读取的数据帧的散点图(如反应函数中所述)。我有几个问题:

  1. 我想使用滑动条输入作为我的 Y 轴 (MyScore)。我怎么做?我目前无法将滑块(箱)链接到我的绘图图中。我希望散点图根据滑块输入进行更新。
  2. 我对反应函数有点困惑。这是否意味着每次我更改滑块时,数据库都会被调用(在反应函数中)?它是如何工作的?
  3. 如果我有其他数据库表要读取和绘制在其他区域,我是否将其包含在反应函数中?请指教。

提前感谢您的帮助!干杯!

【问题讨论】:

  • 需要为带有 ui 和服务器的散点图添加更多代码,最后你需要使用渲染图。我只在闪亮的页面上看到了几篇文章。

标签: r shiny r-plotly


【解决方案1】:

我对您三个问题的解决方案/答案。

1.如您想知道如何使用sliderInput控制Y轴,下面的代码解释了如何做到这一点。

library(shiny)
library(plotly)
library(DBI)
library(pool)

pool <- dbPool(drv = RMySQL::MySQL(),dbname = "db",host = "localhost",username = "root",password = "psw", port = 3306)
data <- dbGetQuery(pool, "SELECT * FROM testTable;")
ui <- fluidPage(
  titlePanel("Demo"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins", "Number of bins:", min = 0, max = 100, value = 70)
    ),
    mainPanel(
      tabPanel("Heading", plotlyOutput("tbTable"), 
               plotOutput("basicPlot") # Added extra as an Example for 3rd question
               )
    )
  )
)
server <- function(input, output, session) {
  QueriedData <- reactive({
    df <- data[data$total <= input$bins,] # filtering datafarme based on sliderInput
    return(df)
  })
  output$tbTable <- renderPlotly({
    plot_ly(QueriedData(), x = ~count, y = ~total, type = 'scatter', mode = 'markers')
  })
  # Added extra as an Example for 3rd question
  output$basicPlot <- renderPlot({
    data_for_plot <- dbGetQuery(pool, "SELECT * FROM dummyTable WHERE uid = 2018;")
    plot(x = data_for_plot$category, y = data_for_plot$performance, type = "p")
  })
}
shinyApp(ui = ui, server = server)

2.对于反应性,最好将表一次提取到数据帧中,然后将该数据帧放入反应性环境中。这样就可以避免多次数据库调用。您可以检查上面的代码。

3.reactive 环境的使用完全取决于您想要与闪亮的应用程序进行交互时的要求。如果您想从其他表中获取数据并在不同的图中使用,则无需将您的数据库连接字符串放置在响应式环境中。只需根据您的要求查询数据库,如上面的代码。

【讨论】:

  • 非常感谢您的代码。这真的很有帮助,你的解释也很准确。我还有另一个后续问题,希望您也能回答。您会看到“TotalCount”和“MyScore”是我的数据框中的 2 列,“MyScore”中的值可以从 0 扩展到 100。在散点图中,我实际上(现在我意识到)我想控制值的范围显示取自输入滑块。所以在代码中,我做了以下更改:滑块的值 = c(0,100)。但我需要在我的散点图中显示该范围。我希望你能理解我的问题。再次感谢
  • 我希望我能说得通;我想在 Y 轴上显示“MyScore”列,但范围必须由输入滑块决定。再次感谢并为给您带来的不便深表歉意。
  • 好的,我想通了:在反应函数中,我添加了以下代码并得到了我想要的范围:df = input$bins[1])
  • 很高兴它对您有所帮助。很高兴您得到了后续问题的解决方案。
猜你喜欢
  • 2021-05-02
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 2019-03-20
  • 2016-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多