Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 创建新数据帧-用一列减去另一列_R_Shiny - Fatal编程技术网

R 创建新数据帧-用一列减去另一列

R 创建新数据帧-用一列减去另一列,r,shiny,R,Shiny,我正在尝试制作一个闪亮的应用程序,用户可以选择列从另一列中减去一列,并编写列的名称。我被困在打印数据文件,可能是因为光泽不想考虑列的方式,我想。有人知道如何按输入变量选择列吗 通常,我只想在df中包含用户指定的样本,比如在提供的打印屏幕中(应忽略空的样本3/4)。有人对如何处理这个问题有什么建议吗 以下是我代码的一部分: library(shiny) ui <- fluidPage( sidebarLayout( sidebarPanel( fluidRow

我正在尝试制作一个闪亮的应用程序,用户可以选择列从另一列中减去一列,并编写列的名称。我被困在打印数据文件,可能是因为光泽不想考虑列的方式,我想。有人知道如何按输入变量选择列吗

通常,我只想在df中包含用户指定的样本,比如在提供的打印屏幕中(应忽略空的样本3/4)。有人对如何处理这个问题有什么建议吗

以下是我代码的一部分:

    library(shiny)
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fluidRow(
        column(
          width = 3,
          div(style = "white-space: nowrap;", 
              div(style = "white-space: nowrap;", 

                  h5(textInput("name1", label = "Sample 1 Name", value = "Enter name..."),style="display: inline-block; width: 100%;"),
                  h5(selectInput(inputId = "sam1", label = "Sample 1",c(),  multiple = FALSE, selectize = TRUE),style="display:inline-block; width: 100%;"),
                  h5(selectInput(inputId = "bla1", label = "Blank 1",c(), multiple = FALSE, selectize = TRUE),style="display:inline-block; width: 100%;")
              ),
              div(style = "white-space: nowrap;",

                  h5(textInput("name2", label = "Sample 2 Name", value = "Enter name..."),style="display: inline-block; width: 100%;"),
                  h5(selectInput(inputId = "sam2", label = "Sample 2",c(),  multiple = FALSE, selectize = TRUE),style="display:inline-block; width: 100%;"),
                  h5(selectInput(inputId = "bla2", label = "Blank 2",c(), multiple = FALSE, selectize = TRUE),style="display:inline-block; width: 100%;")
              ),
              div(style = "white-space: nowrap;",

                  h5(textInput("name3", label = "Sample 3 Name", value = "Enter name..."),style="display: inline-block; width: 100%;"),
                  h5(selectInput(inputId = "sam3", label = "Sample 3",c(),  multiple = FALSE, selectize = TRUE),style="display:inline-block; width: 100%;"),
                  h5(selectInput(inputId = "bla3", label = "Blank 3",c(), multiple = FALSE, selectize = TRUE),style="display:inline-block; width: 100%;")
              ),
              div(style = "white-space: nowrap;",


                  h5(textInput("name4", label = "Sample 4 Name", value = "Enter name..."),style="display: inline-block; width: 100%;"),
                  h5(selectInput(inputId = "sam4", label = "Sample 4",c(),  multiple = FALSE, selectize = TRUE),style="display:inline-block; width: 100%;"),
                  h5(selectInput(inputId = "bla4", label = "Blank 4",c(), multiple = FALSE, selectize = TRUE),style="display:inline-block; width: 100%;")
              ),
              actionButton("update", "Update", class = "btn-primary",style='padding:4px; font-size:120%')
          )))),
    mainPanel(
      DT::dataTableOutput("contents"),
      plotOutput("plot_preview", height = "auto")
    )))

server <- function(input, output, session) {

  Layout <- c("A", " B", " A", "B")

  col1 <- c(0.84, 0.65, 0.97, 0.81)
  col2 <- c(0.43,0.55,0.53,0.66)
  col3 <- c(0.74, 0.75, 0.87, 0.71)

  df <- data.frame(Layout, col1, col2, col3) 

  cols <- colnames(df) 
  cols <- c("NULL", cols[2:4])

  updateSelectInput(session, "sam1", choices=cols)
  updateSelectInput(session, "sam2", choices=cols)
  updateSelectInput(session, "sam3", choices=cols)
  updateSelectInput(session, "sam4", choices=cols)
  updateSelectInput(session, "bla1", choices=cols)
  updateSelectInput(session, "bla2", choices=cols)
  updateSelectInput(session, "bla3", choices=cols)
  updateSelectInput(session, "bla4", choices=cols)

 ## take a colum choosed before and substract the blank - save as one column 
 observeEvent(input$update, 
              {mydatanew <- reactive(
                mydatanew <- data.frame(input$name1 = input$sam1 - input$bla1, input$name2 = input$sam2 - input$bla2))

              output$contents2 <- DT::renderDataTable( DT::datatable(mydatanew()))
             }
              )


  output$contents <- DT::renderDataTable(df)

}

shinyApp(ui, server)
库(闪亮)

ui您可以使用
reactiveVal
保存数据帧并对其进行更新。使用
observeEvent
添加列并将修改后的数据帧存储回reactiveVal

