我有一个闪亮的应用程序,可以根据用户输入返回数据表。
我希望通过插入换行符来分隔字符串来格式化数据表元素。
例如,如果我向列1输入“事实”,向列2输入“数据”,向行1输入“是”,向行2输入“更多”,则数据表中的输出应如下所示:
我的闪亮应用程序示例如下:
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)
如果运行该应用程序,您将看到表中的字符元素由逗号分隔,如何设置它们的格式,使其在开头有换行符和连字符
答案 0 :(得分:2)
您可以将列转换为字符串,然后在每一行上循环并添加htmltag。 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)