是否可以通过调用列表中的数据框来填充“ selectInput”的选择(值)?这将有助于加快脚本编写速度,并省去冗长的选择。
我的 app.R 文件夹中有一个文件夹(选择)。此文件夹包含15个.xlsx(Excel)文件,每个文件包含两列(x = id,y = value)。
l list<-list.files(pattern=“*.xlsx”)
df_list<-lapply(list,readxl::read_excel)
这将返回一个数据帧列表(例如):
列表2 $ 001.txt:'data.frame':15磅2个变量: .. $ X:具有50个水平的因子 .. $ Y:具有50个水平的因子 $ 002.txt:'data.frame':5个观察点2个变量 .. $ X:具有5个级别的因子 .. $ Y:w / 5级因子
当我尝试调用值(相关数据框的第二列)时,应用失败,提示选择必须具有值。
selectInput (“state”, “States:”,
df_list[2][[2]]),
selectInput (“cnfd”, “Confidence”,
df_list[7][[2]])
...
str(df_list [2])
df x:1,2,3,4,5 ... 50 y:阿拉斯加,阿拉巴马州,阿肯色州...
str(df_list [7]
df x:1,2,3,4,5 y:“非常低”,“低”,“中等”,“高”,“非常高”
有什么想法吗?
===========更新==============
想要确保列出的结构正确:
str(df_list)
List of 4
$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 5 obs. of 2 variables:
..$ id : num [1:5] 1 2 3 4 5
..$ value: chr [1:5] "very low" "low" "moderate" "high" ...
$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 45 obs. of 2 variables:
..$ id : num [1:45] 1 2 3 4 5 6 7 8 9 10 ...
..$ value: chr [1:45] "George Washington" "John Adams" "Thomas Jefferson" "James Madison" ...
$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 50 obs. of 2 variables:
..$ id : num [1:50] 1 2 3 4 5 6 7 8 9 10 ...
..$ value: chr [1:50] "Alabama" "Alaska" "Arizona" "Arkansas" ...
$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 196 obs. of 2 variables:
..$ id : num [1:196] 1 2 3 4 5 6 7 8 9 10 ...
..$ value: chr [1:196] "Kabul" "Tirana" "Algiers" "Andorra la Vella"...
ShinyApp:
load('C:/Users/me/OneDrive/Documents/RProj/apps/data/dfenv.RData')
ui <- fluidPage(
titlePanel("Testing selectInput with list of data-frames"),
sidebarLayout(
sidebarPanel(
selectInput("state","States:",
df_list[3][2]),
selectInput("cty","Capital City:",
df_list[4][2]),
selectInput("cnf","Confidence:",
df_list[1][2]),
selectInput('potus',"President:",
df_list[2][2])
)))
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)
错误:
(功能(选择,名称):)中的错误: 必须选择“选择”中的所有子列表。
答案 0 :(得分:0)
和往常一样,我找到了答案。.为未来发布。
library(readxl)
#create list of all files
list<-list.files(pattern="*.xlsx")
#read the files from `list`
df_list<-lapply(list,readxl::read_excel)
#apply function to take the second column from each sub-list (df)
x<-lapply(df_list, function(x) x[2])
#unlist IOT call in app
x2<-unlist(x, recursive = FALSE)
结构(x2):
> List of 4 $ value: chr [1:5] "very low" "low" "moderate" "high" ...
> $ value: chr [1:45] "George Washington" "John Adams" "Thomas
> Jefferson" "James Madison" ... $ value: chr [1:50] "Alabama" "Alaska"
> "Arizona" "Arkansas" ... $ value: chr [1:196] "Kabul" "Tirana"
> "Algiers" "Andorra la Vella" ...
App(请注意,输出与我测试selectInput
时完全不同)
library(shiny)
library(readxl)
list<-list.files(pattern="*.xlsx")
df_list<-lapply(list,readxl::read_excel)
df_x<-lapply(df_list, function(x) x[2])
x2<-unlist(x, recursive = FALSE)
shinyApp(
ui = fluidPage(
selectInput(inputId = "state",label="States:",
choices =
x2[3]),
selectInput(inputId="cty",label="Capital City:",
choices=
x2[4]),
selectInput(inputId="cnf",label = "Confidence:",
choices =
x2[1]),
selectInput(inputId='potus',label = "President:",
choices=
x2[2]),
tableOutput('data')),
server = function(input, output) {
output$data<-renderTable({
mtcars[,c("mpg",input$state),drop=FALSE]},rownames=TRUE)
}
)