2016-04-10 7 views
1

私は他のものの中でも私の毎日のステップのヒストグラムをプロットするShinyアプリケーションに取り組んでいます。基本的には、フィットネスメトリックの完全なダッシュボードを構築する作業です。今はOAuth2.0を介してAPIスクレープを理解している間に、私は毎日の手順でAPIを扱う作業をしています。renderDataTableまたはShiny on ActionButtonのテーブル

これまでヒストグラム、プロット、スケーリングなどを使って、私が持っているものはすべてこれまでに働いていました。私が見逃しているのは、Shiny文学とスタックを例にして数多くの読解をしても理解できないようですが、どこかでマークが紛れているようです。

これを念頭に置いて、私がこれで作成したdplyr集計テーブルからプリントをレンダリングする方法を理解できる人がいるかもしれませんか?コード?

ui.R

library(shiny) 

# Define UI for application that draws a histogram 
shinyUI(fluidPage(

    # Application title 
    titlePanel("Data Products - Final Project"), 

    # Sidebar with a slider input for the number of bins 
    sidebarLayout(
    sidebarPanel(
     helpText("Select some of the features of the histogram."), 

     sliderInput("bins", label = h4("Number of bins: ") 
        , min = 5 
        , max = 50 
        , value = 10), 
     radioButtons("radio-color", helpText(h5("Select a color for density plot.")), 
        choices = list("Salmon" = "salmon", "Black" = "black" 
            ,"Red" = "red", "Dark Blue" = "darkblue" 
            , "Dark Grey" = "darkgrey") 
        ,selected = "salmon"), 
     helpText(h5("Select some plot overlays")), 
     checkboxInput("checkCurve", label = "Curve", value = FALSE), 
     checkboxInput("checkMean", label = "Mean", value = FALSE), 
     checkboxInput("checkMed", label = "Median", value = FALSE), 
     helpText(h5("Generate daily summary statistics?")), 
     actionButton("tableButton", label = "Generate") 
    ),#end sideBarPanel 

    # Show a plot of the generated distribution 
    mainPanel(
    tabsetPanel(
    tabPanel(p(icon("line-chart"), "Visualize Data"), 
      plotOutput("histPlot", height = "400px") 
    ), #end viz tab 
    tabPanel(p(icon("about"), "About")) #end dataset tab 

), #end tabsetPanel 
    tableOutput("table") 
    )#End mainPanel 
)#End sidebarLayout 
)#End fluidPage 
)#End ShinyUI 

server.R

library(shiny) 
library(dplyr) 
library(lubridate) 
library(data.table) 

dat <- fread("data/fitbit_data.csv", stringsAsFactors = FALSE, na.strings = "0") 
dat$Day <- weekdays(x = as.Date(dat$Date, "%m/%d/%Y" 
           ,label = TRUE, abbr = FALSE)) 
dat$Steps <- as.numeric(sub(",","",dat$Steps)) 
dat$`Calories Burned` <- as.numeric(sub(",","",dat$`Calories Burned`)) 
dat$`Minutes Sedentary` <- as.numeric(sub(",","",dat$`Minutes Sedentary`)) 
dat$`Activity Calories` <- as.numeric(sub(",","",dat$`Activity Calories`)) 

# Define server logic required to draw a histogram 
shinyServer(function(input, output) { 

    output$histPlot <- renderPlot({ 
    steps <- dat$Steps 
    bins <- seq(min(steps, na.rm = TRUE), max(steps, na.rm = TRUE) 
       , length.out = input$bins + 1) 

    h <- hist(dat$Steps, breaks = bins, density = 45, col = input$`radio-color` 
     , xlim = c(500, 25000) 
     , ylim = c(0, 25) 
     , xlab = "# of Steps" 
     , ylab = "Frequency" 
     , main = "Histogram of Steps") 

    m <- mean(dat$Steps, na.rm = TRUE) 
    s <- sqrt(var(dat$Steps, na.rm = TRUE)) 
    md <- median(dat$Steps, na.rm = TRUE) 
    xfit <- seq(min(dat$Steps, na.rm = TRUE) 
       , max(dat$Steps, na.rm = TRUE), length = 40) 
    yfit <- dnorm(xfit, mean = m, sd = s) 
    yfit2 <- yfit*diff(h$mids[1:2])*length(dat$Steps) 

    if(input$checkCurve == TRUE) { 
      lines(xfit, yfit2, col = "darkblue", lwd = 2) 
    }#end plot-curve if 
    if(input$checkMean == TRUE) { 
     abline(v = m, lwd = 2, col = "blue")  
    }#end plot-mean-if 
    if(input$checkMed == TRUE) { 
     abline(v = md, lwd = 2, col = "red") 
    }#end plot-median-if 
    })#end renderPlot 

output$table <- renderTable({ 
    if(input$tableButton == 0) {return()} 
    else{ 
    dat %>% 
     group_by(Day) %>% 
     summarise(., total = sum(Steps, na.rm=TRUE) 
       , avg = mean(Steps, na.rm=TRUE) 
       , stdev = sd(Steps, na.rm = TRUE) 
       , min = min(Steps, na.rm = TRUE) 
       , max = max(Steps, na.rm = TRUE) 
       , med = median(Steps, na.rm = TRUE)) 
    } 
}) 


})#end shinyServer 

溶液に更新

tableOutput( "表")は内部から移動する必要がtabSetPanelの外側ではあるがmainPanel()の内側にある。今それはすばらしくプロットしています。

+0

'dplyr :: summarise'の結果はまだdata.frameなので、それ以外の場合は表示するために使うことができます。 'shiny :: renderDataTable'と' DT'パッケージのよりカスタマイズ可能なバージョンは素晴らしいですが、 'renderTable'だけがそれを行うべきです。 – alistaire

+0

私はrenderTable、renderDataTableを試しましたが、私のプロット内の情報をactionButton clickでドロップしているようではありません。それは何もしません。 – Zach

+0

そこにデータはありますか? – alistaire

答えて

0

tableOutput( "table")はtabSetPanelの内側からmainPanel()の内側に移動する必要がありました。今それはすばらしくプロットしています。

関連する問題