gpt4 book ai didi

r - 如何让我的数据正确呈现?

转载 作者:行者123 更新时间:2023-12-05 05:42:18 27 4
gpt4 key购买 nike

有人可以检查我的代码以确保其全部正确吗?我已经读入了数据,看起来一切正常,就在我运行 shiny 时,图表出现了,但没有数据。我用 # 隐藏了程序的一些功能,因为我无法正确选择其他变量。我已经包含了 weather_files、weather_years 和天气数据的一些数据。

天气文件

structure(list(Town = structure(c(2L, 4L, 2L, 1L, 1L, 1L, 1L, 
4L, 1L, 4L, 5L, 1L, 2L, 4L, 2L, 5L, 5L, 1L, 3L, 4L, 3L, 3L, 5L,
5L, 5L), .Label = c("braemar", "dunstaffnage", "nairn", "stornoway",
"tiree"), class = c("ordered", "factor")), year = structure(c(132L,
37L, 128L, 100L, 137L, 147L, 143L, 22L, 103L, 89L, 96L, 138L,
109L, 87L, 142L, 109L, 61L, 138L, 136L, 75L, 112L, 92L, 118L,
100L, 66L), .Label = c("1873", "1874", "1875", "1876", "1877",
"1878", "1879", "1880", "1881", "1882", "1883", "1884", "1885",
"1886", "1887", "1888", "1889", "1890", "1891", "1892", "1893",
"1894", "1895", "1896", "1897", "1898", "1899", "1900", "1901",
"1902", "1903", "1904", "1905", "1906", "1907", "1908", "1909",
"1910", "1911", "1912", "1913", "1914", "1915", "1916", "1917",
"1918", "1919", "1920", "1921", "1922", "1923", "1924", "1925",
"1926", "1927", "1928", "1929", "1930", "1931", "1932", "1933",
"1934", "1935", "1936", "1937", "1938", "1939", "1940", "1941",
"1942", "1943", "1944", "1945", "1946", "1947", "1948", "1949",
"1950", "1951", "1952", "1953", "1954", "1955", "1956", "1957",
"1958", "1959", "1960", "1961", "1962", "1963", "1964", "1965",
"1966", "1967", "1968", "1969", "1970", "1971", "1972", "1973",
"1974", "1975", "1976", "1977", "1978", "1979", "1980", "1981",
"1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989",
"1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997",
"1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005",
"2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013",
"2014", "2015", "2016", "2017", "2018", "2019", "2020"), class =
"factor"),
month_year = structure(c(10L, 7L, 2L, 7L, 6L, 11L, 8L, 3L,
4L, 3L, 12L, 6L, 1L, 7L, 3L, 9L, 4L, 4L, 8L, 4L, 10L, 3L,
4L, 10L, 8L), .Label = c("April", "August", "December", "February",
"January", "July", "June", "March", "May", "November", "October",
"September"), class = "factor"), month = c(11, 6, 8, 6, 7,
10, 3, 12, 2, 12, 9, 7, 4, 6, 12, 5, 2, 2, 3, 2, 11, 12,
2, 11, 3), `Maximum Temp` = c(104, 134, 191, 132, 178, 98,
76, 8, 6, 58, 151, 17, 121, 158, 81, 132, 71, 23, 82, 29,
93, 59, 81, 93, 101), `Minimum Temp` = c(63, 8, 115, 5, 82,
2, 1, 26, 22, 11, 101, 89, 36, 87, 28, 75, 31, 58, 16, 21,
42, 1, 31, 41, 66), `Air Frost Days` = c(2, 0, 0, 0, 0, 12,
13, 4, 17, 19, 0, 0, 2, 0, 3, 0, 2, 24, 6, 17, 2, 12, 2,
1, 0), `Rainfall (mm)` = c(1074, 564, 107, 636, 884, 642,
684, 1789, 356, 104, 1281, 93, 247, 744, 2712, 78, 904, 156,
416, 111, 841, 466, 2463, 1104, 1091), Sunshine = c(NA, NA,
1538, 1431, NA, NA, NA, NA, 801, 263, 1375, NA, NA, 206,
NA, 1993, 723, NA, 1068, 863, 339, NA, 459, 456, 628)), row.names =
c(NA,
-25L), class = c("tbl_df", "tbl", "data.frame"))

读取它的人,Shiny 的输入数据是这样的:

weather_data <- levels(weather_files$Town)
Weather_years <- levels(weather_files$year)
Weather_month <- levels(weather_files$month_year)

本质上,我想做的是选择城镇,然后选择年份和一个变量(第 5:9 列 - 然而这根本行不通),所以我现在选择了阳光列。然后绘制 X 轴上的 month_year 列与 Y 轴上的 Sunshine 列,颜色/按年份填充作为 id 希望您可以选择多个年份。希望这能让我清楚地知道我在做什么。提前致谢。

ui <- fluidPage(

titlePanel("Met Office Weather"),


sidebarLayout(
sidebarPanel(
selectInput(label = ("Select Data Set"),
inputId = "Town",
choices = weather_data,
multiple = TRUE),

selectInput(label = "Select Year",
inputId = "year",
choices = Weather_years,
multiple = TRUE),

#selectInput(inputId = "selection",
# label="Select Variable",
# choices = c("Sunshine"),
# multiple = FALSE),

actionButton(inputId = "go", label = "Plot Data")
),

# Show the generated plot
mainPanel(
tabsetPanel(
tabPanel("scatterplot", plotOutput("scatter")),
tabPanel("Barplot", plotOutput("barplot")),
tabPanel("The Data", dataTableOutput("table"))
)
)
)
)


server <- function(input, output) {

select_weatherdf <- reactive({
# gather info from user but only when asked
pick_town <- isolate(input$Town)
select_year <- isolate(input$year)
#choose_variable <- isolate(input$Sunshine)

# listen to go button
input$go

# select the correct town and year looking at sunshine variable only
weather_to_plot <- weather_files %>% filter(Town %in% pick_town, year %in% select_year)
#weather_to_plot <- weather_to_plot %>% select("Town", "year", "month_year", "Sunshine")

return(weather_to_plot)

})

output$scatter <- renderPlot({

data_to_plot <- select_weatherdf()
ggplot(data_to_plot, aes(x = month_year, y = Sunshine , fill = year)) +
geom_point(size = 2.5) +
scale_x_discrete(limits = month.name)



})

output$barplot <- renderPlot({

data_to_plot <- select_weatherdf()
ggplot(data_to_plot, aes(x = month_year, fill = year)) +
geom_bar() +
scale_x_discrete(limits = month.name)
})


output$table <- renderDataTable({

data_to_plot <- select_weatherdf()

})
}

# Run the application
shinyApp(ui = ui, server = server)

最佳答案

应用程序按预期工作,图中没有任何内容的原因是数据在 sunshine col 中有一些 NA,并且输入的选择基于 levels 变量但不是它的 unique 值。所有这些结合起来将在 selectInput 中产生不会出现在图中的选择。而是尝试做:

weather_data <- unique(weather_files$Town)
Weather_years <- unique(weather_files$year)
Weather_month <- unique(weather_files$month_year)

这将缩小对数据集中观察到的数据的输入选择。

我们可以选择像这样创建响应式 select_weatherdf:

  select_weatherdf <- eventReactive(input$go, {
weather_files %>% filter(Town %in% input$Town, year %in% input$year) %>%
select("Town", "year", "month_year", "Sunshine")
#no need for a return statement here R will return the last value called from the function
})

关于r - 如何让我的数据正确呈现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72071908/

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