2017-04-19 9 views
0

現在、私は、HTMLコードのサイズのために、別のファイルをソースとする静的なHTMLテーブルを表示するShinyアプリケーションを開発中です。空のテーブルをレンダリングするために、テーブルは空のデータテーブルで初期化されます。 HTMLテーブルの上には通常selectizeInput(observe()関数を介して)バックグラウンドでデータテーブルをフィルタリングするフィールドがあります。 HTMLテーブルにフィルタリングされたデータテーブルが設定されます。シャイニー - 入力に基づいてフィルタリングされたデータで静的なHTMLテーブルを作成する

「新しい」データテーブルを使用してHTMLテーブルを更新するプロセスで悩んでいます。私は観察でテーブルを再び調達しようとしました(変化なし)。私は、データテーブルをreactiveValueとして初期化し、HTMLテーブルをreact() - 関数でラップしました。 、あなたが見ることができるように

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())のような何らかの更新機能がないことを知っていますが、自分でコード化する方法を見つけることはできません。

アイデアやヒントに感謝します。

EDIT#1: HTMLテーブルのポイントを明確にするために、私のアプリに損益テーブルを表示しています。このテーブルは、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) 
} 
関連する問題