【问题标题】:Shiny/R - Unable to rename column headersShiny/R - 无法重命名列标题
【发布时间】:2019-05-07 15:50:43
【问题描述】:

希望获得一些专业知识。以下代码 sn-p 执行以下操作:

  1. 允许用户从 CSV 文件中选择他们想要的变量(列),然后为每个变量生成数字输入字段。
  2. 用用户输入的值填充数据框。

但是,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,我已经放置了最小的工作代码来生成我所询问的函数。如果有更好的方法来实现我想要做的事情,请告诉我。感谢您以及其他任何可以提供帮助的人的帮助和反馈!

标签: r shiny


【解决方案1】:

我想出了一个解决我自己问题的方法。如果有人可以改进它,请告诉我。此代码位于 R.Server 部分。

 #This creates sliders from the selected variables from the reactive function called
 #"df_sel()".  Note the use of "tagList".  The RenderUI function below creates as 
 #many sliders as variables selected, and passes in the correct variable name.  
 #It selects the last data value from each column, since this is time series data,
 #the last data value \ (most recent) was desired.  

 output$scplan <- renderUI({  
    vars <- df_sel()
    n = nrow(vars)
    tagList(lapply(colnames(vars), function(z) {
       sliderInput(
       sprintf("%s",z),
       label = z,
       min = ceiling(min(vars[[z]])), # min value is the minimum of the column
       max = ceiling(max(vars[[z]])), # max is the max of the column
       value = vars[[z]][[n]])
    }))     


 #this reactive function creates a dataframe from variables that were selected from 
 #checkboxes.  The user moves the sliders to generate the values, and the code 
 #repeats the values for as many "input$months" as were selected. 

 sp_numbers <- reactive({
   vars <- df_sel()
   num = as.integer(ncol(vars))
   sp_pred <- data.frame(lapply(colnames(vars), function(z) {
   input[[z]]
     }))
   names(sp_pred) <- colnames(vars)
   n = input$sp_months
   df_sp_pred = data.frame(sp_pred, z=rep(z:n,ea=NROW(input$sp_months)))
   df_sp_pred[1:(length(df_sp_pred)-1)] #this removes the last column which just shows the repeat count
  })

#this code renders the table of the dataframe created above. 
output$spo_table <- renderTable({ 
  sp_numbers()
  })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-11
    • 2018-04-24
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-23
    • 2019-01-23
    相关资源
    最近更新 更多