【发布时间】:2016-04-10 23:45:52
【问题描述】:
我正在开发一款 Shiny 应用程序,该应用程序绘制了我在 Fitbit 中的每日步数的直方图。建立一个完整的健身指标仪表板基本上是一项正在进行的工作。现在我只是在处理日常步骤来处理它,同时我通过 OAuth2.0 找出 API 抓取。
任何人,到目前为止,我所拥有的一切都在使用直方图、绘图、缩放等。我缺少什么,即使通过大量阅读 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("table") 需要从 tabSetPanel 内部移到它之外,但在 mainPanel() 内部。现在它的情节非常精彩。
【问题讨论】:
-
dplyr::summarise的结果仍然是一个 data.frame,因此您可以使用任何其他方式来显示它。shiny::renderDataTable和DT包的更可定制的版本很好,但只有renderTable应该这样做。 -
我已经尝试了 renderTable、renderDataTable 并且它似乎并没有在我点击 actionButton 的情节中删除信息。它什么都不做。
-
那里有数据吗?
-
是的,有数据。我的直方图绘制,如果我删除 renderTable 片段并进行打印,那么数据框将打印到控制台。
-
哦,在
ui.R中是tabSetPanel,但不是tabPanel。移动你的括号,它可能会出现。
标签: r shiny reactive-programming