2017-12-28 10 views
1

データセットから読み込み、要素ごとの平均時間をカテゴリ別にグループ化した小さな光沢のあるアプリがあります。私は、各カテゴリに起因する総時間の割合を記述したテーブルを生成し、そのファイルに読み込んだ後R shiny反応テーブルの特定の要素を変更します

item time category 
apple 13 cat1 
bobcat 6 cat2 
cement 15 cat1 
doomsday 15 cat1 
elephant 7 cat2 

:私のデータセットは、より複雑なバージョンです。この部分は美しく動作しています。

カテゴリ|パーセンテージ

cat1 | 76.79

cat2 | 23.21

複雑なことには、ユーザーが特定のカテゴリに費やした時間の割合を見積もることができる入力フィールドがあります。これは、データに表示されていない推定値を取得することを目的としていますセット。次に、それぞれのカテゴリに割り当てられた時間のパーセンテージがであるかどうかを再計算する2番目のテーブルを生成し、そのカテゴリの一部であると推定される時間の割合を調整します。我々は、入力データで表現されていなかったcat2に向かう時間の50%の値をした場合、私はテーブルのように変更することが期待される:

カテゴリ|パーセンテージ

cat1 | 38.39

cat2 | 61.61 = 11.61 + 50

私は現在ユーザ入力パーセンテージの添加を除く各カテゴリ時間の正しい比率で構成されるテーブルを生成することができる午前61.61

。だから私が得るものは100%未満になります:

カテゴリ|パーセンテージ

cat1 | 38.39

cat2 | 11.61

Iは、計算されたパーセント時に入力値を加算することによりcat2ための1つの特定のPercentTime要素を微調整したいので、正しい数(61.61)です。

シャイニーアプリコード:

library(shiny) 
library(readxl) 
library(dplyr) 
library(ggplot2) 

ui = fluidPage(
    sidebarPanel(
    fileInput('file1', 'inputtext'), 
    numericInput('UnknownPercent', 'labeltext', 50)), 

    mainPanel(
    tabsetPanel(type = "tabs", 
       tabPanel("tab1", 
         tableOutput("data_only"), 
         tableOutput("data_inc_unknown_percent")) 
    ) 
) 
) 

server = function(input, output) { 

    G2T <- reactive({ 
    if(input$file1 == 0){return()} 
    inFile <- input$file1 
    if (is.null(inFile)){return(NULL)} 

    isolate({ 
     G2T_raw <- read.csv(inFile$datapath) 
    }) 
    G2T_raw 
    }) 

    TotalTime <- reactive({ 
    if(input$file1 == 0){return()} 
    inFile <- input$file1 
    if (is.null(inFile)){return(NULL)} 

    isolate({ 
     TotalTime <- G2T() %>% 
     summarise(TotalTime = sum(time)) 
     }) 
    TotalTime 
    }) 

    CatTable <- reactive({ 
    if(input$file1 == 0){return()} 
    inFile <- input$file1 
    if (is.null(inFile)){return(NULL)} 

    isolate({ 
     TotalTime <- G2T() %>% 
     summarise(TotalTime = sum(time)) 
     CatTable <- G2T() %>% 
     group_by(category) %>% 
     summarise(PercentTime = sum(time)/TotalTime * 100) 
    }) 
    CatTable 
    }) 

    CatTablePlus <- reactive({ 
    if(input$file1 == 0){return()} 
    inFile <- input$file1 
    if (is.null(inFile)){return(NULL)} 

    isolate({ 
     TotalTime <- G2T() %>% 
     summarise(TotalTime = sum(time)) 
     CatTablePlus <- G2T() %>% 
     group_by(category) %>% 
     summarise(PercentTime = ((sum(time)/TotalTime()) * (100 - input$UnknownPercent[1]))) 
     CatTablePlus <- as.data.frame(CatTablePlus) 
    }) 
    CatTablePlus 
    }) 



    output$data_only <- renderTable({ 
    req(input$file1) 
    CatTable() 
    }) 

    output$data_inc_unknown_percent <- renderTable({ 
    req(input$file1) 
    CatTablePlus() 
    }) 
} 
runApp(list(ui = ui, server = server)) 

私は私の人生のために私のテーブルの[カテゴリ= CAT2、時間]要素に私のユーザー入力の割合を追加する方法を見つけ出すことはできません。どちらかrenderTableセクション、

  • が別の場所にテーブル操作ロジックを移動する、またはCatTablePlus[CatTablePlus$category == "cat1", "PercentTime"] <- CatTablePlus[CatTablePlus$category == "cat1", "PercentTime"] + input$UnknownPercent[1]のようなコマンドを使用して、変更可能な要素を持つものとして、私の作成したテーブルの治療反応性セクション
  • でそれを維持する:私が試してみました。私がこれにどのようにアプローチするかに応じて、私はすべてのPercentTime要素にUnknownPercent [1]を追加する操作を実行するか、Error: 'row.names' should specify one of the variablesのようなエラーを出力します。
  • 反応テーブルの代わりに、新しいローカル変数を作成するように私は反応的、シャイニーrenderTableオブジェクト内の単一の要素を調整することができますどのようにdata.frame

