我做什么: 我有一个闪亮的应用程序,它返回我的csv的每一列作为逐字输出。我附上了我当前的代码(UI.R和Server.R)和下面的csv-File。
我的问题:我需要为许多不同的csv文件编写这样的应用程序,这些文件都有不同数量的列。如何自动执行此操作而无需编写
output$myColumn01 = renderPrint({
as.character(D$Names)
})
和
h1("Names"),
verbatimTextOutput("myColumn01"),
手动每列?
-
这是我的csv(“myCSV.csv”):
Names;Pages;Scores;Numbers
George;T;3;5
Jim;I;4;23
Jack;T;6;12
Anna;R;4;3
这是我的服务器.R-File:
library(shiny)
library(dplyr)
library(shinydashboard)
server <- shinyServer(function(input, output, session) {
D = read.csv(file = "myCSV.csv", sep = ";")
output$myColumn01 = renderPrint({
as.character(D$Names)
})
output$myColumn02 = renderPrint({
as.character(D$Pages)
})
output$myColumn03 = renderPrint({
as.character(D$Scores)
})
output$myColumn04 = renderPrint({
as.character(D$Numbers)
})
})
这是我的ui.R-File:
library(shiny)
library(dplyr)
library(shinydashboard)
ui <- shinyUI(dashboardPage(
dashboardHeader(title = "Sessions"),
dashboardSidebar(
width = 350,
collapsed = TRUE,
""
),
dashboardBody(
h1("Names"),
verbatimTextOutput("myColumn01"),
h1("Pages"),
verbatimTextOutput("myColumn02"),
h1("Scores"),
verbatimTextOutput("myColumn03"),
h1("Numbers"),
verbatimTextOutput("myColumn04")
)
))
答案 0 :(得分:3)
这是你期望的吗?
library(shiny)
library(dplyr)
library(shinydashboard)
server <- shinyServer(function(input, output, session) {
D = read.csv(file = "myCSV.csv", sep = ";")
lapply(1:ncol(D), function(i){
output[[sprintf("myColumn%02d",i)]] <-
renderPrint({
as.character(D[[colnames(D)[i]]])
})
})
output$ui <- renderUI({
lapply(1:ncol(D), function(i){
tagList(
h1(colnames(D)[i]),
verbatimTextOutput(sprintf("myColumn%02d",i))
)
})
})
})
ui <- shinyUI(dashboardPage(
dashboardHeader(title = "Sessions"),
dashboardSidebar(
width = 350,
collapsed = TRUE,
""
),
dashboardBody(
uiOutput("ui")
)
))
shinyApp(ui=ui, server=server)