【问题标题】:How to use sql queries as output in a Rshiny web page如何在 Rshiny 网页中使用 sql 查询作为输出
【发布时间】:2019-07-15 17:48:16
【问题描述】:

所以,我正在尝试向 Rshiny 应用程序输出查询,但输出中不断出现错误。这是我的输出:

#
# 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(DBI)

 for Spark 2.1.X
Sys.setenv(SPARK_HOME="/opt/cloudera/parcels/SPARK2/lib/spark2/")
Sys.setenv(SPARK_HOME_VERSION="2.1.0")


#Connecting to Spark
sc <- spark_connect(master ="yarn-client")

ui <- fluidPage(
  numericInput("nrows", "Enter the number of rows to display:", 5),
  tableOutput("tbl")
)

server <- function(input, output, session) {
  output$tbl <- renderTable({

    iris_preview <- dbGetQuery(sc,"select * from sndbx_dx.ncct_mapping limit 3")
    iris_preview

  })
}

shinyApp(ui, server)

我只是想让 iris_preview 在我运行它时显示在闪亮的应用程序中。有人看到我做错了吗?

自从我连接到 spark 后,我想从我的 Hive 数据库中输出表。

【问题讨论】:

    标签: r shiny rstudio dbi sparklyr


    【解决方案1】:

    这是一个基于 iris 数据集和 sqlite 的简单示例,可帮助您入门:

    library(shiny)
    library(RSQLite)
    library(DBI)
    library(datasets)
    
    con <- dbConnect(RSQLite::SQLite(), ":memory:")
    dbWriteTable(con, "iris", iris)
    dbListTables(con)
    
    ui <- fluidPage(
      numericInput("nrows", "Enter the number of rows to display:", 5),
      actionButton(
        inputId = "queryButton",
        label = "Query",
        icon = icon("refresh")
      ),
      tableOutput("tbl")
    )
    
    server <- function(input, output, session) {
      iris_preview <- reactiveVal(data.frame())
    
      observeEvent(input$queryButton, {
        queryString <- sprintf("select * from iris limit %s", input$nrows)
        iris_preview(dbGetQuery(con, queryString))
      })
    
      output$tbl <- renderTable({
        iris_preview()
      })
    }
    
    shinyApp(ui, server)
    

    从长远来看,请阅读this

    【讨论】:

    • 我能尽快和你聊天吗?我有几个问题。请和谢谢你
    • 好的,请随时询问。
    • 如何显示多个查询字符串?因此,如果我想显示另一个表,我是否只需复制并粘贴带有不同 select 子句的 observeEvent 部分?
    • 在线时告诉我,我会安排聊天。
    • 要显示另一个表格,您需要第二个tableOutput
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多