链接选项卡以在R Shiny中按行选择显示绘图

时间:2016-10-08 22:03:04

标签: r tabs shiny dt

Attached sample image

我在标签1数据上显示所选行项目的绘图和原始数据时遇到问题。

我使用observeEvent将所选行作为输入并将焦点切换到选项卡2.在同一个函数中,我还尝试为保存的文件位置生成文本字符串。焦点确实会切换,但我的绘图和原始数据会出错。

我确信我在这里遗漏了一些东西,可能是获取我在observeEvent中生成的文件名文本字符串并将其传递给renderDataTable和反应函数。 数据文件的链接在代码中。我正在分享' maintab.csv' (tab1)和第一行数据文件' Saratoga_Shen.csv' (表2和3)。请帮忙。

## app.R ##
# maintab.csv
# https://drive.google.com/open?id=0B28LLO8YLgDdVlVjc1BUV3JKQVE

# Saratoga_Shen.csv
# https://drive.google.com/open?id=0B28LLO8YLgDdeGh2RFl4RUpSUVE
library(shiny)
library(DT)
library(ggplot2)
library(plotly)

maintab = read.csv("/data/maintab.csv", header = TRUE)

##############################
## UI
##############################

ui <- fluidPage( tabsetPanel(id = "mainPanel",
                         # tab 1 shows data from maintab.csv
                         tabPanel("State County List with funding",dataTableOutput('table')),
                         # tab 2 shows basic box plot of data based on row selection on tab 1
                         tabPanel("County Project Spending by school district",
                                  fluidRow(
                                    box(plotlyOutput("plot1", height = 450)))),
                         # tab 3 is raw data that is used in plot on tab 2
                         tabPanel("Data used for plotting", dataTableOutput("rawdata")),
                         # tab 4 is just the link to confirm right data file is used for plotting and showing raw data
                         tabPanel("Tab2", textOutput("text"))
              ) #end of tabsetPanel
)

##############################
### SERVER
##############################

server <- function(input, output, session) {
options(DT.options = list(pageLength = 15))
maintab = read.csv("/data/maintab.csv", header = TRUE)
colnames(maintab)<- c("State","County","School District")
output$table <- renderDataTable({maintab}, selection = "single", server = FALSE,
                              options = list(paging=FALSE,
                                             searching=FALSE,
                                             filtering=FALSE,
                                             ordering=FALSE))

# Observing which row gets selected, based on that generate a string of file location
# and also switch to tab 2
observeEvent(input$table_rows_selected, {
row <- input$table_rows_selected
output$text <- renderText({
  t1 = paste(maintab[row, "county"], sep = "_", maintab[row, "school_district"])
  t2 = paste(t1, sep = "",".csv")
  t3 = paste("/data/",sep = "",t2)
})
updateTabsetPanel(session, "mainPanel", selected = "County Project Spending by school district")
})

# Pull county project data in filename from row selection for plotting
filename <- reactive({
read.csv(text(),header = TRUE)
})

#Pull data in rawdata for selected row on tab1 to show on tab 3
output$rawdata <- renderDataTable( 
{read.csv(text(),header = TRUE)},
options = list(scrollX = TRUE)
)

####################
# PLOTTING
####################
output$plot1 <- renderPlotly({
p1<-ggplot(data = filename(), aes_string(x=names(filename()[1]),y=names(filename()[3]),color = names(filename()[2]),shape = names(filename()[2])))
p1<- p1 + geom_boxplot() + geom_jitter(position=position_jitter(0.2),size=2)
p1<- ggplotly(p1)
p1
})
}

##############################
### SHINYAPP
##############################
shinyApp(ui, server)

1 个答案:

答案 0 :(得分:2)

See Sample output image

## app.R ##
library(shiny)
library(DT)
library(ggplot2)
library(plotly)

maintab = read.csv("/data/maintab.csv", header = TRUE)

##############################
## UI
##############################

ui <- fluidPage( tabsetPanel(id = "mainPanel",
                     # tab 1 shows data from maintab.csv
                     tabPanel("State County List with funding",dataTableOutput('table')),
                     # tab 2 shows basic box plot of data based on row selection on tab 1
                     tabPanel("County Project Spending by school district",
                              fluidRow(
                                box(plotlyOutput("plot1", height = 450)))),
                     # tab 3 is raw data that is used in plot on tab 2
                     tabPanel("Data used for plotting", dataTableOutput("rawdata")),
                     # tab 4 is just the link to confirm right data file is used for plotting and showing raw data
                     tabPanel("Tab2", textOutput("text"))
          ) #end of tabsetPanel
)

##############################
### SERVER
##############################

server <- function(input, output, session) {
options(DT.options = list(pageLength = 15))
maintab = read.csv("/data/maintab.csv", header = TRUE)
colnames(maintab)<- c("State","County","School District")
output$table <- renderDataTable({maintab}, selection = "single", server = FALSE,
                          options = list(paging=FALSE,
                                         searching=FALSE,
                                         filtering=FALSE,
                                         ordering=FALSE))

# Observing which row gets selected, based on that generate a string of file location
# and also switch to tab 2
observeEvent(input$table_rows_selected, {
row <- input$table_rows_selected
updateTabsetPanel(session, "mainPanel", selected = "County Project Spending by school district")
})

# Removed the lines from ObserveEvent and added under eventReactive to assign to path variable
  path <- eventReactive(input$table_rows_selected, {
    row <- input$table_rows_selected
      t1 = paste(maintab[row, "County"], sep = "_", maintab[row, "School District"])
      t2 = paste(t1, sep = "",".csv")
      t3 = paste("/users/home/tparmar/Rscripts/Shiny/county/data/",sep = "",t2)
  })

# Pull county project data in filename from row selection for plotting
filename <- reactive({
read.csv(path(),header = TRUE)
})

#Pull data in rawdata for selected row on tab1 to show on tab 3
output$rawdata <- renderDataTable( 
{read.csv(path(),header = TRUE)},
options = list(scrollX = TRUE)
)

####################
# PLOTTING
####################
output$plot1 <- renderPlotly({
p1<-ggplot(data = filename(), aes_string(x=names(filename()[1]),y=names(filename()[3]),color = names(filename()[2]),shape = names(filename()[2])))
p1<- p1 + geom_boxplot() + geom_jitter(position=position_jitter(0.2),size=2)
p1<- ggplotly(p1)
p1
})
}

##############################
### SHINYAPP
##############################
shinyApp(ui, server)