R闪亮错误-尝试使用.csv数据集创建R闪亮仪表板:https://www.kaggle.com/datasets/ws



你好,我正在尝试创建一个Rshing仪表板,我已经完成了以下R脚本,但函数错误无法加载网页,并导致以下错误。谢谢你,因为我选错了选修课,我不得不在一周内第一次做R shine,这是我完成研究生院学业所需要完成的最后一项作业,我一直在尝试不同的方法来解决问题,但一周内都没能完成。如果你手头有一点时间,并为用这个数据集开发一个快速仪表板提供一些帮助(对于任何类型的分析图,只有2-3页的子项,简直是超级基本的(,那真的会很有帮助。

数据集:**https://www.kaggle.com/datasets/wsj/college-salaries**

library(shiny)
library (shinydashboard)
library (shinythemes)
library (readr)
library (ggplot2)
library(shinyWidgets)

setwd("C:/")
degrees <- read.csv("C: /degrees-that-pay-back.csv", header =  TRUE)
college <- read.csv("C:  /salaries-by-college-type.csv", header =  TRUE)
salaries <- read.csv("C:/ /salaries-by-region.csv", header =  TRUE)
library(shiny)
library(shinyWidgets)
library(ggplot2)
library(dplyr)


ui <- fluidPage(
setBackgroundColor(color = c("#66e0ff", "#00a3cc", "#003d4d")),
titlePanel("Where does it pay to attend college?"),
sidebarLayout(
sidebarPanel(tags$style(".well {background-color:#e6f9ff;}"),
sliderInput(inputId = "range",
label = "Chose the year range:",
min = 30000, max = 1000000, value = c(30000,100000)),
selectInput(inputId = "dis",
label = "Chose the School type",
choices = unique(college$School.Type)),
checkboxGroupInput(inputId = "con",
label = "Region of the school ",
choices = unique(salaries$Region),
selected = unique(salaries$Region)[1])

),
mainPanel(plotOutput("graph1")) 
)
)
server <- function(input, output){

df_dat <- observeEvent({


req(input$con, input$dis, input$range) 


df_dat <- filter(degrees, between(degrees$Starting.Median.Salary, input$range[1], input$range[2]), college$School.Type == input$dis, salaries$Region %in% input$con)

return(df_dat)
})

observe(print(str(df_dat())))

# create a graph 
output$graph1 <- renderPlot({


req(df_dat())

# plot filtered data
ggplot(df_dat(), aes(x = degrees$Starting.Median.Salary, y = college$School.Name)) +
geom_line(aes(colour = salaries$Region))+      
geom_point()
})

}
shinyApp(ui = ui, server = server)

我正试图使用以下数据集链接创建一个带有R光泽的仪表板:https://www.kaggle.com/datasets/wsj/college-salaries真的感谢你的帮助!谢谢。:(

我不得不猜测你想要什么,但我想我大致想出了你想要的。我把我的笔记排成一行,以帮助你,并显示你在哪里错过了东西。例如,你的observeEvent()没有观察到任何东西,你试图将其存储为一个变量,我认为你无法做到这一点。我还认为你需要将数据集组合起来才能进行过滤,否则它们的长度会不同,无法进行绘制。希望这能给你一个如何进步的想法:

library(shiny)
# library (shinydashboard) #Not needed for this
# library (shinythemes) #Don't have shinythemes, not necessary to your question
library (readr)
library (ggplot2)
# library(shinyWidgets) #Not needed for this

# setwd("C:/")  #I don't need to change wd so I blocked this

# library(shiny) #Duplicated
# library(shinyWidgets) #Duplicated
# library(ggplot2) #Duplicated
library(dplyr) #Moved file read in after dplyr to have mutate
#At least how I interpeted your use of the data, I don't know if the degrees data set is used. You just filter the starting median salary
#But there is also the starting median salary in the college dataset, and so I just filtered that column instead
# degrees <- read.csv("degrees-that-pay-back.csv", header =  TRUE)%>% #Adjusted csv location to my default directory
#   mutate(Starting.Median.Salary = parse_number(as.character(Starting.Median.Salary))) #Parse number to make currency into character then numeric
college <- read.csv("salaries-by-college-type.csv", header =  TRUE)%>% #Adjusted csv location to my default directory
mutate(Starting.Median.Salary = parse_number(as.character(Starting.Median.Salary))) #Parse number to make currency into character then numeric
salaries <- read.csv("salaries-by-region.csv", header =  TRUE) #Adjusted csv location to my default directory 
total<-college%>% #Combined the dataset into ONE dataframe instead of three, though I'm not actually using the degrees data
left_join(salaries%>% #Join college data with salaries data
select(School.Name, Region), #Only need school name and region columns from the salaries data
by = "School.Name")%>% #Join by the school.name column
mutate(Region = ifelse(is.na(Region), "Other", as.character(Region))) #Filtering when NA wasn't working right, so I'm changing those missing to other
#I think the otnly school without a region is Embry Riddle

MinSal<-min(total$Starting.Median.Salary) #Creating a minimum starting median salary
MaxSal<-max(total$Starting.Median.Salary) #Creating a maximum starting median salary
ui <- fluidPage(
# setBackgroundColor(color = c("#66e0ff", "#00a3cc", "#003d4d")), #Don't have shinythemes, not necessary to your question
titlePanel("Where does it pay to attend college?"),
sidebarLayout(
sidebarPanel(tags$style(".well {background-color:#e6f9ff;}"),
sliderInput(inputId = "range",
label = "Choose the starting median salary range:", #Originally was Chose the year range:
min = MinSal, 
max = MaxSal,
value = c(MinSal*1.25, MaxSal*.75)), #Default value is .25% and .75% between min/max
selectInput(inputId = "dis",
label = "Chose the School type",
choices = unique(total$School.Type)),
checkboxGroupInput(inputId = "con",
label = "Region of the school ",
choices = unique(total$Region),
selected = unique(total$Region)[1])

),
mainPanel(plotOutput("graph1")) 
)
)
server <- function(input, output){

df_dat<-reactiveValues("combined" = NULL) #Create a NULL reactiveValues. This is where we will store the data used for the graph

observeEvent(c(input$con, input$dis, input$range),{ #Observes these inputs for changes
#Also, observeEvent was previously assigned to a variable, which I don't think is needed so I removed that.
req(input$con, input$dis, input$range)

df_dat$combined<-total%>% #Using the total data from above, filter using the conditions you set up, and add it to the reactiveValue
filter(between(Starting.Median.Salary, input$range[1], input$range[2]), 
School.Type == input$dis,
Region %in% input$con)

})

observe(print(str(df_dat$combined))) #Print the reactiveValue

# create a graph 
output$graph1 <- renderPlot({
req(df_dat$combined) #Requires the reactiveValue to make the graph
# plot filtered data 
#This was my understanding of what you wanted for a graph. It was pretty unclear in the question though, so this is just a guess. 
ggplot(df_dat$combined) + aes(x = Starting.Median.Salary, y = School.Name, colour = Region, group = Region) +
#added the group section so the line is based on each region.
geom_line()+
geom_point()
})

}
shinyApp(ui = ui, server = server)