另外,为了让事情变得更简单,您可以将输入调用为
input[[x]]
,其中x是一个字符串。因此,我们可以循环输入,而不是键入所有内容。您也可以将其用于updateSelectInput语句。按下
操作按钮
后,重新设置
选择输入
元素也会很好。您可以通过放置像
lappy(1:4,函数(x){updateSelectInput(session,paste0('bla',x),selected='NULL'))这样的行来实现这一点
在观察事件的末尾

下面是一个工作示例。希望这有帮助



库(闪亮)

ui不能对变量列名使用
$
,但可以使用
[[
。建议的重复项。(至少部分重复-不管背景阅读如何。)另外,我不确定您在
mydatanew中要做什么,是否应该有一个单独的
contents2
输出?或者您是否正在尝试替换原始表?我不确定这里想要的行为是什么。
library(shiny)
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fluidRow(
        column(
          width = 3,
          div(style = "white-space: nowrap;", 
              div(style = "white-space: nowrap;", 

                  h5(textInput("name1", label = "Sample 1 Name", value = "Enter name..."),style="display: inline-block; width: 100%;"),
                  h5(selectInput(inputId = "sam1", label = "Sample 1",c(),  multiple = FALSE, selectize = TRUE),style="display:inline-block; width: 100%;"),
                  h5(selectInput(inputId = "bla1", label = "Blank 1",c(), multiple = FALSE, selectize = TRUE),style="display:inline-block; width: 100%;")
              ),
              div(style = "white-space: nowrap;",

                  h5(textInput("name2", label = "Sample 2 Name", value = "Enter name..."),style="display: inline-block; width: 100%;"),
                  h5(selectInput(inputId = "sam2", label = "Sample 2",c(),  multiple = FALSE, selectize = TRUE),style="display:inline-block; width: 100%;"),
                  h5(selectInput(inputId = "bla2", label = "Blank 2",c(), multiple = FALSE, selectize = TRUE),style="display:inline-block; width: 100%;")
              ),
              div(style = "white-space: nowrap;",

                  h5(textInput("name3", label = "Sample 3 Name", value = "Enter name..."),style="display: inline-block; width: 100%;"),
                  h5(selectInput(inputId = "sam3", label = "Sample 3",c(),  multiple = FALSE, selectize = TRUE),style="display:inline-block; width: 100%;"),
                  h5(selectInput(inputId = "bla3", label = "Blank 3",c(), multiple = FALSE, selectize = TRUE),style="display:inline-block; width: 100%;")
              ),
              div(style = "white-space: nowrap;",


                  h5(textInput("name4", label = "Sample 4 Name", value = "Enter name..."),style="display: inline-block; width: 100%;"),
                  h5(selectInput(inputId = "sam4", label = "Sample 4",c(),  multiple = FALSE, selectize = TRUE),style="display:inline-block; width: 100%;"),
                  h5(selectInput(inputId = "bla4", label = "Blank 4",c(), multiple = FALSE, selectize = TRUE),style="display:inline-block; width: 100%;")
              ),
              actionButton("update", "Update", class = "btn-primary",style='padding:4px; font-size:120%')
          )))),
    mainPanel(
      DT::dataTableOutput("contents"),
      plotOutput("plot_preview", height = "auto")
    )))

server <- function(input, output, session) {

  Layout <- c("A", " B", " A", "B")

  col1 <- c(0.84, 0.65, 0.97, 0.81)
  col2 <- c(0.43,0.55,0.53,0.66)
  col3 <- c(0.74, 0.75, 0.87, 0.71)

  df <- data.frame(Layout, col1, col2, col3) 

  cols <- colnames(df) 
  cols <- c("NULL", cols[2:4])

  updateSelectInput(session, "sam1", choices=cols)
  updateSelectInput(session, "sam2", choices=cols)
  updateSelectInput(session, "sam3", choices=cols)
  updateSelectInput(session, "sam4", choices=cols)
  updateSelectInput(session, "bla1", choices=cols)
  updateSelectInput(session, "bla2", choices=cols)
  updateSelectInput(session, "bla3", choices=cols)
  updateSelectInput(session, "bla4", choices=cols)

  reval_df <- reactiveVal(df)

  ## take a colum choosed before and substract the blank - save as one column 
  observeEvent(input$update, {
    df <- reval_df()
    for (i in 1:4)
    {
      if(input[[paste0('sam',i)]]!='NULL' & input[[paste0('bla',i)]]!='NULL')
      {
        print(i)
        df[[input[[paste0('name',i)]]]] = df[[input[[paste0('sam',i)]]]]- df[[input[[paste0('bla',i)]]]]
      }
    }
    reval_df(df)

    # reset inputs
    lapply(1:4,function(x) {updateSelectInput(session,paste0('bla',x),selected='NULL')})
    lapply(1:4,function(x) {updateSelectInput(session,paste0('name',x),selected='NULL')})
    lapply(1:4,function(x) {updateSelectInput(session,paste0('sam',x),selected='NULL')})


  })


  output$contents <- DT::renderDataTable(reval_df())

}

shinyApp(ui, server)