如何在处理 csv 文件后添加带有统计信息的部分
How to add a section with stats after process csv file
我正在用 Shiny 做一个实验,上传一个 csv 文件,处理它并显示两个不同的东西:1) 数据的总结分析,和 2) 遵循一些特定条件的值列表,代码这是:
library(shiny)
library(DT)
library(sqldf)
library(data.table)
# Define UI
ui <- shinyUI(fluidPage(
h1("Sample Report"),
h1(""),
fileInput(
'target_upload',
'Choose file to upload',
accept = c('text/csv',
'text/comma-separated-values',
'.csv')
),
radioButtons(
"separator",
"Separator: ",
choices = c(","),
selected = ",",
inline = TRUE
),
DT::dataTableOutput("sample_table"),
tags$style(type="text/css",
".shiny-output-error { visibility: hidden; }",
".shiny-output-error:before { visibility: hidden; }"
),
))
# Define server logic
server <- shinyServer(function(input, output) {
df_products_upload <- reactive({
inFile <- input$target_upload
if (is.null(inFile))
return(NULL)
df <-
read.csv(inFile$datapath,
header = TRUE,
sep = input$separator)
return(df)
})
output$sample_table <- DT::renderDataTable({
df <- df_products_upload()
pros <-
sqldf('select * from df where XXXX="any value" AND XX > 2 ')
qual <-
sqldf('select * from df where YYYY="any value" AND XX > 2 ')
AllDataIssue <-
rbind(pros,
qual)
DT::datatable(
{ AllDataIssue },
caption = htmltools::tags$caption(
style = 'caption-side: bottom; text-align: center;',
'Table 1: ', htmltools::em('Any message.')
),
extensions = 'Buttons',
options = list(
fixedColumns = TRUE,
autoWidth = TRUE,
ordering = TRUE,
dom = 'Bftsp',
filter = 'top',
buttons = c('csv', 'excel')
))
})
})
# Run the application
shinyApp(ui = ui, server = server)
使用此代码,我的问题的第 2 部分已解决,但我不知道如何添加一个框,例如显示结果 table 的 summary(AllDataIssue)
。我的意思是,我想添加一个界面元素,其中可能包含对结果对象的基本分析。
有什么建议吗?
您可以使用包含过滤数据的反应式,然后使用 renderPrint 输出其摘要:
在 UI 中添加:
textOutput("summary"),
并更改服务器
AllDataIssue <- reactive({
df <- df_products_upload()
pros <-
sqldf('select * from df where XXXX="any value" AND XX > 2 ')
qual <-
sqldf('select * from df where YYYY="any value" AND XX > 2 ')
AllDataIssue <-
rbind(pros,
qual)
})
output$summary = renderPrint(summary(AllDataIssue()))
output$sample_table <- DT::renderDataTable({
DT::datatable(
{ AllDataIssue() },
caption = htmltools::tags$caption(
style = 'caption-side: bottom; text-align: center;',
'Table 1: ', htmltools::em('Any message.')
),
extensions = 'Buttons',
options = list(
fixedColumns = TRUE,
autoWidth = TRUE,
ordering = TRUE,
dom = 'Bftsp',
filter = 'top',
buttons = c('csv', 'excel')
))
})
注意 AllDataIssue() 后的括号`
我正在用 Shiny 做一个实验,上传一个 csv 文件,处理它并显示两个不同的东西:1) 数据的总结分析,和 2) 遵循一些特定条件的值列表,代码这是:
library(shiny)
library(DT)
library(sqldf)
library(data.table)
# Define UI
ui <- shinyUI(fluidPage(
h1("Sample Report"),
h1(""),
fileInput(
'target_upload',
'Choose file to upload',
accept = c('text/csv',
'text/comma-separated-values',
'.csv')
),
radioButtons(
"separator",
"Separator: ",
choices = c(","),
selected = ",",
inline = TRUE
),
DT::dataTableOutput("sample_table"),
tags$style(type="text/css",
".shiny-output-error { visibility: hidden; }",
".shiny-output-error:before { visibility: hidden; }"
),
))
# Define server logic
server <- shinyServer(function(input, output) {
df_products_upload <- reactive({
inFile <- input$target_upload
if (is.null(inFile))
return(NULL)
df <-
read.csv(inFile$datapath,
header = TRUE,
sep = input$separator)
return(df)
})
output$sample_table <- DT::renderDataTable({
df <- df_products_upload()
pros <-
sqldf('select * from df where XXXX="any value" AND XX > 2 ')
qual <-
sqldf('select * from df where YYYY="any value" AND XX > 2 ')
AllDataIssue <-
rbind(pros,
qual)
DT::datatable(
{ AllDataIssue },
caption = htmltools::tags$caption(
style = 'caption-side: bottom; text-align: center;',
'Table 1: ', htmltools::em('Any message.')
),
extensions = 'Buttons',
options = list(
fixedColumns = TRUE,
autoWidth = TRUE,
ordering = TRUE,
dom = 'Bftsp',
filter = 'top',
buttons = c('csv', 'excel')
))
})
})
# Run the application
shinyApp(ui = ui, server = server)
使用此代码,我的问题的第 2 部分已解决,但我不知道如何添加一个框,例如显示结果 table 的 summary(AllDataIssue)
。我的意思是,我想添加一个界面元素,其中可能包含对结果对象的基本分析。
有什么建议吗?
您可以使用包含过滤数据的反应式,然后使用 renderPrint 输出其摘要:
在 UI 中添加:
textOutput("summary"),
并更改服务器
AllDataIssue <- reactive({
df <- df_products_upload()
pros <-
sqldf('select * from df where XXXX="any value" AND XX > 2 ')
qual <-
sqldf('select * from df where YYYY="any value" AND XX > 2 ')
AllDataIssue <-
rbind(pros,
qual)
})
output$summary = renderPrint(summary(AllDataIssue()))
output$sample_table <- DT::renderDataTable({
DT::datatable(
{ AllDataIssue() },
caption = htmltools::tags$caption(
style = 'caption-side: bottom; text-align: center;',
'Table 1: ', htmltools::em('Any message.')
),
extensions = 'Buttons',
options = list(
fixedColumns = TRUE,
autoWidth = TRUE,
ordering = TRUE,
dom = 'Bftsp',
filter = 'top',
buttons = c('csv', 'excel')
))
})
注意 AllDataIssue() 后的括号`