有人能判断一下我的代码,确保其正确无误吗?我已经在中读取了数据,看起来一切都很好,就在我运行shiny时,图表出现了,但没有数据.我用#对程序隐藏了一些功能,因为我无法正确 Select 其他变量.我已经包括了一些关于天气文件、天气年份和天气数据的数据.

weather_files

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"))

读到它的人,其输入数据如下:

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

基本上,我想做的是 Select 城镇,然后 Select 年份和一个变量(第5列:第9列——但这根本不起作用),所以我现在 Select 了Sunshine列.然后将X轴上的"月\年"列与Y轴上的"阳光"列相对应,按年份着色/填充,希望您可以 Select 多个年份.希望这能让我明白我在做什么.

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列中有一些NA,输入的 Select 基于levels个变量,而不是其中的unique个值.所有这些组合将在selectInput中产生不会出现在绘图中的选项.相反,试着做:

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

这将把输入的 Select 范围缩小到数据集中观察到的数据.

或者,我们可以创建如下react 式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相关问答推荐

对lme 4对象运行summary()时出错(diag中的错误(from,names = RST):对象unpackedMatrix_diag_get找不到)

按R中的组查找相邻列的行累积和的最大值

从gtsummary包中使用tBL_strata()和tBL_summary()时删除变量标签

如何删除gggvenn与gggplot绘制的空白?

修改用R编写的用户定义函数

我想在R中总结一个巨大的数据框架,使我只需要唯一的lat、lon、Date(Year)和Maxium Value""""""""

如何优化向量的以下条件赋值?

根据日期从参考帧中创建不同的帧

如何从R ggplot图片中获取SVG字符串?

我正在努力用R计算数据集中的中值逐步距离

R:用GGPLATE,如何在两个独立的变量中制作不同形状的散点图?

如何在R中使用hmm TMB提前一步预测观察到的状态?

'使用`purrr::pwalk`从嵌套的嵌套框架中的列表列保存ggplots时出现未使用的参数错误

使用ggplot2中的sec_axis()调整次轴

整理曲线图、曲线图和点图

如何在AER::ivreg中指定仪器?

条形图中的条形图没有try 赋予它们的 colored颜色

使用列名和r中的前缀 Select 列的CREATE函数

使用一个标签共享多个组图图例符号

从r2d3到htmlwidget