r - Shiny:使用 fileInput() 和 selectizeInput() 自动化 Web 应用程序
问题描述
我正在尝试使用 fileInput() 和 selectizeInput() 函数自动化 Web 应用程序。实际上,我想根据所选变量绘制相关图。
但是,我得到:
错误:同时提供“X”和“Y”或类似矩阵的“X”
我认为我的问题来自这段代码:
data03 <- reactive({
file1 <- input$file
req(file1)
dataSet <- read_excel(file1$datapath)
col <- names(dataSet)
updateSelectizeInput(session = session, inputId = "corr02", choices = col)
})
observe({
varZ <- names(data03())
})
output$corrplot <- renderPlot({
df <- data03()
if(input$dataSubmit03){
isolate({
corr <- cor(x = df, method = "pearson", use = "pairwise.complete.obs")
corrplot(corr = corr,
type = "lower",
method = "circle",
tl.col = "black",
tl.srt = 45)
})
}
})
这是我使用的全部代码。谢谢你的帮助 !
library(shiny)
library(xlsx)
library(corrplot)
library(readxl)
# File used for the example
data(iris)
write.xlsx(x = iris, file = "iris.xlsx")
ui <- fluidPage(
navbarPage(
tabPanel(title = "Presentation"),
tabPanel(title = "Importing data",
sidebarLayout(
sidebarPanel(
fileInput(inputId = "file",
label = "Import a file",
accept = c(".xlsx", ".txt", ".csv")
)
),
mainPanel(
width = 12,
verbatimTextOutput("table"))
)
),
navbarMenu(title = "Descriptive analytics",
tabPanel(title = "Correlogram",
sidebarLayout(
sidebarPanel(
selectizeInput(inputId = "corr02",
label = "Select the variables:",
choices = NULL,
multiple = TRUE),
br(),
actionButton(inputId = "dataSubmit03",
label = "RUN")
),
mainPanel(
plotOutput(outputId = "corrplot",
height = "600px")
)
)
)
)
)
)
server <- function(input, output, session) {
df <- reactive({
inFile <- input$file
db <- read_excel(inFile$datapath)
db <- data.frame(db)
})
df1 <- reactive({
inFile <- input$file
db <- read_excel(inFile$datapath, na = "NA")
db <- data.frame(db)
})
data03 <- reactive({
file1 <- input$file
req(file1)
dataSet <- read_excel(file1$datapath)
col <- names(dataSet)
updateSelectizeInput(session = session, inputId = "corr02", choices = col)
})
observe({
varZ <- names(data03())
})
output$corrplot <- renderPlot({
df <- data03()
if(input$dataSubmit03){
isolate({
corr <- cor(x = df, method = "pearson", use = "pairwise.complete.obs")
corrplot(corr = corr,
type = "lower",
method = "circle",
tl.col = "black",
tl.srt = 45)
})
}
})
}
shinyApp(ui = ui, server = server)
期待
现实
解决方案
这应该适用于您的ui()
未更改。我改变的总结:
- 我更喜欢
update()
在自己的observe()
调用中使用函数(虽然不确定这是否是最好的方法) - 我还对列选择选项进行了子集化,以便您只能选择数字列。
- 在
renderPlot()
函数中,您永远不会为您选择的列设置子集。(那是df <- df[,input$corr02]
) - 我通常写这些的方式要求 if 语句在开头 (
if (!is.null(input$corr02))
,否则它会在你有机会选择哪些列之前立即抛出错误。
代码:
server <- function(input, output, session) {
data03 <- reactive({ if (!is.null(input$file)) {
file1 <- input$file
req(file1)
dataSet <- read_excel(file1$datapath)
return(dataSet)
}}) # reactive
observe({ if (!is.null(data03())) {
col_v <- names(data03())
whichNum_v <- which(sapply(data03(), class) == "numeric")
col_v <- col_v[whichNum_v]
updateSelectizeInput(session = session, inputId = "corr02", choices = col_v)
}}) # observe
output$corrplot <- renderPlot({
## Only run if selection has been made
if (!is.null(input$corr02)) {
## Subset columns
df <- data03()
df <- df[,input$corr02]
## Make plot on click
if(input$dataSubmit03){
isolate({
corr <- cor(x = df, method = "pearson", use = "pairwise.complete.obs")
corrplot(corr = corr,
type = "lower",
method = "circle",
tl.col = "black",
tl.srt = 45)})
} # fi
} # fi
}) # renderPlot
} # server
推荐阅读
- python - Heroku 无法加载我的 css 文件,因为它是 mimetype
- c++ - Base unique_ptr 的向量导致在 emplace_back(new T()) 上进行对象切片
- javascript - 如何在 JavaScript 中使用逗号分隔符从字符串中生成数字(“23,21”---> 23,21)
- reactjs - 条纹结帐后如何生成收据页面
- php - 致命错误:未捕获的错误:找不到类“INFOCUS_THEME\Inc\Menus”
- elasticsearch - Minikube:运行 Kibana 的问题
- javascript - 如何根据地理坐标获取日期?(JavaScript)
- c++ - 声明属于所有特化的类模板成员
- flutter - 在 Flutter 的 Style Dictionary 中为来自 Figma 的设计令牌编写自定义转换文件
- angular - 滚动页面时,mat-autocomplete 下拉菜单不会移动