actionDataTable或表格在Shiny on actionButton上

时间:2016-04-10 23:45:53

标签: r shiny reactive-programming

我正在制作一个Shiny应用程序,该应用程序绘制了我在Fitbit中的每日步骤的直方图。它基本上是一项正在进行的工作,以构建完整的健身指标仪表板。现在,我正在处理日常步骤,以便在通过OAuth2.0找出API scrape的同时处理它。

Anywho,我所拥有的一切目前都是直方图,绘图,缩放等。我错过了什么,似乎无法弄清楚即使通过Shiny文献和Stack的大量阅读,但似乎在某处错过了标记。我尝试过反应,eventReactive,eventObserve,隔离,观察等等。

因此,考虑到这一点,有人可以帮我弄清楚如何使用我在此代码中创建的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(&#34; table&#34;)需要从tabSetPanel内部移动,但需要在mainPanel()内移动。现在它很精彩。

1 个答案:

答案 0 :(得分:0)

tableOutput(&#34; table&#34;)需要从tabSetPanel内部移动,但需要在mainPanel()内移动。现在它很精彩。