[英]Deploying dataTableOutput and plot in Shiny in R; "tidying" the dataTableOutput
[英]Linking dataTableOutput and plot in Shiny in R
我想在一個閃亮的應用程序中鏈接一個dataTableOutput
和一個繪圖,這樣當在表中選擇一行或一個單元格時,繪圖將使用與該行關聯的數據進行響應更新。
這是我的數據:-
數據
#relevant libraries
library(wakefield)#for generating the Status variable
library(dplyr)
library(stringi)
library(Pareto)
library(uuid)
library(ggplot2)
library(shiny)
library(DT)
#mock data creation
set.seed(1)
#data<-data.frame()
Date<-seq(as.Date("2015-01-01"), as.Date("2015-12-31"), by = "1 day")
Date<-sample(rep(Date,each=10),replace = T)
event<-r_sample_factor(x = c("Wrestling", "Drama",
"Information", "Football", "Rugby", "Movie", "Music", "News"), n=length(Date))
channel<-r_sample_factor(x = c("Channel 1", "Channel 2", "Channel 3", "Channel 4"), n=length(Date))
Hour<-r_sample_factor(x = c(0:23), n=length(Date))
Group<-r_sample_factor(x = c("A","B","C","D","E"), n=length(Date))
#creating user ID
set.seed(1)
n_users <- 100
n_rows <- 3650
relative_probs <- rPareto(n = n_users, t = 1, alpha = 0.3, truncation = 500)
unique_ids <- UUIDgenerate(n = n_users)
AnonID <- sample(unique_ids, size = n_rows, prob = relative_probs, replace = TRUE)
data<-data.frame(AnonID,Group,Date,Hour,channel,event)
data$Hour<-as.numeric(data$Hour)
head(data)
到目前為止,這是我閃亮的應用程序:-
閃亮代碼
#ui================================
ui<-fluidPage(
titlePanel("Example panel"),
tabsetPanel(
tabPanel("example text",
sidebarPanel(width = 4,
dateRangeInput("daterange","Select dates", format = "yyyy-mm-dd",
start = min("2015-01-01"),
end = max("2015-01-10")),
numericInput("hourmin", "Select mininum hour",10,0,23),
numericInput("hourmax", "Select maximum hour", 22,0,23),
pickerInput("channel", "Select channel",
choices = unique(channel), options = list('actions-box'=T,'live-search'=T),multiple = T)),#end of sidebarPanel
mainPanel(
column(width = 10, plotOutput("barplot", width = "100%")),
column(width = 8, dataTableOutput("table"))
)#end of mainPanel
)
)#end of tabPanel
)#end of tabsetPanel
)#end of fluidPage
#server===========================================
server<-function(input,output,session){
rv <- reactiveVal(NULL)
observe({
rv(data)
output$table<-renderDT({
rv()%>%
arrange(desc(Date))%>%
filter(Date>=input$daterange[1] & Date<=input$daterange[2])%>%
filter(Hour>=input$hourmin & Hour<=input$hourmax)%>%
filter(channel %in% input$channel)%>%
group_by(channel,Date)%>%
arrange(Date)%>%
summarise(Programme=paste0(Hour,":",substr(event,1,3)), .groups = 'drop')%>%
#mutate(rn=rowid(Date))%>%
pivot_wider(names_from = Date,values_from = Programme) # %>%
#select(-rn)
})
output$barplot<-renderPlot({
rv()%>%
filter(Date>=input$daterange[1] & Date<=input$daterange[2])%>%
filter(Hour>=input$hourmin & Hour<=input$hourmax)%>%
filter(channel %in% input$channel)%>%
group_by(Date,Group)%>%
summarise(UniqueID=n_distinct(AnonID))%>%
ggplot()+
geom_bar(aes(x=Date,y=UniqueID, fill=Group), stat = "identity", position = "dodge")
})
})#end of observe
}
shinyApp(ui,server)
這會給你這個:-
我想要做的是能夠單擊dataTableOutput
的一行(按日期和小時),然后在與該日期和時間關聯的數據中繪制唯一的UniqueID
的數量。 我的代碼中缺少什么可以讓我這樣做?
謝謝!
作為第一步,我們可以使用reactiveValues
從渲染保持數據datatable
。
rv <- reactiveValues(data = NULL)
其次,我們可以使用從DT
表中選擇行的可能性。
output$table <- DT::renderDataTable({
DT::datatable(rv$data, selection = list(target = "row"))
})
現在剩下的就是捕獲結果表中包含的小時和日期,並使用該信息創建 de barplot。
請注意,由於以前使用pivot_table
,此表將包含日期作為列名和包含在字符串中的小時數。
代碼:
# relevant libraries
library(wakefield) # for generating the Status variable
library(tidyverse)
library(stringi)
library(Pareto)
library(uuid)
library(shiny)
library(DT)
library(shinyWidgets)
################### // UI //###################
ui <- fluidPage(
titlePanel("Example panel"),
tabsetPanel(
tabPanel(
"example text",
sidebarPanel(
width = 4,
dateRangeInput("daterange", "Select dates",
format = "yyyy-mm-dd",
start = min("2015-01-01"),
end = max("2015-01-10")
),
numericInput("hourmin", "Select mininum hour", 10, 0, 23),
numericInput("hourmax", "Select maximum hour", 22, 0, 23),
pickerInput("channel", "Select channel",
choices = unique(channel),
options = list("actions-box" = T, "live-search" = T),
multiple = T,
selected = unique(channel)
)
), # end of sidebarPanel
mainPanel(
column(width = 10, plotOutput("barplot", width = "100%")),
column(width = 8, DTOutput("table"))
) # end of mainPanel
) # end of tabPanel
) # end of tabsetPanel
) # end of fluidPage
################### // SERVER //###################
server <- function(input, output, session) {
rv <- reactiveValues(data = NULL)
# TABLE -------------------------------------------------------------------
observe({
rv$data <-
data %>% # data was created outside the shiny app meaning that is present in the global env
arrange(desc(Date)) %>%
filter(Date >= input$daterange[1] & Date <= input$daterange[2]) %>%
filter(Hour >= input$hourmin & Hour <= input$hourmax) %>%
filter(channel %in% input$channel) %>%
group_by(channel, Date) %>%
arrange(Date) %>%
summarise(Programme = paste0(Hour, ":", substr(event, 1, 3)), .groups = "drop") %>%
pivot_wider(
names_from = Date,
values_from = Programme,
values_fn = as_mapper(~ reduce(., paste, sep = ","))
)
output$table <- DT::renderDataTable({
DT::datatable(rv$data, selection = list(target = "row"))
})
})
observeEvent(input$table_rows_selected, {
req(input$table_rows_selected)
# the dates i want are the names of the columns
dts <- rv$data[input$table_rows_selected, ] %>%
names() %>%
`[`(-1)
hrs <- # this will give me the hours present in the cells selected from DT
rv$data[input$table_rows_selected, ] %>%
map(~ str_extract_all(., "\\d+")) %>%
`[`(-1) %>%
reduce(c) %>%
reduce(c)
# i know that i need the first column only
channel_values <- rv$data[input$table_rows_selected, 1] %>%
pull(channel) %>%
as.character()
###### BARPLOT #########
output$barplot <- renderPlot({
data %>%
filter(Date >= min(dts) & Date <= max(dts)) %>%
filter(Hour >= min(hrs, na.rm = TRUE) & Hour <= max(hrs, na.rm = TRUE)) %>%
filter(channel %in% channel_values) %>%
group_by(Date, Group) %>%
summarise(UniqueID = n_distinct(AnonID)) %>%
ggplot() +
geom_bar(aes(x = Date, y = UniqueID, fill = Group), stat = "identity", position = "dodge")
})
}) # end of observe
}
shinyApp(ui, server)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.