在 shiny 中绘制两个不同的数据集
plot two different datasets in shiny
我正在尝试上传两个不同的数据集,然后一起生成一个图,其中一个在 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)
您的绘图代码存在一些问题。第一的。每个 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
我正在尝试上传两个不同的数据集,然后一起生成一个图,其中一个在 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)
您的绘图代码存在一些问题。第一的。每个 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