在数据表 R 中拆分字符元素
Split character elements in datatables R
我有一个闪亮的应用程序,它 returns 一个数据table 取决于用户输入。
我希望通过插入换行符来分隔字符串来格式化数据table 元素。
例如,如果我输入 "fact" 到第 1 列,"data" 到第 2 列,"are" 到第 1 行,"more" 到第 2 行,数据 table 应该如下所示:
我闪亮的应用程序示例如下:
library(shiny)
library(shinydashboard)
library(statquotes)
library(sqldf)
library(DT)
data(quotes)
quotes
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu( )),
dashboardBody(
tabItem(tabName = "Tabs",
fluidRow(
column(width=3,
box(
title="Search ",
solidHeader=TRUE,
collapsible=TRUE,
width=NULL,
textInput("column1", " Col 1 ", '', placeholder = "Type keyword/statement"),
textInput("column2", " Col 2 ", '', placeholder = "Type keyword/statement"),
textInput("row1", " Row 1 ", '', placeholder = "Type keyword/statement"),
textInput("row2", " Row 2 ", '', placeholder = "Type keyword/statement"),
submitButton("Search")
)
),
column( width=9,
tabBox(
width="100%",
tabPanel("tab1",
DT::dataTableOutput("matrix")
)))))
))
server <- function(input, output) {
output$matrix <- DT::renderDataTable({
if (input$column1 != "") {
col1row1 <- reactive({ sqldf(paste0("SELECT topic
FROM quotes
WHERE (text LIKE '%",input$column1,"%'
AND text LIKE '%",input$row1,"%'
)"))
})
col1row2 <- reactive({ sqldf(paste0("SELECT topic
FROM quotes
WHERE (text LIKE '%",input$column1,"%'
AND text LIKE '%",input$row2,"%'
)"))
})
col2row1 <- reactive({ sqldf(paste0("SELECT topic
FROM quotes
WHERE (text LIKE '%",input$column2,"%'
AND text LIKE '%",input$row1,"%'
)"))
})
col2row2 <- reactive({ sqldf(paste0("SELECT topic
FROM quotes
WHERE (text LIKE '%",input$column2,"%'
AND text LIKE '%",input$row2,"%'
)"))
})
tabledata <- reactive({ matrix(c(col1row1 (), col1row2 (), col2row1 (),
col2row2 ()), ncol = 2) })
tabledata <- tabledata ()
colnames(tabledata) <- c(input$column1, input$column2)
row.names(tabledata) <- c (input$row1, input$row2)
tabledata
}
},
rownames = TRUE ,
filter = "top", server = FALSE,
extensions = c("Buttons"),
options = list(
scrollY = 400,
scrollX = TRUE,
scroller = TRUE,
dom = 'Bfrtip',
buttons = c('copy', 'excel', 'pdf', 'print')
))
}
shinyApp(ui, server)
如果您 运行 应用程序,您会看到 table 中的字符元素由逗号分隔,如何设置它们的格式以便它们在开头有换行符和连字符
您可以将列转换为字符串,然后遍历每一行并添加 html 标签。 mutate
也可以完成这项工作。最后,您必须传递 escape = FALSE
才能使 HTML 标签起作用。
library(shiny)
library(shinydashboard)
library(statquotes)
library(sqldf)
library(DT)
data(quotes)
quotes
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu( )),
dashboardBody(
tabItem(tabName = "Tabs",
fluidRow(
column(width=3,
box(
title="Search ",
solidHeader=TRUE,
collapsible=TRUE,
width=NULL,
textInput("column1", " Col 1 ", '', placeholder = "Type keyword/statement"),
textInput("column2", " Col 2 ", '', placeholder = "Type keyword/statement"),
textInput("row1", " Row 1 ", '', placeholder = "Type keyword/statement"),
textInput("row2", " Row 2 ", '', placeholder = "Type keyword/statement"),
submitButton("Search")
)
),
column( width=9,
tabBox(
width="100%",
tabPanel("tab1",
DT::dataTableOutput("matrix")
)))))
))
server <- function(input, output) {
output$matrix <- DT::renderDataTable({
if (input$column1 != "") {
col1row1 <- reactive({
resultstring <- ""
df1 <- sqldf(paste0("SELECT topic
FROM quotes
WHERE (text LIKE '%",input$column1,"%'
AND text LIKE '%",input$row1,"%'
)"))
for(i in 1:nrow(df1)) {
resultstring <- paste0(resultstring, "<br>-", df1$topic[i])
}
return(resultstring)
})
col1row2 <- reactive({
resultstring <- ""
df1 <-sqldf(paste0("SELECT topic
FROM quotes
WHERE (text LIKE '%",input$column1,"%'
AND text LIKE '%",input$row2,"%'
)"))
for(i in 1:nrow(df1)) {
resultstring <- paste0(resultstring, "<br>-", df1$topic[i])
}
return(resultstring)
})
col2row1 <- reactive({
resultstring <- ""
df1 <- sqldf(paste0("SELECT topic
FROM quotes
WHERE (text LIKE '%",input$column2,"%'
AND text LIKE '%",input$row1,"%'
)"))
for(i in 1:nrow(df1)) {
resultstring <- paste0(resultstring, "<br>-", df1$topic[i])
}
return(resultstring)
})
col2row2 <- reactive({
resultstring <- ""
df1 <- sqldf(paste0("SELECT topic
FROM quotes
WHERE (text LIKE '%",input$column2,"%'
AND text LIKE '%",input$row2,"%'
)"))
for(i in 1:nrow(df1)) {
resultstring <- paste0(resultstring, "<br>-", df1$topic[i])
}
return(resultstring)
})
tabledata <- reactive({ matrix(c(col1row1 (), col1row2 (), col2row1 (),
col2row2 ()), ncol = 2) })
tabledata <- tabledata ()
colnames(tabledata) <- c(input$column1, input$column2)
row.names(tabledata) <- c (input$row1, input$row2)
tabledata
}
},
rownames = TRUE ,
filter = "top", server = FALSE,
extensions = c("Buttons"),
options = list(
scrollY = 400,
scrollX = TRUE,
scroller = TRUE,
dom = 'Bfrtip',
buttons = c('copy', 'excel', 'pdf', 'print')
),
escape = FALSE)
}
shinyApp(ui, server)
我有一个闪亮的应用程序,它 returns 一个数据table 取决于用户输入。
我希望通过插入换行符来分隔字符串来格式化数据table 元素。
例如,如果我输入 "fact" 到第 1 列,"data" 到第 2 列,"are" 到第 1 行,"more" 到第 2 行,数据 table 应该如下所示:
我闪亮的应用程序示例如下:
library(shiny)
library(shinydashboard)
library(statquotes)
library(sqldf)
library(DT)
data(quotes)
quotes
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu( )),
dashboardBody(
tabItem(tabName = "Tabs",
fluidRow(
column(width=3,
box(
title="Search ",
solidHeader=TRUE,
collapsible=TRUE,
width=NULL,
textInput("column1", " Col 1 ", '', placeholder = "Type keyword/statement"),
textInput("column2", " Col 2 ", '', placeholder = "Type keyword/statement"),
textInput("row1", " Row 1 ", '', placeholder = "Type keyword/statement"),
textInput("row2", " Row 2 ", '', placeholder = "Type keyword/statement"),
submitButton("Search")
)
),
column( width=9,
tabBox(
width="100%",
tabPanel("tab1",
DT::dataTableOutput("matrix")
)))))
))
server <- function(input, output) {
output$matrix <- DT::renderDataTable({
if (input$column1 != "") {
col1row1 <- reactive({ sqldf(paste0("SELECT topic
FROM quotes
WHERE (text LIKE '%",input$column1,"%'
AND text LIKE '%",input$row1,"%'
)"))
})
col1row2 <- reactive({ sqldf(paste0("SELECT topic
FROM quotes
WHERE (text LIKE '%",input$column1,"%'
AND text LIKE '%",input$row2,"%'
)"))
})
col2row1 <- reactive({ sqldf(paste0("SELECT topic
FROM quotes
WHERE (text LIKE '%",input$column2,"%'
AND text LIKE '%",input$row1,"%'
)"))
})
col2row2 <- reactive({ sqldf(paste0("SELECT topic
FROM quotes
WHERE (text LIKE '%",input$column2,"%'
AND text LIKE '%",input$row2,"%'
)"))
})
tabledata <- reactive({ matrix(c(col1row1 (), col1row2 (), col2row1 (),
col2row2 ()), ncol = 2) })
tabledata <- tabledata ()
colnames(tabledata) <- c(input$column1, input$column2)
row.names(tabledata) <- c (input$row1, input$row2)
tabledata
}
},
rownames = TRUE ,
filter = "top", server = FALSE,
extensions = c("Buttons"),
options = list(
scrollY = 400,
scrollX = TRUE,
scroller = TRUE,
dom = 'Bfrtip',
buttons = c('copy', 'excel', 'pdf', 'print')
))
}
shinyApp(ui, server)
如果您 运行 应用程序,您会看到 table 中的字符元素由逗号分隔,如何设置它们的格式以便它们在开头有换行符和连字符
您可以将列转换为字符串,然后遍历每一行并添加 html 标签。 mutate
也可以完成这项工作。最后,您必须传递 escape = FALSE
才能使 HTML 标签起作用。
library(shiny)
library(shinydashboard)
library(statquotes)
library(sqldf)
library(DT)
data(quotes)
quotes
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu( )),
dashboardBody(
tabItem(tabName = "Tabs",
fluidRow(
column(width=3,
box(
title="Search ",
solidHeader=TRUE,
collapsible=TRUE,
width=NULL,
textInput("column1", " Col 1 ", '', placeholder = "Type keyword/statement"),
textInput("column2", " Col 2 ", '', placeholder = "Type keyword/statement"),
textInput("row1", " Row 1 ", '', placeholder = "Type keyword/statement"),
textInput("row2", " Row 2 ", '', placeholder = "Type keyword/statement"),
submitButton("Search")
)
),
column( width=9,
tabBox(
width="100%",
tabPanel("tab1",
DT::dataTableOutput("matrix")
)))))
))
server <- function(input, output) {
output$matrix <- DT::renderDataTable({
if (input$column1 != "") {
col1row1 <- reactive({
resultstring <- ""
df1 <- sqldf(paste0("SELECT topic
FROM quotes
WHERE (text LIKE '%",input$column1,"%'
AND text LIKE '%",input$row1,"%'
)"))
for(i in 1:nrow(df1)) {
resultstring <- paste0(resultstring, "<br>-", df1$topic[i])
}
return(resultstring)
})
col1row2 <- reactive({
resultstring <- ""
df1 <-sqldf(paste0("SELECT topic
FROM quotes
WHERE (text LIKE '%",input$column1,"%'
AND text LIKE '%",input$row2,"%'
)"))
for(i in 1:nrow(df1)) {
resultstring <- paste0(resultstring, "<br>-", df1$topic[i])
}
return(resultstring)
})
col2row1 <- reactive({
resultstring <- ""
df1 <- sqldf(paste0("SELECT topic
FROM quotes
WHERE (text LIKE '%",input$column2,"%'
AND text LIKE '%",input$row1,"%'
)"))
for(i in 1:nrow(df1)) {
resultstring <- paste0(resultstring, "<br>-", df1$topic[i])
}
return(resultstring)
})
col2row2 <- reactive({
resultstring <- ""
df1 <- sqldf(paste0("SELECT topic
FROM quotes
WHERE (text LIKE '%",input$column2,"%'
AND text LIKE '%",input$row2,"%'
)"))
for(i in 1:nrow(df1)) {
resultstring <- paste0(resultstring, "<br>-", df1$topic[i])
}
return(resultstring)
})
tabledata <- reactive({ matrix(c(col1row1 (), col1row2 (), col2row1 (),
col2row2 ()), ncol = 2) })
tabledata <- tabledata ()
colnames(tabledata) <- c(input$column1, input$column2)
row.names(tabledata) <- c (input$row1, input$row2)
tabledata
}
},
rownames = TRUE ,
filter = "top", server = FALSE,
extensions = c("Buttons"),
options = list(
scrollY = 400,
scrollX = TRUE,
scroller = TRUE,
dom = 'Bfrtip',
buttons = c('copy', 'excel', 'pdf', 'print')
),
escape = FALSE)
}
shinyApp(ui, server)