'How do i fix error: data.frame: replacement has 148 rows, data has 1 (shiny and R)
I'm new to R and I'm trying to make my chart interactive using shiny and leaflet in R. however I can't seem to run my map_data() under reactive ({ code }) because I receive an error that says:
Warning: Error in $<-.data.frame: replacement has 148 rows, data has 1
I tried creating a new variable but it resulted in the samed thing. I stumbled upon similar solutions but I didn't exactly understand how to go about it.
This is my code overall:
library(janitor)
library(leaflet)
library(dplyr)
library(sp)
library(shiny)
climate <- read.csv("globalcrops.csv")
View(climate)
coordinates <- read.csv("worldcoord.csv")
View(coordinates)
#cleaning the data
climate <- climate %>%
janitor::clean_names()#clean names
#selecting specific columns
#removing missing values with na.omit
df3 <- climate[, c("i_country", "act_ch_wha1f2050", "act_ch_mza1f2050", "act_ch_ria1f2050")] %>%
na.omit(df)
df3
#combine tables together
#new data frame df3
df4 = merge(x=df3, y=coordinates, by="i_country")
df4
#changing data of latitude and longitude into numerical value
df4$latitude <- as.numeric(df4$latitude)
df4$longitude <- as.numeric(df4$longitude)
#creating a new spatial point data frame
#to echo spatial point data frame there
#for mapping
# -
#getting spatial data from columns 5 and 6
#the
df4.SP <- SpatialPointsDataFrame(df4[,c(5,6)], df4[,-c(5,6)])
# change colors
countCol <- colorFactor(palette = 'RdYlGn', df4$i_country)
#plotting the map using leaflet
print (
m2 <- leaflet() %>%
addProviderTiles("Esri.WorldGrayCanvas") %>%
addCircles(data = df4, lng = ~longitude, lat = ~latitude,
popup = paste("Country:", df4$i_country, "<br>",
"Wheat Total Production Changes:", df4$act_ch_wha1f2050, "<br>",
"Maize Total Production Changes:", df4$act_ch_mza1f2050, "<br>",
"Rice Total Production Changes:", df4$act_ch_ria1f2050),
color = ~countCol(i_country)
)
)
ui <- fluidPage(
titlePanel("Wheat, Maize and Rice Total Production Changes in 2050 Applying the SRES A1F2050 Scenario Yield Change to 1990 Production"),
sidebarPanel (
uiOutput("countrynames")
#helpText("Production Changes of Staple Crops in Various Countries"),
#selectInput(inputId = "var",
# label = "Choose a Location",
# choices = c ("Andorra",
# "United Arab Emirates"),
# selected = "Andora"),
),
leafletOutput("mymap")
)
server <- function(input, output, session){
output$countrynames <- renderUI({
helpText("Production Changes of Staple Crops in Various Countries")
selectInput(inputId = "i_country",
label = "Select a country",
c(as.character(df4$i_country)))
})
map_data <- reactive({
data <- data.frame(df4[df4$i_country == input$i_country,])
data$popup <- paste0("Country:", df4$i_country, "<br>",
"Wheat Total Production Changes:", df4$act_ch_wha1f2050, "<br>",
"Maize Total Production Changes:", df4$act_ch_mza1f2050, "<br>",
"Rice Total Production Changes:", df4$act_ch_ria1f2050)
return(data)
})
output$mymap <- renderLeaflet({
m2 <- leaflet(data = map_data()) %>%
addProviderTiles("Esri.WorldGrayCanvas") %>%
addCircles(lng = ~longitude, lat = ~latitude, popup = ~popup,
#paste("Country:", df4$i_country, "<br>",
# "Wheat Total Production Changes:", df4$act_ch_wha1f2050, "<br>",
# "Maize Total Production Changes:", df4$act_ch_mza1f2050, "<br>",
# "Rice Total Production Changes:", df4$act_ch_ria1f2050),
color = ~countCol(i_country)
)
})
}
shinyApp(ui, server)
Any help will be great! Thanks
Solution 1:[1]
Problem is probably here:
map_data <- reactive({
data <- data.frame(df4[df4$i_country == input$i_country,])
# you filtered data to one row but you used df4 below
data$popup <- paste0("Country:", data$i_country, "<br>",
"Wheat Total Production Changes:", data$act_ch_wha1f2050, "<br>",
"Maize Total Production Changes:", data$act_ch_mza1f2050, "<br>",
"Rice Total Production Changes:", data$act_ch_ria1f2050)
return(data)
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SmokeyShakers |
