我有一个闪亮的应用程序,允许用户通过从下拉列表中选择列名并为其分配值来对数据框进行子集化。在下面的代码段中,df1是一个数据框,如果用户从下拉列表中选择“country”并在文本框中输入“UK”,则临时df,temp_dat应该只包含UK的值。由于某种原因,我无法理解它说明已返回0行3列。有人可以帮我这个吗?提前谢谢。
library(shinydashboard)
ui = dashboardPage(dashboardHeader(),
dashboardSidebar(),
dashboardBody(uiOutput("subset_check1"), uiOutput("subsetbox1")))
server = function(input, output, session)
{
names = c("Jane", "Doe", "Job", "Right")
emp_id = c(1000, 1001, 1002, 1003)
country = c("UK", "UK", "UK", "US")
df1 = data.frame(names, emp_id, country)
file1_cols = colnames(df1)
output$subset_check1 = renderUI(checkboxInput(
"subcheck1",
"Would you like to subset this dataset with a condition?"
))
observe({
if (!is.null(input$subcheck1))
{
if (input$subcheck1 == "TRUE")
{
#print("box was checked")
output$subsetbox1 = renderUI(flowLayout(
selectInput(
"subsetbox1sel",
"Select column:",
choices = c(file1_cols),
multiple = T
),
textInput("subsetbox1text", "Enter value")
))
observe({
if (!is.null(input$subsetbox1sel))
{
if (!is.null(input$subsetbox1text))
{
cols_sel1 = as.character(input$subsetbox1sel)
vals_sel1 = as.character(input$subsetbox1text)
temp_dat = df1[df1$cols_sel1 == vals_sel1, ]
print(temp_dat)
}
}
})
}
}
})
}
答案 0 :(得分:3)
Currently, there is no column named cols_sel1 in the dataframe. But you intend to pass its string literal value. Hence, do not use the dollar $
qualifier but a string to reference the column (i.e., use latter of below) especially since this cols_sel1 varies with user selection:
df$col
df['col']
So simply adjust your temp_dat
line to:
temp_dat = df1[df1[cols_sel1]==vals_sel1,]