我正在嘗試上傳兩個不同的資料集,然后一起生成一個圖,其中一個在 geom_line() 中,另一個在 geom_point() 中。我在 UI 中創建了兩個不同的 fileInput 來上傳資料集。但是,我完全迷失了,代碼不會執行。執行此操作的解決方案是什么?
ui <- navbarPage("pp",
tabPanel("Vis",
sidebarLayout(
sidebarPanel(
fileInput("upload1","Observed Data",
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')
),
selectInput('xcol1', 'X Variable', "",width=140),
selectInput('ycol1', 'Y Variable', "", selected = "",width=140),
fileInput("upload2","Simulated Data",
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')
),
selectInput('xcol2', 'X Variable', "",width=140),
selectInput('ycol2', 'Y Variable', "", selected = "",width=140)
),
mainPanel(
plotOutput("plot")
)
)
)
)
server <- function(input, output, session) {
data <- reactive({
req(input$upload1)
inFile <- input$upload1
df_1 <- read.csv(inFile$datapath, header = TRUE, sep = ",")
updateSelectInput(session, inputId = 'xcol1', label ='X vairable',
choices = names(df_1), selected = names(df_1)[1])
updateSelectInput(session, inputId = 'ycol1', label ='Y vairable',
choices = names(df_1), selected = names(df_1)[2])
return(df_1)
})
#####
data_2 <- reactive({
req(input$upload2)
inFile <- input$upload2
df_2 <- read.csv(inFile$datapath, header = TRUE, sep = ",")
updateSelectInput(session, inputId = 'xcol2', label ='X vairable',
choices = names(df_2), selected = names(df_2)[3])
updateSelectInput(session, inputId = 'ycol2', label ='Y vairable',
choices = names(df_2), selected = names(df_2)[4])
return(df_2)
})
output$plot <-renderPlot({
ggplot()
geom_point(data(), aes(.data[[input$xcol1]], .data[[input$ycol1]] *1000, size=1)
geom_line(data_2(), aes(.data_2[[input$xcol2]]/60, .data_2[[input$ycol2]], size=1)
theme_bw()
})
}
shinyApp(ui, server)
uj5u.com熱心網友回復:
您的繪圖代碼存在一些問題。第一的。每個引數的第一個引數geom是mapping. 如果你傳遞一個資料集,你必須明確地命名引數,即例如data = data(). 其次,沒有.data_2代詞。只需使用.data. 最后:注意你設定括號的位置。(;
注意:我取消了與檔案輸入相關的所有代碼的注釋,僅用于mtcars兩個資料集。
library(shiny)
library(ggplot2)
ui <- navbarPage(
"pp",
tabPanel(
"Vis",
sidebarLayout(
sidebarPanel(
# fileInput("upload1", "Observed Data",
# accept = c(
# "text/csv",
# "text/comma-separated-values,text/plain",
# ".csv"
# )
# ),
selectInput("xcol1", "X Variable", "", width = 140),
selectInput("ycol1", "Y Variable", "", selected = "", width = 140),
# fileInput("upload2", "Simulated Data",
# accept = c(
# "text/csv",
# "text/comma-separated-values,text/plain",
# ".csv"
# )
# ),
selectInput("xcol2", "X Variable", "", width = 140),
selectInput("ycol2", "Y Variable", "", selected = "", width = 140)
),
mainPanel(
plotOutput("plot")
)
)
)
)
server <- function(input, output, session) {
data <- reactive({
# req(input$upload1)
# inFile <- input$upload1
df_1 <- mtcars # read.csv(inFile$datapath, header = TRUE, sep = ",")
updateSelectInput(session,
inputId = "xcol1", label = "X vairable",
choices = names(df_1), selected = names(df_1)[1]
)
updateSelectInput(session,
inputId = "ycol1", label = "Y vairable",
choices = names(df_1), selected = names(df_1)[2]
)
return(df_1)
})
#####
data_2 <- reactive({
# req(input$upload2)
# inFile <- input$upload2
df_2 <- mtcars # read.csv(inFile$datapath, header = TRUE, sep = ",")
updateSelectInput(session,
inputId = "xcol2", label = "X vairable",
choices = names(df_2), selected = names(df_2)[3]
)
updateSelectInput(session,
inputId = "ycol2", label = "Y vairable",
choices = names(df_2), selected = names(df_2)[4]
)
return(df_2)
})
output$plot <- renderPlot({
ggplot()
geom_point(data = data(), aes(.data[[input$xcol1]], .data[[input$ycol1]] * 1000), size = 1)
geom_line(data = data_2(), aes(.data[[input$xcol2]] / 60, .data[[input$ycol2]]), size = 1)
theme_bw()
})
}
shinyApp(ui, server)
#>
#> Listening on http://127.0.0.1:5810
#> Warning: Error in is_string: argument "x" is missing, with no default

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/462755.html
