r-在一个页面上显示多个数据表
我正在尝试在r-在一个页面上显示多个数据表,r,shiny,record-linkage,R,Shiny,Record Linkage,我正在尝试在R中创建一个应用程序。我想做的第一件事是选择两个csv文件显示在一个页面上。根据这一点:我需要为UI中的每个表添加一个dataTableOutput,并在我的服务器中添加另一个renderDataTable。然而,当我尝试的时候,这个应用程序不起作用 下面的代码只允许用户加载一个csv文件 X <- c("plyr", "dplyr", "tm", "wordcloud", "SnowballC", "stringdist", "tidytext", "rmarkdown
R
中创建一个应用程序。我想做的第一件事是选择两个csv文件显示在一个页面上。根据这一点:我需要为UI
中的每个表添加一个dataTableOutput
,并在我的服务器中添加另一个renderDataTable
。然而,当我尝试的时候,这个应用程序不起作用
下面的代码只允许用户加载一个csv文件
X <- c("plyr", "dplyr", "tm", "wordcloud", "SnowballC", "stringdist", "tidytext",
"rmarkdown", "knitr", "quanteda", "qdap", "reshape", "stringr", "RecordLinkage",
"data.table", "rvest", "shiny", "shinydashboard", "DT")
lapply(X, FUN = function(X){
do.call("library", list(X))
})
X我对您的代码做了一些更改。我没有选择checkboxInput
,而是选择了shinyWidgets
包中的pickerInput
(我将它添加到了您的代码中)。在同一页上显示两个表已经是很多内容了,在上面添加checkboxInputs
将占用更多的空间。pickerInput重量轻、美观且易于使用
它在这里工作,请检查一下,如果有任何问题请告诉我
您可能需要配置如何读入CSV,尤其是sep
参数
请注意,使用fileInput读取多个文件在RStudio窗格或旧浏览器(如IE 9)中不起作用
更新:现在允许使用readxl读取excel文件,并将两个表排列在彼此相邻的列中
X <- c("plyr", "dplyr", "tm", "readxl", "wordcloud", "SnowballC", "stringdist", "tidytext",
"rmarkdown", "knitr", "quanteda", "reshape", "stringr", "RecordLinkage",
"data.table", "rvest", "qdap", "shiny", "shinydashboard", "shinyWidgets", "DT")
lapply(X, FUN = function(X){
do.call("library", list(X))
})
ui <- dashboardPage(
dashboardHeader(title = "Record Linkage App"),
dashboardSidebar(
sidebarMenu(
## Tab 1 -- Specify Task
menuItem("Select Task And Upload Files", tabName = "task", icon = icon("file-text-o")),
## Tab 2 -- View Raw Data Files
menuItem("View Raw Data", tabName = "raw", icon = icon("file-text-o")),
## Tab 3 -- View Processed Data Files
menuItem("View Processed Data", tabName = "processed", icon = icon("file-text-o")),
## Tab 4 -- Select Training Set
menuItem("Select Training Set", tabName = "mltrain", icon = icon("file-text-o")),
## Tab 5 -- View Weight & Probabilities (choose which chart to view or both?)
menuItem("Visualize Distributions", tabName = "distributions", icon = icon("bar-chart-o")),
## Tab 6 -- View Results (review, match and trash files--need to be able to choose dataset)
## Want to be able to add checkboxes to select rows for inclusion in deletion later on
menuItem("View Result Files", tabName = "fileview", icon = icon("file-text-o"))
)), # close dashboard sidebar
#### Dashboard Body starts here
dashboardBody(
tabItems(
### Specify Task & Upload Files Tab
tabItem(tabName = "task",
radioButtons("task", "Select a Task:", c("Frame Deduplication", "Frame Record Linkage")),
fileInput("selection", "Upload Files:", multiple = T,
accept = c(".xlsx", ".xls", "text/csv", "text/comma-separated-values, text/plain", ".csv")),
helpText(paste("Please upload a file. Supported file types are: .txt, .csv and .xls.")),
helpText(paste("Note: Record Linkage requires two data frames."))
), # close first tabItem
tabItem(tabName = "raw",
helpText(paste("This tab displays the raw, unprocessed data frames selected in the previous tab.")),
helpText(paste("Select the columns you wish to display. These columns will be used for string comparisons")),
fluidRow(
column(width = 6,
uiOutput("pick_col1"),
dataTableOutput("content1")
),
column(width = 6,
uiOutput("pick_col2"),
dataTableOutput("content2")
)
)
) # close tabItem
) # close tabItems
) # close dashboardBody
) # closes dashboardpage
options(shiny.maxRequestSize = 100*1024^2)
server <- function(input, output, session) {
data <- reactiveValues(file1 = NULL,
file2 = NULL)
observe({
if (!is.null(input$selection$datapath[1]))
if (grepl(".csv$", input$selection$datapath[1])) {
data$file1 <- read.csv(input$selection$datapath[1], header = TRUE, sep = ";")
} else if (grepl(".xls$|.xlsx$", input$selection$datapath[1])) {
data$file1 <- read_excel(input$selection$datapath[1], col_names = TRUE)
}
})
observe({
if (!is.null(input$selection$datapath[2]))
if (grepl(".csv$", input$selection$datapath[2])) {
data$file2 <- read.csv(input$selection$datapath[2], header = TRUE, sep = ";")
} else if (grepl(".xls$|.xlsx$", input$selection$datapath[2])) {
data$file2 <- read_excel(input$selection$datapath[2], col_names = TRUE)
}
})
output$pick_col1 <- renderUI({
pickerInput(
inputId = "pick_col1",
label = "Select the columns of table 1 you wish to display:",
choices = colnames(data$file1),
selected = colnames(data$file1),
options = list(`actions-box` = TRUE,
`selected-text-format` = paste0("count > ", length(colnames(data$file1)) - 1),
`count-selected-text` = "Alle",
liveSearch = TRUE,
liveSearchPlaceholder = TRUE), # build buttons for collective selection
multiple = TRUE)
})
output$pick_col2 <- renderUI({
pickerInput(
inputId = "pick_col2",
label = "Select the columns of table 2 you wish to display:",
choices = colnames(data$file2),
selected = colnames(data$file2),
options = list(`actions-box` = TRUE,
`selected-text-format` = paste0("count > ", length(colnames(data$file2)) - 1),
`count-selected-text` = "Alle",
liveSearch = TRUE,
liveSearchPlaceholder = TRUE), # build buttons for collective selection
multiple = TRUE)
})
output$content1 <- renderDataTable({
data$file1[, req(input$pick_col1)]
})
output$content2 <- renderDataTable({
data$file2[, req(input$pick_col2)]
})
}
shinyApp(ui, server)
X我对您的代码做了一些更改。我没有选择checkboxInput
,而是选择了shinyWidgets
包中的pickerInput
(我将它添加到了您的代码中)。在同一页上显示两个表已经是很多内容了,在上面添加checkboxInputs
将占用更多的空间。pickerInput重量轻、美观且易于使用
它在这里工作,请检查一下,如果有任何问题请告诉我
您可能需要配置如何读入CSV,尤其是sep
参数
请注意,使用fileInput读取多个文件在RStudio窗格或旧浏览器(如IE 9)中不起作用
更新:现在允许使用readxl读取excel文件,并将两个表排列在彼此相邻的列中
X <- c("plyr", "dplyr", "tm", "readxl", "wordcloud", "SnowballC", "stringdist", "tidytext",
"rmarkdown", "knitr", "quanteda", "reshape", "stringr", "RecordLinkage",
"data.table", "rvest", "qdap", "shiny", "shinydashboard", "shinyWidgets", "DT")
lapply(X, FUN = function(X){
do.call("library", list(X))
})
ui <- dashboardPage(
dashboardHeader(title = "Record Linkage App"),
dashboardSidebar(
sidebarMenu(
## Tab 1 -- Specify Task
menuItem("Select Task And Upload Files", tabName = "task", icon = icon("file-text-o")),
## Tab 2 -- View Raw Data Files
menuItem("View Raw Data", tabName = "raw", icon = icon("file-text-o")),
## Tab 3 -- View Processed Data Files
menuItem("View Processed Data", tabName = "processed", icon = icon("file-text-o")),
## Tab 4 -- Select Training Set
menuItem("Select Training Set", tabName = "mltrain", icon = icon("file-text-o")),
## Tab 5 -- View Weight & Probabilities (choose which chart to view or both?)
menuItem("Visualize Distributions", tabName = "distributions", icon = icon("bar-chart-o")),
## Tab 6 -- View Results (review, match and trash files--need to be able to choose dataset)
## Want to be able to add checkboxes to select rows for inclusion in deletion later on
menuItem("View Result Files", tabName = "fileview", icon = icon("file-text-o"))
)), # close dashboard sidebar
#### Dashboard Body starts here
dashboardBody(
tabItems(
### Specify Task & Upload Files Tab
tabItem(tabName = "task",
radioButtons("task", "Select a Task:", c("Frame Deduplication", "Frame Record Linkage")),
fileInput("selection", "Upload Files:", multiple = T,
accept = c(".xlsx", ".xls", "text/csv", "text/comma-separated-values, text/plain", ".csv")),
helpText(paste("Please upload a file. Supported file types are: .txt, .csv and .xls.")),
helpText(paste("Note: Record Linkage requires two data frames."))
), # close first tabItem
tabItem(tabName = "raw",
helpText(paste("This tab displays the raw, unprocessed data frames selected in the previous tab.")),
helpText(paste("Select the columns you wish to display. These columns will be used for string comparisons")),
fluidRow(
column(width = 6,
uiOutput("pick_col1"),
dataTableOutput("content1")
),
column(width = 6,
uiOutput("pick_col2"),
dataTableOutput("content2")
)
)
) # close tabItem
) # close tabItems
) # close dashboardBody
) # closes dashboardpage
options(shiny.maxRequestSize = 100*1024^2)
server <- function(input, output, session) {
data <- reactiveValues(file1 = NULL,
file2 = NULL)
observe({
if (!is.null(input$selection$datapath[1]))
if (grepl(".csv$", input$selection$datapath[1])) {
data$file1 <- read.csv(input$selection$datapath[1], header = TRUE, sep = ";")
} else if (grepl(".xls$|.xlsx$", input$selection$datapath[1])) {
data$file1 <- read_excel(input$selection$datapath[1], col_names = TRUE)
}
})
observe({
if (!is.null(input$selection$datapath[2]))
if (grepl(".csv$", input$selection$datapath[2])) {
data$file2 <- read.csv(input$selection$datapath[2], header = TRUE, sep = ";")
} else if (grepl(".xls$|.xlsx$", input$selection$datapath[2])) {
data$file2 <- read_excel(input$selection$datapath[2], col_names = TRUE)
}
})
output$pick_col1 <- renderUI({
pickerInput(
inputId = "pick_col1",
label = "Select the columns of table 1 you wish to display:",
choices = colnames(data$file1),
selected = colnames(data$file1),
options = list(`actions-box` = TRUE,
`selected-text-format` = paste0("count > ", length(colnames(data$file1)) - 1),
`count-selected-text` = "Alle",
liveSearch = TRUE,
liveSearchPlaceholder = TRUE), # build buttons for collective selection
multiple = TRUE)
})
output$pick_col2 <- renderUI({
pickerInput(
inputId = "pick_col2",
label = "Select the columns of table 2 you wish to display:",
choices = colnames(data$file2),
selected = colnames(data$file2),
options = list(`actions-box` = TRUE,
`selected-text-format` = paste0("count > ", length(colnames(data$file2)) - 1),
`count-selected-text` = "Alle",
liveSearch = TRUE,
liveSearchPlaceholder = TRUE), # build buttons for collective selection
multiple = TRUE)
})
output$content1 <- renderDataTable({
data$file1[, req(input$pick_col1)]
})
output$content2 <- renderDataTable({
data$file2[, req(input$pick_col2)]
})
}
shinyApp(ui, server)
X(旁注:在不检查返回值的情况下使用“require”
是一种不好的做法:如果它不可用,请做些什么!我建议您要么使用“library”
(如果未安装,它将停止),要么捕获返回值并做些什么。那就是“做些什么”可能是install.packages
,或者可能会在闪亮的界面中返回一条更有意义的错误消息(我建议使用后者)。第三个选项(不太常见)是在特定的软件包不可用的情况下提供备用功能,通常只有在软件包用于代码速度时才有意义。)公平点。我使用“require”是因为不久前“library”不起作用,但我同意使用“library”是更好的做法。(旁注:不检查返回值而使用“require”
是不好的做法:如果它不可用,请做些什么!我建议您要么使用“library”
(如果未安装,将停止)或者捕获返回值并执行某些操作。该“操作”可能是install.packages
,或者在闪亮的界面中返回更有意义的错误消息(我建议使用后者)。第三个选项(不太常见)如果特定的包不可用,则具有备用功能,通常只有在包用于代码速度时才有意义。我使用“require”是因为前一段时间“library”不起作用,但我同意使用“library”是更好的做法。有没有一种方法可以使表彼此相邻?理想情况下,我希望同时查看这两个文件的内容,而将它们堆叠起来会使这一点变得困难。这没有问题。您可以使用fluidRow(列(…),列(…)来执行此操作。稍后我会相应地更新我的答案。谢谢。我还想知道readxl
是否也可以有条件地合并?例如,如果上传的文件是.xls而不是.csv,应用程序怎么会知道?这太神奇了!非常感谢!我一直在努力把两者结合在一起,我对闪亮很陌生。这肯定会帮助我更好地理解功能。只要我有能力,我就会奖励赏金。我一直在尝试将用户定义的函数应用于两个表的选定列,方法是将函数包装在输出$content1
,即函数(数据$file1[,req(输入$pick_col1)
但我每次都会出错。你知道为什么吗?有没有一种方法可以让表彼此相邻?理想情况下,我希望同时查看这两个表的内容,并且将它们堆叠起来会很困难。这没有问题。你可以使用fluidRow(列(…),列(…)来完成此操作。我稍后会相应地更新我的答案。谢谢。我还想知道readxl
是否也可以有条件地合并?例如,如果上载的文件是.xls而不是.csv,应用程序怎么会知道?这太神奇了!非常感谢!我一直在努力将两者结合在一起,我对shiny非常陌生。这将是肯定的请帮助我更好地理解该功能。我会尽快授予奖金。我一直在尝试将用户定义的函数应用于两个表的选定列,方法是将函数包装在输出$content1
,即函数(数据$file1[,req(输入$pick\u col1)
但我每次都会出错。你知道为什么吗?
X <- c("plyr", "dplyr", "tm", "readxl", "wordcloud", "SnowballC", "stringdist", "tidytext",
"rmarkdown", "knitr", "quanteda", "reshape", "stringr", "RecordLinkage",
"data.table", "rvest", "qdap", "shiny", "shinydashboard", "shinyWidgets", "DT")
lapply(X, FUN = function(X){
do.call("library", list(X))
})
ui <- dashboardPage(
dashboardHeader(title = "Record Linkage App"),
dashboardSidebar(
sidebarMenu(
## Tab 1 -- Specify Task
menuItem("Select Task And Upload Files", tabName = "task", icon = icon("file-text-o")),
## Tab 2 -- View Raw Data Files
menuItem("View Raw Data", tabName = "raw", icon = icon("file-text-o")),
## Tab 3 -- View Processed Data Files
menuItem("View Processed Data", tabName = "processed", icon = icon("file-text-o")),
## Tab 4 -- Select Training Set
menuItem("Select Training Set", tabName = "mltrain", icon = icon("file-text-o")),
## Tab 5 -- View Weight & Probabilities (choose which chart to view or both?)
menuItem("Visualize Distributions", tabName = "distributions", icon = icon("bar-chart-o")),
## Tab 6 -- View Results (review, match and trash files--need to be able to choose dataset)
## Want to be able to add checkboxes to select rows for inclusion in deletion later on
menuItem("View Result Files", tabName = "fileview", icon = icon("file-text-o"))
)), # close dashboard sidebar
#### Dashboard Body starts here
dashboardBody(
tabItems(
### Specify Task & Upload Files Tab
tabItem(tabName = "task",
radioButtons("task", "Select a Task:", c("Frame Deduplication", "Frame Record Linkage")),
fileInput("selection", "Upload Files:", multiple = T,
accept = c(".xlsx", ".xls", "text/csv", "text/comma-separated-values, text/plain", ".csv")),
helpText(paste("Please upload a file. Supported file types are: .txt, .csv and .xls.")),
helpText(paste("Note: Record Linkage requires two data frames."))
), # close first tabItem
tabItem(tabName = "raw",
helpText(paste("This tab displays the raw, unprocessed data frames selected in the previous tab.")),
helpText(paste("Select the columns you wish to display. These columns will be used for string comparisons")),
fluidRow(
column(width = 6,
uiOutput("pick_col1"),
dataTableOutput("content1")
),
column(width = 6,
uiOutput("pick_col2"),
dataTableOutput("content2")
)
)
) # close tabItem
) # close tabItems
) # close dashboardBody
) # closes dashboardpage
options(shiny.maxRequestSize = 100*1024^2)
server <- function(input, output, session) {
data <- reactiveValues(file1 = NULL,
file2 = NULL)
observe({
if (!is.null(input$selection$datapath[1]))
if (grepl(".csv$", input$selection$datapath[1])) {
data$file1 <- read.csv(input$selection$datapath[1], header = TRUE, sep = ";")
} else if (grepl(".xls$|.xlsx$", input$selection$datapath[1])) {
data$file1 <- read_excel(input$selection$datapath[1], col_names = TRUE)
}
})
observe({
if (!is.null(input$selection$datapath[2]))
if (grepl(".csv$", input$selection$datapath[2])) {
data$file2 <- read.csv(input$selection$datapath[2], header = TRUE, sep = ";")
} else if (grepl(".xls$|.xlsx$", input$selection$datapath[2])) {
data$file2 <- read_excel(input$selection$datapath[2], col_names = TRUE)
}
})
output$pick_col1 <- renderUI({
pickerInput(
inputId = "pick_col1",
label = "Select the columns of table 1 you wish to display:",
choices = colnames(data$file1),
selected = colnames(data$file1),
options = list(`actions-box` = TRUE,
`selected-text-format` = paste0("count > ", length(colnames(data$file1)) - 1),
`count-selected-text` = "Alle",
liveSearch = TRUE,
liveSearchPlaceholder = TRUE), # build buttons for collective selection
multiple = TRUE)
})
output$pick_col2 <- renderUI({
pickerInput(
inputId = "pick_col2",
label = "Select the columns of table 2 you wish to display:",
choices = colnames(data$file2),
selected = colnames(data$file2),
options = list(`actions-box` = TRUE,
`selected-text-format` = paste0("count > ", length(colnames(data$file2)) - 1),
`count-selected-text` = "Alle",
liveSearch = TRUE,
liveSearchPlaceholder = TRUE), # build buttons for collective selection
multiple = TRUE)
})
output$content1 <- renderDataTable({
data$file1[, req(input$pick_col1)]
})
output$content2 <- renderDataTable({
data$file2[, req(input$pick_col2)]
})
}
shinyApp(ui, server)