Shiny中数据集中所有变量的数字框

时间:2017-03-02 10:03:41

标签: input shiny

我正在尝试创建一个代码,用于替换数据集中所有变量的用户输入值(每个变量的单独值)的所有缺失值?任何帮助将非常感谢!!谢谢!

更准确地说,以下是评论表中的问题。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script>

  function SortRowIDs() {
  
    // The code below finds all the <tr> elements in the table WITH checkboxes in them.
    // This way, we skip the first row containing column headers and the last row containing buttons
    
    // We use the jQuery .each() method to iterate through jQuery-object arrays 
    $('#maintable').find('tr > td > input[type="checkbox"]').each(function(index) {
      
      // We assign the parent row of the current checkbox to the variable 'currentRow'
      let currentRow = $(this).parent().parent();
      
      // Here we give the current row an id based on its position in the table
      $(currentRow).attr('id', 'id_' + (index + 1));
      
      // Prints the id's of each row
      console.log('Current row\'s id: ' + $(currentRow).attr('id'));
      
    });
    
    // This prints the id attribute of the selected checkbox's parent row, to show that 
    // the Id's were successfully assigned by the SortRowIDs() function
    console.log('');
    console.log('Selected row\'s id: ' + $( "input:checked" ).parent().parent().attr('id'));
    
  }

  function addNewRowAbove() {
    
    var rowNumber = document.getElementById("rowIndex").value;
    var rowNumberNew = parseInt(rowNumber) - 1;
    
    var newRow = $('<tr/>');
    
    newRow.html('<td><input type="checkbox" name="radio1" id="radio' + rowNumberNew + '" /><input type="hidden" id="rowIndex' + rowNumberNew + '" value="' + rowNumberNew + '"/></td><td><input type="text" name="empid" id="empid' + rowNumberNew + '"/></td><td><input type="text" name="empfname" id="empfname' + rowNumberNew + '"></input></td><td><input type="text" name="emplname" id="emplname' + rowNumberNew + '"></input></td>');
    
    var selectedRow = $( "input:checked" ).parent().parent();
    
    $(newRow).insertBefore(selectedRow);
    
    SortRowIDs();
  }

  function addNewRowBelow() {

    var rowNumber = document.getElementById("rowIndex").value;
    var rowNumberNew = parseInt(rowNumber) + 1;
    
    var newRow = $('<tr/>');
    
    newRow.html('<td><input type="checkbox" name="radio1" id="radio' + rowNumberNew + '"></input><input type="hidden" id="rowIndex' + rowNumberNew + '" value="' + rowNumberNew + '"/></td><td><input type="text" name="empid" id="empid' + rowNumberNew + '"></input></td><td><input type="text" name="empfname" id="empfname' + rowNumberNew + '"></input></td><td><input type="text" name="emplname" id="emplname' + rowNumberNew + '"></input></td>');
    
    var selectedRow = $( "input:checked" ).parent().parent();
    
    $(newRow).insertAfter(selectedRow);
    
    SortRowIDs();
  }
</script>

<form>
  <table id="maintable" width="50%" cellpadding="0" cellspacing="0" border="#729111 1px solid">
    <tr>
      <th align="center">Select</th>
      <th align="center">Employee ID</th>
      <th align="center">First Name</th>
      <th align="center">Last Name</th>
    </tr>
    <tr>
      <td>
        <input type="checkbox" name="radio" id="radio"/>
        <input type="hidden" id="rowIndex" value="1" />
      </td>
      <td>
        <input type="text" name="empid" />
      </td>
      <td>
        <input type="text" name="empfname" />
      </td>
      <td>
        <input type="text" name="emplname" />
      </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" name="radio1" id="radio1" />
        <input type="hidden" id="rowIndex" value="2" />
      </td>
      <td>
        <input type="text" name="empid1" />
      </td>
      <td>
        <input type="text" name="empfname1" />
      </td>
      <td>
        <input type="text" name="emplname1" />
      </td>
    </tr>
    <tr>
      <td></td>
      <td> 
        <input type="submit" name="AddRowAbove" value="AddRowAbove" onclick="addNewRowAbove()">
      </td>
      <td> 
        <input type="submit" name="AddRowBelow" value="AddRowBelow" onclick="addNewRowBelow()">
      </td>
      <td></td>
    </tr>
  </table>
</form>

1 个答案:

答案 0 :(得分:1)

以下是一个示例代码,可以将您置于正确的轨道,以便将来我建议创建良好的可重现示例Link),以便它将有助于更轻松,更快速地解决问题。

library(shiny)
library(DT)

x <- c(1,2, NA)
y <- c(1:3)
data <- data.frame(x,y)

ui= basicPage(
  selectInput("value_na", "Choose value:", choices = c(3,4), selected=3), #Here you can choose the value which is going to replace Na's,
  dataTableOutput("tab"),
  dataTableOutput("tab2"))

server= function(input, output,session) {

  output$tab <-  DT::renderDataTable({datatable(data,options=list(bFilter=FALSE, bPaginate=FALSE))})

  data2 <- reactive({
  data$x[is.na(data$x)] <-input$value_na
  data})

  output$tab2 <-  DT::renderDataTable({datatable(data2(),options=list(bFilter=FALSE, bPaginate=FALSE))})
}

shinyApp(ui, server)