#r #tabs #shiny #dt
#r #вкладки #блестящий #dt
Вопрос:
Прилагаемый образец изображения
У меня возникли проблемы с отображением графика и необработанных данных выбранного элемента строки на вкладке 1 данных.
Я использую observeEvent, чтобы получить выбранную строку в качестве входных данных и переключить фокус на вкладку 2. В той же функции я также пытаюсь сгенерировать текстовую строку для местоположения сохраненного файла. Фокус переключается, но я получаю ошибку для графика и необработанных данных.
Я уверен, что мне здесь чего-то не хватает, возможно, в получении текстовой строки имени файла, которую я генерирую в observeEvent и передаю ее в функции renderdatable и reactive. Ссылки на файлы данных находятся в коде. Я делюсь «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)
Ответ №1:
Смотрите Пример выходного изображения
## 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)