など、さまざまなタイプに私の反応性テーブルを強制変換

  • オブジェクトユーザーがnumericInput値を調整すると、

  • +0

    だけで簡単にチェック、あなたもあなたのデータで唯一の2つのカテゴリがありますか? – amrrs

    +0

    私は実際のデータセットに5つのカテゴリを持っていますが、私はそれらのうち2つを最終的に変更しています。 – omgitsjessie

    +0

    次のコードは光沢のある部分でうまくいくはずですが、5つのカテゴリの場合のロジックを2番目のテーブルで更新する必要があります。 btw、コードを試しましたか? – amrrs

    答えて

    0

    私はあなたのコードをいくつか変更して、あなたの一部をコメントアウトしました。

    1. は、入力を追跡するためにnumericInputを使用してデータフレームを変更してレンダリングします。
    2. カテゴリが2つしかないと仮定すると、シフトロジックのパーセンテージがハードコードされています。

    更新されたコード:

    library(shiny) 
    library(readxl) 
    library(dplyr) 
    library(ggplot2) 
    
    ui = fluidPage(
        sidebarPanel(
        fileInput('file1', 'inputtext'), 
        numericInput('UnknownPercent', 'labeltext', 50)), 
    
        mainPanel(
        tabsetPanel(type = "tabs", 
           tabPanel("tab1", 
             tableOutput("data_only"), 
             tableOutput("data_inc_unknown_percent")) 
        ) 
    ) 
    ) 
    
    server = function(input, output) { 
    
        G2T <- reactive({ 
        if(input$file1 == 0){return()} 
        inFile <- input$file1 
        if (is.null(inFile)){return(NULL)} 
    
        isolate({ 
         G2T_raw <<- read.csv(inFile$datapath) 
        }) 
        G2T_raw 
        }) 
    
        TotalTime <- reactive({ 
        if(input$file1 == 0){return()} 
        inFile <- input$file1 
        if (is.null(inFile)){return(NULL)} 
    
        isolate({ 
         TotalTime <- G2T() %>% 
         summarise(TotalTime = sum(time)) 
        }) 
        TotalTime 
        }) 
    
        CatTable <- reactive({ 
        if(input$file1 == 0){return()} 
        inFile <- input$file1 
        if (is.null(inFile)){return(NULL)} 
    
        isolate({ 
         CatTable <- G2T() %>% group_by(category) %>% 
         summarise(time = sum(time)) %>% ungroup(category) %>% mutate(time = (time/sum(time))*100) 
        }) 
        CatTable 
        }) 
    
        CatTablePlus <- eventReactive(input$UnknownPercent,{ 
    
    
        # TotalTime <- G2T() %>% 
         # summarise(TotalTime = sum(time)) 
    
         # G2T() %>% 
         # group_by(category) %>% 
         # summarise(PercentTime = ((sum(time)/TotalTime) * (100 - input$UnknownPercent[1]))) 
    
        unknownpct_table <- CatTable() 
    
        #unknownpct_table$time[unknownpct_table$category == 'cat2'] <- 100 - unknownpct_table$time[unknownpct_table$category == 'cat1'] 
    
        unknownpct_table$time[unknownpct_table$category == 'cat1'] <- unknownpct_table$time[unknownpct_table$category == 'cat1'] - unknownpct_table$time[unknownpct_table$category == 'cat1'] * (input$UnknownPercent[1] /100) 
        unknownpct_table$time[unknownpct_table$category == 'cat2'] <- 100 - unknownpct_table$time[unknownpct_table$category == 'cat1'] 
    
        unknownpct_table 
    
        }) 
    
    
    
        output$data_only <- renderTable({ 
        req(input$file1) 
        CatTable() 
        }) 
    
        output$data_inc_unknown_percent <- renderTable({ 
        req(input$file1) 
        CatTablePlus() 
        }) 
    } 
    runApp(list(ui = ui, server = server)) 
    
    +0

    はい!これは完璧で正確に私が必要なものです。ありがとうございました!私は5つのカテゴリにハードコードを拡張することができると確信しています。可愛くないでしょうが、それはうまくいくでしょう:) – omgitsjessie

    +0

    それを知ってうれしい! – amrrs

    関連する問題