data
フォルダに処理したいファイルを置きます。
今回は各ファイルに対して次のようなデータが入っており、これらを棒グラフにして別タブで出力したいとします。
Name | Count |
---|---|
hoge | 10 |
fuga | 15 |
piyo | 12 |
... | ... |
ui.R
library(shiny)
files <- list.files("data")
shinyUI(pageWithSidebar(
headerPanel(title="Tab by File Test"),
sidebarPanel(
sliderInput(
inputId = "obs",
label = "Number of observations: ",
min = 1,
max = 100,
value = 50
)
),
mainPanel(
do.call(tabsetPanel,
lapply(files, function(filename) {
panelName <- substring(filename, 1, nchar(filename) - 4)
tabPanel(panelName, plotOutput(filename))
})
)
)
))
output に for
は使えません。sapply
なら使えます。
server.R
library(shiny)
files <- list.files("data")
shinyServer(function(input, output) {
sapply(files, function(filename) {
output[[filename]] <- renderPlot( {
filepath <- file.path("data", filename)
n <- input$obs
data <- read.table(filepath, header=TRUE, sep="\t")
values <- data[, 2]
names(values) <- data[, 1]
barplot(values[1:n], main=filename)
})
})
})
実際に作ってみたものがこれです → http://glimmer.rstudio.com/hoxom/TabByFileTest/