2016-03-31 9 views
1

selectizeInputまたはcheckboxInputの値を渡してデータフレームを作成できるようにするアプリを構築しています。私はしばらくの間を検索し、私が期待するものと類似していることこれらのソースを見つけた:それはここからですr - 光沢を介してデータフレームへのユーザー入力値

  1. handsontable

https://github.com/jrowen/rhandsontable。私の場合は、この例とよく似ています。

shiny::runGitHub("jrowen/rhandsontable", 
       subdir = "inst/examples/rhandsontable_portfolio") 

しかし、光沢のあるウィジェットを使用して値をデータフレームに渡したいとします。例えば次のように行を追加/削除することができなければならない:

ここで
  • shinyIncubator
  • コード:

    library("shiny") 
    library('devtools') 
    install_github('shiny-incubator', 'rstudio') 
    library("shinyIncubator") 
    
    # initialize data with colnames 
    df <- data.frame(matrix(c("0","0"), 1, 2)) 
    colnames(df) <- c("Input1", "Input2") 
        server = shinyServer(
         function(input, output) { 
         # table of outputs 
         output$table.output <- renderTable(
          { res <- matrix(apply(input$data,1,prod)) 
          res <- do.call(cbind, list(input$data, res)) 
          colnames(res) <- c("Input 1","Input 2","Product") 
          res 
          } 
          , include.rownames = FALSE 
          , include.colnames = TRUE 
          , align = "cccc" 
          , digits = 2 
          , sanitize.text.function = function(x) x  
         ) 
         } 
        ) 
    
    ui = shinyUI(
        pageWithSidebar(
    
        headerPanel('Simple matrixInput example') 
    
    , 
    sidebarPanel(
    
        # customize display settings  
        tags$head(
        tags$style(type='text/css' 
           , "table.data { width: 300px; }" 
           , ".well {width: 80%; background-color: NULL; border: 0px solid rgb(255, 255, 255); box-shadow: 0px 0px 0px rgb(255, 255, 255) inset;}" 
           , ".tableinput .hide {display: table-header-group; color: black; align-items: center; text-align: center; align-self: center;}" 
           , ".tableinput-container {width: 100%; text-align: center;}" 
           , ".tableinput-buttons {margin: 10px;}" 
           , ".data {background-color: rgb(255,255,255);}" 
           , ".table th, .table td {text-align: center;}" 
    
        ) 
    ) 
        , 
        wellPanel(
        h4("Input Table") 
        ,  
        matrixInput(inputId = 'data', label = 'Add/Remove Rows', data = df) 
        , 
        helpText("This table accepts user input into each cell. The number of rows may be controlled by pressing the +/- buttons.") 
    ) 
    ) 
    , 
    mainPanel(
        wellPanel(
        wellPanel(
         h4("Output Table") 
         , 
         tableOutput(outputId = 'table.output') 
         , 
         helpText("This table displays the input matrix together with the product of the rows of the input matrix") 
        ) 
    ) 
        ) 
    ) 
    ) 
    runApp(list(ui = ui, server = server)) 
    

    値は、ユーザによって入力されなければなりませんselectizeInputcheckboxInputまたはtextInputなどの光沢のあるウィジェットから、ユーザがactionButtonをクリックするとデータフレームに渡されます。私が望むのは、上記の機能の組み合わせにかなり似ていますが、私はどのように行うのか分かりません。助言がありますか?

    事前に感謝します。

    +0

    を最も簡単な方法は、直接 'rhandsontable'を(あなたは、編集、追加、および行を削除することができます)を使用することです。あなた自身でビルドするには、textInputとactionButtonの組み合わせを使用して記述することができます(サーバーコードでボタンをクリックし、textInputから値を取得してデータフレームを構築するために 'observeEvent'を使用します) –

    +0

    値を渡すことができます光沢のあるウィジェットを使用していますが、行を追加/削除する方法はまだ分かりません。また、新しい行を追加した後、前の行を修正する必要があります。つまり、私が 'submit'を押すと、私は最初の行に' selectizeInput'または 'textInput'の値で変更されるデフォルト値を持つことになります。なぜなら今は1行しか持たないからです。次に、新しい値で新しい行を 'submit 'した後、最初の行の値を固定する必要があります。つまり、2番目の行が入力されるため、入力で変更されません。 –

    +0

    「挿入」、「更新」、「削除」の3つの異なるボタンが必要です。後者の2つの場合は、更新または削除する行を指定するための追加入力が必要です。 –

    答えて

    6

    私は2つのパッケージのどれを使用していないことになったが、これはうまく働いた:

    library(shiny) 
    
    server = shinyServer(function(input, output, session){ 
    
        values <- reactiveValues() 
    
    values$DT <- data.frame(Name = NA, 
             status = NA, 
             compare = NA, 
             stringsAsFactors = FALSE) 
    
    newEntry <- observeEvent(input$addrow, { 
        newLine <- c(input$textIn, input$boxIn, input$selectIn) 
        values$DT <- rbind(values$DT, newLine) 
    }) 
    
    newEntry <- observeEvent(input$revrow, { 
        deleteLine <- values$DT[-nrow(values$DT), ] 
        values$DT <- deleteLine 
    }) 
    
        output$table <- renderTable({ 
        values$DT 
        }) 
    
    }) 
    
    ui = shinyUI(navbarPage(
        "Backtest System", inverse = TRUE, id = "navbar", 
        tabPanel("Strategy", 
          sidebarLayout(
          sidebarPanel(
           h4("Indicator"), 
           textInput("textIn", "Text", "try"), 
           checkboxInput("boxIn", "Box", TRUE), 
           selectizeInput("selectIn", "Select", 
               choices = c(">" = ">", 
                  ">=" = ">=", 
                  "<" = "<", 
                  "<=" = "<=")), 
           actionButton("addrow", "Add Row"), 
           actionButton("revrow", "Remove Row") 
          ), 
          mainPanel(
           tableOutput("table") 
          ) 
          ) 
    ) 
    ) 
    ) 
    runApp(list(ui = ui, server = server)) 
    
    関連する問題