gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:17:33 25 4
gpt4 key购买 nike

Attached sample image

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

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

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

最佳答案

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)

关于r - 链接选项卡以在 R Shiny 中按行选择显示绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39937812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com