如何在 R shinydashboard 中获取列表
how to get the list in R shinydashboard
我得到的所有文本都像一个段落。我想要列表格式的所有文本,例如 - html 中的 li。请帮助我。我尝试使用矢量但无法做到。这就是我使用 paste0 方法使用 sep="\n" 附加每个文本但 \n 没有显示新行的原因。
我的 ui.R 文件是
# shinydashboard makes it easy to use Shiny to create dashboards
# shinydashboard requires Shiny 0.11 or above
#First Selecting the shiny Dashboard
library(shiny)
library(shinydashboard)
library(openxlsx)
FileNames <- list.files("ExcelSheets/")
countDays <- length(FileNames)
positive = 0
neutral = 0
negative = 0
count = 0
positiveTweets = ""
negativeTweets = ""
neutralTweets = ""
p = 1
nu = 1
ng = 1
for (i in seq(1, length(FileNames)))
{
excelSheetData = read.xlsx(paste0("ExcelSheets/", FileNames[i]), startRow = 0, colNames = TRUE, detectDates = TRUE)
countRows <- dim(excelSheetData)
countRows <- countRows[1]
rows <- countRows
count = count + rows
data = excelSheetData[, c("polarity", "polarity_confidence", "Text")]
for (j in seq(1, rows)){
if(data[j, 1] == "positive")
{
positive = positive + data[j, 2]
positiveTweets = paste0(positiveTweets, paste0(paste(paste0(p, ":"), data[j,3]), "\n"))
p = p + 1
}
else if(data[j, 1] == "negative")
{
negative = negative + data[j, 2]
negativeTweets = paste0(negativeTweets, paste0(paste(paste0(ng, ":"), data[j,3]), "\n"))
ng = ng + 1
}
else
{
neutral = neutral + data[j, 2]
neutralTweets = paste0(neutralTweets, paste0(paste(paste0(nu, ":"), data[j,3]), "\n"))
nu = nu + 1
}
}
}
total <- positive + negative + neutral
positivePercent <- round((positive * 100) / total)
negativePercent <- round((negative * 100) / total)
neutralPercent <- round((neutral * 100) / total)
countVect = c(positive, neutral, negative)
shinyUI(dashboardPage(
dashboardHeader(title = "Sentiment Analysis"),
dashboardSidebar(
sidebarMenu(
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Tweets", icon = icon("twitter"),
menuSubItem("Positive Tweets", tabName = "pTweets", icon = icon("thumbs-up")),
menuSubItem("Neutral Tweets", tabName = "neuTweets", icon = icon("hand-spock-o")),
menuSubItem("Negative Tweets", tabName = "negTweets", icon = icon("thumbs-down"))
)
)
),
## Body content
dashboardBody(
tabItems(
# First tab content
tabItem(tabName = "dashboard",
div(class = "my-class", h2("Sentiment Analysis of Twitter Tweets using RapidMinor and Shiny Dashboard.")),
fluidRow(
valueBox(count, "Total Number of Tweets Analyzed in the competition", icon = icon("twitter"), width = 6),
valueBox(countDays, "Number of Days ", icon = icon("calendar-check-o"), width = 6, color = "yellow")
),
fluidRow(
infoBox("Positive", paste(positivePercent, "%"), icon = icon("thumbs-up"), width = 4, fill = TRUE, color = "green"),
infoBox("Neutral", paste(neutralPercent, "%"), icon = icon("hand-spock-o"), width = 4, fill = TRUE, color = "light-blue"),
infoBox("Negative", paste(negativePercent, "%"), icon = icon("thumbs-down"), width = 4, fill = TRUE, color = "red")
)
),
# Positive Tweets tab content
tabItem(tabName = "pTweets",
h2("Positive Tweets #Brexit"),
h4(positiveTweets)
),
# Neutral Tweets tab content
tabItem(tabName = "neuTweets",
h2("Neutral Tweets #Brexit"),
h4(neutralTweets)
),
# Negative Tweets tab content
tabItem(tabName = "negTweets",
h2("Negative Tweets #Brexit"),
h4(negativeTweets)
)
)
)
))
我的 server.R 文件是
# This is the server logic for a Shiny web application.
# You can find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com
#
library(shiny)
library(shinydashboard)
shinyServer(function(input, output) {
})
您的问题的一个可能的解决方案是附加带有 html 标签的矢量 <br>
而不是 "\n"
(这将适用于 cat
和 verbatimTextOutput
) 然后将 positiveTweets
包装成 HTML
函数,如下所示:
h4(HTML(positiveTweets))
您还想显示带有当前工作目录中文件名的新选项卡。
在下面的示例中,我创建了一个新的 menuItem
,其中包含具有随机名称的随机数量的选项卡。
首先,在 dashboardHeader
中,我添加了 ID 为 out1
的动态输出。
menuItemOutput("out1")
之后,在服务器端,出于测试目的,我定义了一个变量 my_files
,其中包含随机数量的具有随机名称的选项卡。每次您 运行 应用程序时,它都会更新。
最后,在 renderUI
中,我定义了 menuItem
("Files") 并在其中放置了一个 menuSubItem
的动态数字,它们由 [=27= 生成].
output$out1 <- renderUI({ ... })
我还添加了一条评论,试图解释如果您想在应用程序 运行宁.
完整示例:
library(shiny)
library(shinydashboard)
#library(openxlsx)
rm(ui)
rm(server)
ui <- shinyUI(dashboardPage(
dashboardHeader(title = "Sentiment Analysis"),
dashboardSidebar(
sidebarMenu(
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Tweets", icon = icon("twitter"),
menuSubItem("Positive Tweets", tabName = "pTweets", icon = icon("thumbs-up")),
menuSubItem("Neutral Tweets", tabName = "neuTweets", icon = icon("hand-spock-o")),
menuSubItem("Negative Tweets", tabName = "negTweets", icon = icon("thumbs-down"))
),
menuItemOutput("out1") # added
)
),
## Body content
dashboardBody(
tabItems(
# First tab content
tabItem(tabName = "dashboard",
div(class = "my-class", h2("Sentiment Analysis of Twitter Tweets using RapidMinor and Shiny Dashboard.")),
fluidRow(
#valueBox(count, "Total Number of Tweets Analyzed in the competition", icon = icon("twitter"), width = 6),
valueBox(15, "Total Number of Tweets Analyzed in the competition", icon = icon("twitter"), width = 6),
#valueBox(countDays, "Number of Days ", icon = icon("calendar-check-o"), width = 6, color = "yellow")
valueBox(10, "Number of Days ", icon = icon("calendar-check-o"), width = 6, color = "yellow")
),
fluidRow(
#infoBox("Positive", paste(positivePercent, "%"), icon = icon("thumbs-up"), width = 4, fill = TRUE, color = "green"),
infoBox("Positive", "80%", icon = icon("thumbs-up"), width = 4, fill = TRUE, color = "green"),
infoBox("Neutral", "15%", icon = icon("hand-spock-o"), width = 4, fill = TRUE, color = "light-blue"),
infoBox("Negative", "5%", icon = icon("thumbs-down"), width = 4, fill = TRUE, color = "red")
)
),
# Positive Tweets tab content
tabItem(tabName = "pTweets",
h2("Positive Tweets #Brexit"),
#h4(positiveTweets)
h4("Great")
),
# Neutral Tweets tab content
tabItem(tabName = "neuTweets",
h2("Neutral Tweets #Brexit"),
#h4(neutralTweets)
h4("ok")
),
# Negative Tweets tab content
tabItem(tabName = "negTweets",
h2("Negative Tweets #Brexit"),
#h4(negativeTweets)
h4("shit :D")
)
)
)
))
server <- function(input, output) {
#my_files will be updated each time you run the app
#my_files <- list.files()
# for testing purposes generate 5 tabs with names given by random letters
my_files <- letters[sample(1:26, 5)]
# There could also be the case when there is no files in a folder
# You can handle it with `req` or `validate(need(...))` functions
#my_files <- ""
output$out1 <- renderUI({
# Just in case if you would put new files to the folder
# while the app is working and wanted an update of tabs:
# - create eventReactive with an actionButton which will
# return list.files().
# - pass new names of files to this renderUi function.
# be careful because "tabName" must not have a "." in it.
req(my_files) # show tabs only if there are files in a directory
# generate and save tabs in a list
tabs <- lapply(seq_along(my_files), function(i) {
menuSubItem(my_files[i], tabName = my_files[i], icon = icon("thumbs-up"))
})
menuItem("Files", tabName = "Files", icon = NULL, tabs)
})
}
shinyApp(ui, server)
我得到的所有文本都像一个段落。我想要列表格式的所有文本,例如 - html 中的 li。请帮助我。我尝试使用矢量但无法做到。这就是我使用 paste0 方法使用 sep="\n" 附加每个文本但 \n 没有显示新行的原因。
我的 ui.R 文件是
# shinydashboard makes it easy to use Shiny to create dashboards
# shinydashboard requires Shiny 0.11 or above
#First Selecting the shiny Dashboard
library(shiny)
library(shinydashboard)
library(openxlsx)
FileNames <- list.files("ExcelSheets/")
countDays <- length(FileNames)
positive = 0
neutral = 0
negative = 0
count = 0
positiveTweets = ""
negativeTweets = ""
neutralTweets = ""
p = 1
nu = 1
ng = 1
for (i in seq(1, length(FileNames)))
{
excelSheetData = read.xlsx(paste0("ExcelSheets/", FileNames[i]), startRow = 0, colNames = TRUE, detectDates = TRUE)
countRows <- dim(excelSheetData)
countRows <- countRows[1]
rows <- countRows
count = count + rows
data = excelSheetData[, c("polarity", "polarity_confidence", "Text")]
for (j in seq(1, rows)){
if(data[j, 1] == "positive")
{
positive = positive + data[j, 2]
positiveTweets = paste0(positiveTweets, paste0(paste(paste0(p, ":"), data[j,3]), "\n"))
p = p + 1
}
else if(data[j, 1] == "negative")
{
negative = negative + data[j, 2]
negativeTweets = paste0(negativeTweets, paste0(paste(paste0(ng, ":"), data[j,3]), "\n"))
ng = ng + 1
}
else
{
neutral = neutral + data[j, 2]
neutralTweets = paste0(neutralTweets, paste0(paste(paste0(nu, ":"), data[j,3]), "\n"))
nu = nu + 1
}
}
}
total <- positive + negative + neutral
positivePercent <- round((positive * 100) / total)
negativePercent <- round((negative * 100) / total)
neutralPercent <- round((neutral * 100) / total)
countVect = c(positive, neutral, negative)
shinyUI(dashboardPage(
dashboardHeader(title = "Sentiment Analysis"),
dashboardSidebar(
sidebarMenu(
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Tweets", icon = icon("twitter"),
menuSubItem("Positive Tweets", tabName = "pTweets", icon = icon("thumbs-up")),
menuSubItem("Neutral Tweets", tabName = "neuTweets", icon = icon("hand-spock-o")),
menuSubItem("Negative Tweets", tabName = "negTweets", icon = icon("thumbs-down"))
)
)
),
## Body content
dashboardBody(
tabItems(
# First tab content
tabItem(tabName = "dashboard",
div(class = "my-class", h2("Sentiment Analysis of Twitter Tweets using RapidMinor and Shiny Dashboard.")),
fluidRow(
valueBox(count, "Total Number of Tweets Analyzed in the competition", icon = icon("twitter"), width = 6),
valueBox(countDays, "Number of Days ", icon = icon("calendar-check-o"), width = 6, color = "yellow")
),
fluidRow(
infoBox("Positive", paste(positivePercent, "%"), icon = icon("thumbs-up"), width = 4, fill = TRUE, color = "green"),
infoBox("Neutral", paste(neutralPercent, "%"), icon = icon("hand-spock-o"), width = 4, fill = TRUE, color = "light-blue"),
infoBox("Negative", paste(negativePercent, "%"), icon = icon("thumbs-down"), width = 4, fill = TRUE, color = "red")
)
),
# Positive Tweets tab content
tabItem(tabName = "pTweets",
h2("Positive Tweets #Brexit"),
h4(positiveTweets)
),
# Neutral Tweets tab content
tabItem(tabName = "neuTweets",
h2("Neutral Tweets #Brexit"),
h4(neutralTweets)
),
# Negative Tweets tab content
tabItem(tabName = "negTweets",
h2("Negative Tweets #Brexit"),
h4(negativeTweets)
)
)
)
))
我的 server.R 文件是
# This is the server logic for a Shiny web application.
# You can find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com
#
library(shiny)
library(shinydashboard)
shinyServer(function(input, output) {
})
您的问题的一个可能的解决方案是附加带有 html 标签的矢量 <br>
而不是 "\n"
(这将适用于 cat
和 verbatimTextOutput
) 然后将 positiveTweets
包装成 HTML
函数,如下所示:
h4(HTML(positiveTweets))
您还想显示带有当前工作目录中文件名的新选项卡。
在下面的示例中,我创建了一个新的 menuItem
,其中包含具有随机名称的随机数量的选项卡。
首先,在 dashboardHeader
中,我添加了 ID 为 out1
的动态输出。
menuItemOutput("out1")
之后,在服务器端,出于测试目的,我定义了一个变量 my_files
,其中包含随机数量的具有随机名称的选项卡。每次您 运行 应用程序时,它都会更新。
最后,在 renderUI
中,我定义了 menuItem
("Files") 并在其中放置了一个 menuSubItem
的动态数字,它们由 [=27= 生成].
output$out1 <- renderUI({ ... })
我还添加了一条评论,试图解释如果您想在应用程序 运行宁.
完整示例:
library(shiny)
library(shinydashboard)
#library(openxlsx)
rm(ui)
rm(server)
ui <- shinyUI(dashboardPage(
dashboardHeader(title = "Sentiment Analysis"),
dashboardSidebar(
sidebarMenu(
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Tweets", icon = icon("twitter"),
menuSubItem("Positive Tweets", tabName = "pTweets", icon = icon("thumbs-up")),
menuSubItem("Neutral Tweets", tabName = "neuTweets", icon = icon("hand-spock-o")),
menuSubItem("Negative Tweets", tabName = "negTweets", icon = icon("thumbs-down"))
),
menuItemOutput("out1") # added
)
),
## Body content
dashboardBody(
tabItems(
# First tab content
tabItem(tabName = "dashboard",
div(class = "my-class", h2("Sentiment Analysis of Twitter Tweets using RapidMinor and Shiny Dashboard.")),
fluidRow(
#valueBox(count, "Total Number of Tweets Analyzed in the competition", icon = icon("twitter"), width = 6),
valueBox(15, "Total Number of Tweets Analyzed in the competition", icon = icon("twitter"), width = 6),
#valueBox(countDays, "Number of Days ", icon = icon("calendar-check-o"), width = 6, color = "yellow")
valueBox(10, "Number of Days ", icon = icon("calendar-check-o"), width = 6, color = "yellow")
),
fluidRow(
#infoBox("Positive", paste(positivePercent, "%"), icon = icon("thumbs-up"), width = 4, fill = TRUE, color = "green"),
infoBox("Positive", "80%", icon = icon("thumbs-up"), width = 4, fill = TRUE, color = "green"),
infoBox("Neutral", "15%", icon = icon("hand-spock-o"), width = 4, fill = TRUE, color = "light-blue"),
infoBox("Negative", "5%", icon = icon("thumbs-down"), width = 4, fill = TRUE, color = "red")
)
),
# Positive Tweets tab content
tabItem(tabName = "pTweets",
h2("Positive Tweets #Brexit"),
#h4(positiveTweets)
h4("Great")
),
# Neutral Tweets tab content
tabItem(tabName = "neuTweets",
h2("Neutral Tweets #Brexit"),
#h4(neutralTweets)
h4("ok")
),
# Negative Tweets tab content
tabItem(tabName = "negTweets",
h2("Negative Tweets #Brexit"),
#h4(negativeTweets)
h4("shit :D")
)
)
)
))
server <- function(input, output) {
#my_files will be updated each time you run the app
#my_files <- list.files()
# for testing purposes generate 5 tabs with names given by random letters
my_files <- letters[sample(1:26, 5)]
# There could also be the case when there is no files in a folder
# You can handle it with `req` or `validate(need(...))` functions
#my_files <- ""
output$out1 <- renderUI({
# Just in case if you would put new files to the folder
# while the app is working and wanted an update of tabs:
# - create eventReactive with an actionButton which will
# return list.files().
# - pass new names of files to this renderUi function.
# be careful because "tabName" must not have a "." in it.
req(my_files) # show tabs only if there are files in a directory
# generate and save tabs in a list
tabs <- lapply(seq_along(my_files), function(i) {
menuSubItem(my_files[i], tabName = my_files[i], icon = icon("thumbs-up"))
})
menuItem("Files", tabName = "Files", icon = NULL, tabs)
})
}
shinyApp(ui, server)