r - 如何在 Shiny 应用程序中正确格式化数据集?
问题描述
到目前为止,我已经能够得到它,以便用户可以将数据上传到闪亮的应用程序(动态上传)。我怎样才能得到它,以便上传的数据框格式正确(见下面的链接)。我相信我想使用的库是 DT,但我对其他选择持开放态度。代码如下
## Only run examples in interactive R sessions
if (interactive()) {
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE)
),
mainPanel(
tableOutput("contents")
)
)
)
server <- function(input, output) {
output$contents <- renderTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header = input$header)
})
}
shinyApp(ui, server)
}
解决方案
您可以使用renderDataTable
和dataTableOutput
。
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE)
),
mainPanel(
dataTableOutput("contents")
)
)
)
server <- function(input, output) {
output$contents <- renderDataTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header = input$header)
}, options = list(pageLength = 5))
}
shinyApp(ui, server)
您也可以尝试DT
具有同名功能的包。与DT::renderDataTable
和一起使用DT::dataTableOutput
。
推荐阅读
- python - 删除熊猫中的一系列字符
- python-3.x - 连接到 PySpark 会话时出现以下错误
- reactjs - React Apollo 3 graphQL 理解重新渲染的机制
- django - django 应用程序的 Heroku 部署后数据库错误
- css - SASS 动态字符串变量
- python - 有什么方法可以在不改变图像纵横比的情况下使用 python pptx 包在 pptx 中拟合图像
- python - 连接到远程计算机上的 Jupyter Notebook 时出现问题
- javascript - 尝试使用 Escape 键将输入的值设置为原始值
- powershell - 我的 powershell 脚本没有停止我正在打开的进程
- flutter - 在列中插入两个 TextField