【问题标题】:Deploying dataTableOutput in shiny in R; improving layout of table contents在 R 中以闪亮的方式部署 dataTableOutput;改进表格内容的布局
【发布时间】:2021-12-29 09:43:43
【问题描述】:

我的闪亮应用中有一个dataTableOutput,我正在尝试改进其中内容的布局。我将演示它的外观,然后描述我的目标:-

数据

#relevant libraries
library(wakefield)#for generating the Status variable
library(dplyr)
library(stringi)
library(Pareto)
library(uuid)
library(ggplot2)
library(data.table)
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)

这会给你这个:-

我想要的输出是让表格的每个单元格内的行像项目符号点一样堆叠在一起。所以不要看起来像这样:-

#current look

channel  | 2015-01-01    | 2015-01-02   |...|....
------------------------------------------
Channel 1| 10:Mus,12:Dra |21:New,18:Foo |
------------------------------------------
Channel 2|               |12:New,20:Inf |  
------------------------------------------
Channel 3|20:Rug         |21:New        |
------------------------------------------
Channel 4|               |22:Inf,11:New,13:Rug

我希望它看起来更像这样:-

#desired look

channel  | 2015-01-01    | 2015-01-02   |...|....
------------------------------------------
Channel 1|10:Mus         |21:New
         |12:Dra         |18:Foo
------------------------------------------
Channel 2|               |12:New
         |               |20:Inf
---------------------------------------------
...
...

谁能建议我如何让我的数据表单元格中的输出看起来像这样?

谢谢!

【问题讨论】:

    标签: r shiny shinyapps


    【解决方案1】:

    我没有尝试重现您的代码,但取决于您的“currentlook”是否是逗号分隔的字符串,那么您应该替换 &lt;br /&gt; 的逗号,而如果“currentlook”是逗号分隔的列表,则使用 paste(x, collapse = '&lt;br /&gt;') .

    确保在renderDT() 中设置escape = F,否则它会将&lt;br /&gt; 标记打印为字符。

    【讨论】:

    • 谢谢你,这几乎就是我所追求的。我想知道是否可以让它在数值后换行,就像我在 OP 中提供的示例一样?有没有办法做到这一点?
    • 能否请您输入(部分)您的data &lt;-data.frame(AnonID,Group,Date,Hour,channel,event)
    猜你喜欢
    • 2018-10-04
    • 2021-12-24
    • 2022-10-14
    • 1970-01-01
    • 2021-12-24
    • 2017-09-05
    • 2016-06-28
    • 2017-09-30
    • 2015-03-05
    相关资源
    最近更新 更多