2017-04-19 113 views
0

我目前工作的一個閃亮的應用程序,它顯示因爲HTML代碼的大小的靜態HTML表,從另一個文件源,過濾數據的靜態HTML表。該表使用空數據表進行初始化,以便呈現空表。在HTML表格上方是正常的selectizeInput在後臺過濾數據表的字段(通過observe()函數)。然後應該使用過濾的數據表填充HTML表格。閃亮 - 填充基於輸入

我堅持在與「新」的數據表更新HTML表的過程。我試圖在觀察()中再次找到表格 - 沒有改變。我將數據表初始化爲reactiveValue,並使用reactive()函數將HTML表格封裝起來 - 再次不變。

這裏是一個玩具例子有點象我閃亮的應用程序:

app.R

library(shiny) 

ui <- fluidPage(

fluidRow(
    column(width = 6, uiOutput("cars")) 
), 
    fluidRow(
    column(width = 6, htmlOutput("html.table")) 
) 
) 

server <- function(input, output) { 

    filtered_cars <- data.frame(matrix("NA", nrow = 1, ncol = 4, dimnames = list("NA", c("mpg","cyl","disp","hp")))) 

    source("server_html_table.R", local = TRUE) 

    output$cars <- renderUI({ 
    selectizeInput(
     inputId = "cars", 
     label = NULL, 
     choices = rownames(mtcars), 
     options = list(placeholder = 'Cars') 
    ) 
    }) 

    output$html.table <- renderUI({ 
    html.table 
    }) 

    observeEvent(input$cars, { 
    filtered_cars <- subset(mtcars, rownames(mtcars) %in% input$cars) 
    #some kind of update for the html table missing 
    }) 
} 

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

server_html_table.R

html.table <- tags$table(style = "border: 1px solid black; padding: 1%; width: 100%;", 
        tags$tr(
          tags$th("Car Name"), 
          tags$th("MPG"), 
          tags$th("CYL"), 
          tags$th("DISP"), 
          tags$th("HP") 

        ), 
        tags$tr(
          tags$td(rownames(filtered_cars)), 
          tags$td(filtered_cars$mpg), 
          tags$td(filtered_cars$cyl), 
          tags$td(filtered_cars$disp), 
          tags$td(filtered_cars$hp) 
        ) 
      ) 

正如你所看到的,表格單元不更新。我意識到在observeEvent中缺少某種更新函數(如updateSelectizeInput()),但我無法找到一種方法來自行編寫代碼。

我對任何形式的意見或建議表示感謝!

編輯#1:也許有關HTML表格更清晰的觀點 - 我在我的應用程序中顯示一個Profit and Loss表格,需要通過HTML手動構建。因此,我不能使用通常的dataTableOutput()renderDataTable()函數。由於表格嚴重依賴CSS,因此使用基本HTML比htmlTable包簡單得多。

回答

0

我找到了解決我的問題的方法!

靜態html表被封裝在一個函數中,該函數將在應用程序的服務器部分啓動一次,然後在renderUI()函數中調用。每次用戶更改菜單時都會觸發渲染功能。在這裏,我過濾與輸入有關的數據幀並將其傳遞給「build_table」函數。然後通過索引從表格中的每個單元格填充來自數據幀的所需值。該函數將完整的html表返回給renderUI()。

這是例如玩具從上方調整到工作溶液:

app.R

library(shiny) 

ui <- fluidPage(

    fluidRow(
      column(width = 6, uiOutput("cars")) 
), 
    fluidRow(
    column(width = 6, htmlOutput("html.table")) 
) 
) 

server <- function(input, output) { 

    source("server_html_table.R", local = TRUE) 

    output$cars <- renderUI({ 
    selectizeInput(
     inputId = "cars", 
     label = NULL, 
     choices = rownames(mtcars), 
     options = list(placeholder = 'Cars') 
    ) 
    }) 

    output$html.table <- renderUI({ 

    input$cars 

    isolate({ 

     filtered_cars <- subset(mtcars, rownames(mtcars) %in% input$cars) 

     build_table(filtered_cars) 
    }) 
    }) 
} 

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

server_html_table.R

build_table <- function(data){ 

    html.table <- tags$table(style = "border: 1px solid black; padding: 1%; width: 100%;", 
          tags$tr(
            tags$th("Car Name"), 
            tags$th("MPG"), 
            tags$th("CYL"), 
            tags$th("DISP"), 
            tags$th("HP") 

         ), 
          tags$tr(
            tags$td(rownames(data)), 
            tags$td(data$mpg), 
            tags$td(data$cyl), 
            tags$td(data$disp), 
            tags$td(data$hp) 
         ) 
       ) 

    return(html.table) 
}