"选择"在R-Shiny中拉一列形成一个放在RMySQL中的表

时间:2017-07-21 14:49:22

标签: r shiny

我有桌子" table1"在MySQL中使用列" name"和"地址"。现在我正在开发R-Shiny模块,以显示该表中存在的所有名称(唯一)列表的下拉列表。有人可以帮助代码。

需要了解选项= table1 $ name的工作原理,因为对我而言,由于未找到对象table1而导致错误消息'。请帮助..

1 个答案:

答案 0 :(得分:0)

首先,您需要在MySQL和R Studio之间建立连接。

sqlQuery <- function (query) {

  # creating DB connection object with RMysql package
  DB <- dbConnect(MySQL(), user='username', password='pass', dbname='databasename', host='localhost')

  # send Query to obtain result set

    rs <- dbSendQuery(DB, query)

  # get elements from result sets and convert to data frame
  result <- fetch(rs, -1)

  # to close resultset in RMySQL
  dbClearResult(dbListResults(DB)[[1]])

  # close db connection after function call exits
  on.exit(dbDisconnect(DB), add = FALSE)

  # return the dataframe
  return(result)
}

闪亮的仪表板网络应用

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody( 
   uiOutput("firstSelection")  
  )
)
mydata <- sqlQuery("select name, address from table1")
server <- function(input, output) { 

output$secondSelection <- renderUI({

selectInput("name", "Select Name", choices = mydata$name)

}) 
}

shinyApp(ui, server)