我试图在Shiny中创建一些可以上传文件的东西,其中包括坐标。最后我想要包含更多变量选项,但是现在我有一个非常简单的设置,也包括我的问题:我无法使用上传的坐标,我可能会遗漏一些东西像一个服务器/代理。这就是我到目前为止所做的:
UI
shinyUI(fluidPage(theme = "style.css",
titlePanel("Data Mapping\n"),
tabsetPanel(
tabPanel(h4("Upload File"),
titlePanel(h5("Upload your data here:")),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
# added interface for uploading data from
# http://shiny.rstudio.com/gallery/file-upload.html
tags$br(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
'"')
),
mainPanel(
tableOutput('contents')
)
)
),
tabPanel(h4("Data Map"),
leafletOutput("leafmap", width = 2000, height = 850),
pageWithSidebar(
headerPanel(''),
absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
draggable = TRUE, top = "20%", left = "auto", right = 40, bottom = "auto",
width = 100, height = 600, offset = 15, style='width: 350px; height: 400px;opacity: 0.8;padding:20px;',
# "Empty inputs" - they will be updated after the data is uploaded
selectInput('xcol', 'Longitude', "", selected = ""),
selectInput('ycol', 'Latitude', "", selected = ""),
selectInput('Varchoice', 'Variable', "", selected = "")
),
mainPanel(
plotOutput('MyPlot')
)
)
),
navbarPage(title = div( "XXX"), position = "fixed-top",
windowTitle = "Pilot", theme = shinytheme("flatly"), tags$style(HTML(".datepicker {z-index:99999 !important;}"))
)
)
))
服务器
shinyServer(function(input, output, session) {
mydata<-reactive({
req(input$file1)
inFile <- input$file1
if(is.null(inFile)){return()}
read.csv(inFile$datapath, header = input$header, sep = input$sep,
quote = input$quote)
})
output$contents <- renderTable({
mydata()
})
observe({
updateSelectInput(session, inputId = 'xcol', label = 'Longitude',
choices = names(mydata()))
updateSelectInput(session, inputId = 'ycol', label = 'Latitude',
choices = names(mydata()))
})
output$leafmap <- renderLeaflet({
mydata<-mydata()
leaflet(data=mydata()) %>%
addTiles() %>%
addCircleMarkers(~input$xcol, ~input$ycol)
})
})
要查看一切是否正常,我使用的文本文件包含以下数据:
example.df<-data.frame(
Season = c("summer", "summer","summer","summer","summer","winter","winter","winter","winter","winter"),
Value = c(10, 30, 40, 70, 60, 80, 90, 45, 78, 50),
Country = c("NL", "AR", "IT", "FI", "IN", "NL", "AR", "IT", "FI", "IN"),
YCOR = c(52.282458,-34.042788, 43.26699, -17.829055, -4.880873,52.282458,-34.042788, 43.26699, -17.829055, -4.880873),
XCOR = c(4.981905, -63.241606, 11.892454, 178.01177, 120.163872, 4.981905, -63.241606, 11.892454, 178.01177, 120.163872))
我得到的错误是:
> Warning: Error in validateCoords: addCircleMarkers requires numeric
> longitude/latitude values Stack trace (innermost first):
> 89: validateCoords
> 88: derivePoints
> 87: addCircleMarkers
> 86: function_list[[k]]
> 85: withVisible
> 84: freduce
> 83: _fseq
> 82: eval
> 81: eval
> 80: withVisible
> 79: %>%
> 78: func [C:\Users\Documents\Projects/server.R#33]
> 77: origRenderFunc
> 76: output$leafmap
> 1: runApp