【发布时间】:2020-12-09 05:21:39
【问题描述】:
我正在尝试使用来自NYC Tree Census 2015 的 R Shiny 创建仪表板。仪表板应该看起来像这里的图片 > Dashboard in Shiny Picture
我的代码如下:
library(shiny)
library(tidyverse)
library(ggplot2)
my_data <- read.csv("/Users/abhikpaul/Documents/Documents/Github/Fiverr/2015_Street_Tree_Census_-_Tree_Data.csv")
ui <- fluidPage(
titlePanel("The Dashboard of Tree Distribution in New York City"),
sidebarLayout(
sidebarPanel(
# Description ----
helpText("In this page you can get information about the tree distribution, status, health conditions, and species rank in New York City. Please choose the borough that you want to check. It may take 10 seconds for the graphics to load. Thank you for your patience!"),
#Input: Check boxes for Boroughs ----
checkboxGroupInput("checkboxInput",
label = "Borough",
choices = list("Bronx",
"Brooklyn",
"Manhattan",
"Queens",
"Staten Island"),
selected = "Bronx"),
),
# Main panel for displaying outputs ----
mainPanel(
# Tabs panel for displaying outputs ----
tabsetPanel(type = "tabs",
#Output: About ----
tabPanel("About",
h3("About this dataset", align = "left"),
p("The dataset displays the information of trees (including health, status, species, etc.) within the five boroughs in New York City. The dataset is organized by NYC parks & Recreation and partner organizations."),
h3("How to make NYC an urban forest?", align = "left"),
p("As a group, we are concerned about planting tree and green environments. Therefore, we will focus on identifying the locations that require more taking care of trees, the top species that have the most number of trees in each borough, the health conditions of those species, and the distribution of trees in each borough."),
HTML("<p>For more information, visit: <a href='https://data.cityofnewyork.us/Environment/2015-Street-Tree-Census-Tree-Data/uvpi-gqnh'>2015 NYC Tree Census</a></p>")
),
#Output: Status ----
tabPanel("Status", plotOutput(outputId = "statusplot")),
)
)
)
) )
server <- function(input, output) {
my_data <- as_tibble(my_data)
my_data <- my_data[my_data$borough %in% checkboxInput,]
my_data <- data.frame(table(my_data$borough,my_data$status))
my_data <- my_data[apply(my_data!=0, 1, all),]
my_data <- my_data %>%
group_by(Var1) %>%
mutate(Percent = (Freq/sum(Freq) * 100))
output$statusplot <- renderPlot({
ggplot(my_data, aes(fill = Var2, y = Percent, x = Var1)) +
geom_bar(position = "dodge", stat = "identity")
})
}
shinyApp(ui = ui, server = server)
但是,在运行应用程序时,我收到如下所述的错误
警告:匹配错误:“匹配”需要向量参数
50:%in%
47:服务器 [/Users/abhikpaul/Documents/Documents/GitHub/Fiverr/my_app.R#90]匹配错误(x, table, nomatch = 0L) : 'match' 需要向量参数
由于我是 R Shiny 的新手,有人可以帮我解决这个问题吗?
【问题讨论】:
-
将
my_data$borough %in% checkboxInput更改为my_data$borough %in% input$checkboxInput。 -
我尝试改变它。我现在收到一个新错误。下面提到的是错误。
59: stop 58: .getReactiveEnvironment()$currentContext 57: getCurrentContext 53: .subset2(x, "impl")$get 52: $.reactivevalues 47: server [/Users/abhikpaul/Documents/Documents/GitHub/Fiverr/my_app.R#89 Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.) -
你好。您必须将使用 UI 中的值的代码放入响应式代码块(渲染器、观察器......)。服务器函数主要是一个管道函数,被调用一次以链接输入和输出。为了证明这一点,在服务器函数的开头放置一个简单的
message("In server")并启动您的应用程序。无论您对界面执行什么操作,您都只会在控制台中看到一条消息。