我正在尝试构建一个有光泽的应用程序。第一步是用户将从可用数据帧列表中选择要处理的数据帧。为此,我使用selectInput来获取用户的输入。我的代码是这样的:
ui <- fluidPage(
title = "Pivot Tool",
sidebarLayout(
sidebarPanel(
selectInput(
inputId = "tableName", label = "Select Table:",
selected = NULL, multiple = FALSE,
choices = c("Select Input",ls()[sapply(ls(), function(x) class(get(x))) == 'data.frame'])
),
<Some other inputs>
)
)
)
这不起作用。我选择的唯一选项是&#34;选择输入&#34;并且未填充全局环境中的数据框。输出的快照如下: Snapshot of the output
如果我将可用数据帧列表存储到变量中然后在上面的代码中使用该变量,则代码可以正常工作。像这样:
temp = ls()[sapply(ls(), function(x) class(get(x))) == 'data.frame']
ui <- fluidPage(
title = "Pivot Tool",
sidebarLayout(
sidebarPanel(
selectInput(
inputId = "tableName", label = "Select Table:",
selected = NULL, multiple = FALSE,
choices = c("Select Input",temp)
),
<Some other inputs>
)
)
)
这种情况下的输出如下: Snapshot of the new output
我在这里缺少什么?我想在运行时生成数据框列表,并且不希望有预定义的列表。
答案 0 :(得分:2)
我会以下列方式做到这一点:
global.R
根据Shiny Scooping Rules使用global.R
将使对象可用于服务器和ui。
dfA <- mtcars
dfB <- airquality
# List dfs
temp = ls()[sapply(ls(), function(x) class(get(x))) == 'data.frame']
调用temp
的默认Shiny app示例如下所示。
ui.R
fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
# Listing DFs
selectInput(
inputId = "tableName", label = "Select Table:",
selected = NULL, multiple = FALSE,
choices = c("Select Input", temp)
),
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
server.R
function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}