【问题标题】:Linking tab to display plot by row selection in R Shiny链接选项卡以在 R Shiny 中按行选择显示绘图
【发布时间】:2017-02-17 16:00:49
【问题描述】:

Attached sample image

我无法在选项卡 1 数据上显示所选行项的绘图和原始数据。

我正在使用 observeEvent 获取选定的行作为输入并将焦点切换到选项卡 2。在同一函数中,我还尝试为保存的文件位置生成文本字符串。焦点确实发生了变化,但绘图和原始数据出现错误。

我确定我在这里遗漏了一些东西,可能是在获取我在 observeEvent 中生成的文件名文本字符串并将其传递给 renderDataTable 和反应函数时。 数据文件的链接在代码中。我正在共享“maintab.csv”(tab1)和第一行数据文件“Saratoga_Shen.csv”(tab2 和 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)

【问题讨论】:

    标签: r tabs shiny dt


    【解决方案1】:

    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)
    

    【讨论】:

      猜你喜欢
      • 2021-07-09
      • 2014-05-20
      • 2014-10-27
      • 1970-01-01
      • 2014-09-27
      • 1970-01-01
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多