【发布时间】:2019-05-07 15:50:43
【问题描述】:
希望获得一些专业知识。以下代码 sn-p 执行以下操作:
- 允许用户从 CSV 文件中选择他们想要的变量(列),然后为每个变量生成数字输入字段。
- 用用户输入的值填充数据框。
但是,Shiny 将列标题分配给数据框,我尝试了所有能找到的方法来更改它们,但似乎没有任何效果。
谁能告诉我我做错了什么?
df_sel() - 这是选择变量的函数
这是 R.UI 部分
ui <- fluidPage(
# App title ----
titlePanel(title = h1("Variable Selection Example", align = "center")),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Select a file ----
fileInput("uploaded_file", "Choose CSV File",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
# Horizontal line ---- This allows the user to create a bunch of repeated values for the numerica inputs they later create
sliderInput("months", "Forecast Months:",
min = 0, max = 60,
value = 1),
tags$hr(),
# Input: Checkbox if file has header ----
checkboxInput("header", "Header", TRUE),
# Input: Select separator ----
radioButtons("sep", "Separator",
choices = c(Semicolon = ";",
Comma = ",",
Tab = "\t"),
selected = ","),
# Horizontal line ----
tags$hr(),
# Input: Select number of rows to display ----
radioButtons("disp", "Display",
choices = c(All = "all",
Head = "head"),
selected = "all"),
# Select variables to display ----
uiOutput("checkbox")
),
# Main panel for displaying outputs ----
mainPanel(
uiOutput("input_ui"), #numeric inputs
tableOutput("table1")) #table to display input values
)
)
这是在 R.Server 部分
server <- function(input, output, session) {
#assign csv file to dataframe df
df <- reactive({
req(input$uploaded_file)
read.csv(input$uploaded_file$datapath,
header = input$header,
sep = input$sep)
})
# Dynamically generate UI input when data is uploaded ----
output$checkbox <- renderUI({
checkboxGroupInput(inputId = "select_var",
label = "Select variables",
choices = setdiff(names(df()), input$select_dev),
selected = setdiff(names(df()), input$select_dev))
})
# Select columns to print ----
df_sel <- reactive({
req(input$select_var)
df_sel <- df() %>% select(input$select_var)
})
output$input_ui <- renderUI({ #this creates dynamic numeric inputs based on the variables selected by the user
pvars <- df_sel()
varn = names(df_sel())
lapply(seq(pvars), function(i) {
numericInput(inputId = paste0("range", pvars[i]),
label = varn,
value = 0)
})
})
numbers <- reactive({ #this creates a reactive dataframe for the numbers
pvars <- df_sel()
num = as.integer(ncol(pvars))
print(num)
pred <- data.frame(lapply(1:num, function(i) {
input[[paste0("range", pvars[i])]]
}))
n = input$months #pull number from that slider up in the UI section
pd = data.frame(pred, i=rep(1:n,ea=NROW(input$months)))
pd[1:(length(pd)-1)]
#colnames(pd, c(df_sel())) #this does not seem to work at all!!!
})
output$table1 <- renderTable({
numbers()
fv = numbers()
print(dim(fv)) #check the dimensions of the table
print(fv) # chcek the table is populating correctly.
#df1 <- fv #show the table
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)
【问题讨论】:
-
如果您可以提供一个最小的工作示例(即,还包括 UI 部分和一些数据),那么测试和回答这个问题会容易得多。
-
嗨 Yanirmor,我已经放置了最小的工作代码来生成我所询问的函数。如果有更好的方法来实现我想要做的事情,请告诉我。感谢您以及其他任何可以提供帮助的人的帮助和反馈!