2017-05-05 14 views
1

それぞれの入力を入力してアクションボタンをクリックすると、別のデータテーブルを吐き出すような光沢のあるアプリケーションを書くのに助けが必要です。私は単にデータテーブルに出力するためのアプリを持っていますが、新しいエントリを上書きします。私はR Shinyのリファレンスセクションをチェックして、insertUI()を使う必要があるようですが、Shinyをよく理解していないと分かりません。助けてください。ありがとう。アクションボタンをクリックした後に新しい/別のデータテーブルを追加する方法

library(shiny) 
library(rhandsontable) 

#UI 
ui <-shinyUI(fluidPage(
    titlePanel('Drug Prep'), 
    sidebarPanel(textInput('expid','Experiment ID'), 
       textInput('nsc','NSC Number'), 
       textInput('solvent','Solvent'), 
       numericInput('stkcon','Stock Conc(mg/ml)',0), 
       numericInput('Repeat','Repeat',0), 
       textAreaInput('instruct','Instructions',height='50px'), 
       numericInput('amt','Amt to weigh(mg)',0), 
       numericInput('vehicle','Tot_vol of vehicle(ml)',0), 
       hr(), 
       numericInput('grp','Group',0), 
       numericInput('micedose','Mice Dose/vial',0), 
       numericInput('dose','Dose(mg/kg)',0), 
       numericInput('doseconc','Dose Concentration(mg/ml)',0), 
       numericInput('numvial','No. of Vials',0), 
       numericInput('volA','Vol of Vehicle_A(ml)',0), 
       numericInput('volB','Vol of Vehicle_B(ml)',0), 
       numericInput('volC','Vol of Vehicle_C(ml)',0), 
       br(), 
       actionButton('save','Save') 
), 
    mainPanel(
    textOutput('nsc'), 
    verbatimTextOutput('instruct'), 
    uiOutput("hot"), 
    hr(), 
    uiOutput('hot2') 
) 
) 
) 

#SERVER 
server=function(input, output) { 
    global <- reactiveValues() 

    observeEvent(input$save, { 
    global$data[[length(global$data) + 1]] = data.frame(ExpID=input$expid,NSC=input$nsc,Stock.conc=input$stkcon, 
                 Repeats=input$Repeat,Amt=input$amt,vol=input$vehicle, 
                 stringsAsFactors = F) 
    }) 
    observeEvent(input$save, { 
    global$ditto[[length(global$ditto) + 1]] = data.frame(group=input$grp,No_of_mice_dosed=input$micedose,dose=input$dose,dose_conc=input$doseconc, 
                  No_vial=input$numvial,vol_Vehicle_A=input$volA,vol_Vehicle_B=input$volB,vol_Vehicle_C=input$volC, 
                  tot_vol=input$volA+input$volB+input$volC,stringsAsFactors = F) 
    }) 

    observeEvent(input$save, { 
    for(nr in 1:length(global$data)){ 
     local({ 
     df <- global$data[[nr]] 
     output[[paste0("hot", nr)]] <- renderRHandsontable(rhandsontable(df)) 
     }) 
    } 
    }) 

    observeEvent(input$save, { 
    for(nr in 1:length(global$ditto)){ 
     local({ 
     df<-global$ditto[[nr]] 
     output[[paste0('hot2',nr)]] <- renderRHandsontable(rhandsontable(df)) 
     }) 
    } 
    }) 
    output$instruct<-renderText({input$save 
    paste(isolate(input$nsc),isolate(input$instruct),sep='\n') 
    }) 
    output$hot <- renderUI({ 
    lapply(1:length(global$data), function(nr) rHandsontableOutput(paste0("hot", nr))) 
    }) 
    output$hot2<-renderUI({ 
    lapply(1:length(global$ditto), function(nr) rHandsontableOutput(paste0("hot2", nr))) 
    }) 
} 
shinyApp(ui = ui, server=server) 

enter image description here

私は、minicこのExcelスプレッドシートにアプリをしたい列の量は、各テーブル(提出)のためにどのように異なるかに注目してください。

+0

あなたが意味するか、あなたはボタンが次回に押されており、変更は何があったかどうかを確認した場合、テーブルに行を追加したいですか? –

+0

私は、ボタンが押されるたびに(そしてtextouputとverbatimtextoutputがヘッダーとして)、テーブルのペアを作成したいと考えています。 –

答えて

1

テーブルに新しいエントリを追加するか、テーブルを置き換えるかのように何かしたいかどうかはわかりませんでした。テーブルの新しい行を追加する例を以下に示します。 duplicated機能を使用して重複が入力されないようにチェックを追加しました。

library(shiny) 
library(rhandsontable) 

#UI 
ui <-shinyUI(fluidPage(
    titlePanel('Drug Prep'), 
    sidebarPanel(textInput('expid','Experiment ID'), 
       textInput('nsc','NSC Number'), 
       textInput('solvent','Solvent'), 
       numericInput('stkcon','Stock Conc(mg/ml)',0), 
       numericInput('Repeat','Repeat',0), 
       textAreaInput('instruct','Instructions',height='50px'), 
       numericInput('amt','Amt to weigh(mg)',0), 
       numericInput('vehicle','Tot_vol of vehicle(ml)',0), 
       hr(), 
       numericInput('grp','Group',0), 
       numericInput('micedose','Mice Dose/vial',0), 
       numericInput('dose','Dose(mg/kg)',0), 
       numericInput('doseconc','Dose Concentration(mg/ml)',0), 
       numericInput('numvial','No. of Vials',0), 
       numericInput('volA','Vol of Vehicle_A(ml)',0), 
       numericInput('volB','Vol of Vehicle_B(ml)',0), 
       numericInput('volC','Vol of Vehicle_C(ml)',0), 
       br(), 
       actionButton('save','Save') 
), 
    mainPanel(
    textOutput('nsc'), 
    verbatimTextOutput('instruct'), 
    rHandsontableOutput("hot"), 
    br(), 
    rHandsontableOutput('hot2')), 
    hr() 

) 
) 

#SERVER 
server=function(input, output, session) { 

    output$instruct <- renderText({input$save 
    paste(isolate(input$nsc),isolate(input$instruct),sep='\n') 
    }) 
    mydf <- reactiveValues() 

    observeEvent(input$save,{ 
    data <- data.frame(ExpID=input$expid,NSC=input$nsc,Stock.conc=input$stkcon, 
         Repeats=input$Repeat,Amt=input$amt,vol=input$vehicle, 
         stringsAsFactors = F) 
    mydf$df <- rbind(mydf$df,data) 
    mydf$df <- mydf$df[!duplicated(mydf$df), ] 

    data2 <- data.frame(group=input$grp,No_of_mice_dosed=input$micedose,dose=input$dose,dose_conc=input$doseconc, 
         No_vial=input$numvial,vol_Vehicle_A=input$volA,vol_Vehicle_B=input$volB,vol_Vehicle_C=input$volC, 
         tot_vol=input$volA+input$volB+input$volC,stringsAsFactors = F) 
    mydf$df2 <- rbind(mydf$df2,data2) 
    mydf$df2 <- mydf$df2[!duplicated(mydf$df2), ] 
    }) 

    output$hot <- renderRHandsontable({ 
    if(is.null(mydf$df)){ 
     return() 
    } 
    rhandsontable(mydf$df) 
    }) 

    output$hot2 <- renderRHandsontable({ 
    if(is.null(mydf$df2)){ 
     return() 
    } 
    rhandsontable(mydf$df2) 
    }) 

} 
shinyApp(ui = ui, server=server) 

enter image description here

関連する問題