3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

メモ: RStudio Shiny でファイルごとにタブを作る

Last updated at Posted at 2013-03-25

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/

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?