来自数据框列的SelectInput闪亮

时间:2017-11-07 18:31:25

标签: r shiny

我尝试从一列读取数据帧行,以感觉UI中的SelectInput对象中的列表.R Shiny UI和服务器之间的全局或本地引用存在问题,我不知道格式是否适合导入selectInput列表中的项目

这里我的DF Ref_comp只有一列(STEP_NAME):

!   STEP_NAME   !
-----------------
  L1_2_3_LR
  C46-C77-OTHERS
  R4
  R10
  C56
  Q4
  L4

这是我的UI.R

 shinyUI(pageWithSidebar(

  headerPanel("My header Text"),
  sidebarPanel(  
    radioButtons("test", "Select a DataBase", 
                       c("test1"="test1",
                        "test2"="test2")),
          textInput("card", "Enter the code card", "CARD.EX"),
          textInput("comp", "Enter the ref comp", "R3"), 
          ######## Here what I tried to do ########
          selectInput("comp_sel","Component", choices= 
          as.character(unique(unlist(Ref_comp$STEP_NAME)))),
          ########################################## 
           downloadButton("plot_export", "Save PDF")
                        ),  

 mainPanel(
    #h4("Text2"),
       library(plotly),
       plotlyOutput("plot"))


     ))

这是我的Server.R

shinyServer(function(input,output){

output$plot <- renderPlotly({

con <- odbcConnect(con, uid="xxx")

##### Here the SQL Query to have my items ######################
sql_ref = paste("select DISTINCT ...") # My SQL query on distant server
###### Output in DF Ref_comp ##############
Ref_comp <- sqlQuery(db, paste (sql_ref))
##########################################
 odbcClose(data_testeur)

 #### An other SQL Query for the graph #######

 graph <- ggplot(...
 ggplotly(graph) # Print graph
 }

 ) 
  })

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

你的问题是你在server.r中生成了data_frame Ref_comp,在这种情况下,我们必须使用renderUIuiOutput()生成selectInput动态,如下所示:

shinyUI(pageWithSidebar(

  headerPanel("My header Text"),
  sidebarPanel(  
    radioButtons("test", "Select a DataBase", 
                 c("test1"="test1",
                   "test2"="test2")),
    textInput("card", "Enter the code card", "CARD.EX"),
    textInput("comp", "Enter the ref comp", "R3"), 
    ######## Here what I tried to do ########
    uiOutput("selectComp"),
    ########################################## 
    downloadButton("plot_export", "Save PDF")
  ),  

  mainPanel(
    #h4("Text2"),
    library(plotly),
    plotlyOutput("plot"))


))

和服务器

shinyServer(function(input,output){

  refDataFrame <- reactive({
    con <- odbcConnect(con, uid="xxx")

    ##### Here the SQL Query to have my items ######################
    sql_ref = paste("select DISTINCT ...") # My SQL query on distant server
    ###### Output in DF Ref_comp ##############
    Ref_comp <- sqlQuery(db, sql_ref)
    odbcClose(data_testeur)
    Ref_comp
  })

  output$selectComp <- renderUI(
    selectInput("comp_sel","Component", choices= 
                  as.character(unique(unlist(refDataFrame()[["STEP_NAME"]]))))
  )

  output$plot <- renderPlotly({
    Ref_comp <- refDataFrame()

    #### An other SQL Query for the graph #######

    graph <- ggplot(...)
                    ggplotly(graph) # Print graph
  }

    ) 
})

由于我们现在需要在两个地方进行数据库查询的结果已经把它放在一个单独的反应函数中