【问题标题】:How to mutate a dataset using an arithmetical expression in R in shinyR如何在闪亮的 R 中使用 R 中的算术表达式来改变数据集
【发布时间】:2018-09-03 14:47:00
【问题描述】:

我正在尝试创建一个闪亮的应用程序,它接受公式作为输入参数并相应地改变文件。

但我做不到,结果是打印公式。感谢是否有人可以调查该问题并提出解决方案。

library(shiny)
library(plyr)
library(dplyr)
library(DT)
library(data.table)

 ui <- pageWithSidebar(
    headerPanel = headerPanel('data'),
    sidebarPanel = sidebarPanel(#         fileInput(
            #         'mtcars', h4('Uplaodmtcardata in csv format')
            # ),
            uiOutput('formula')),
    mainPanel(dataTableOutput("data"))
)

server <- function(input, output, session) {
    mtcarsFile <- reactive({
            input$mtcars
    })


    xxmtcars <-
            reactive({
                    as.data.table(mtcars)
            })



    output$formula <- renderUI({
            textInput('formula',
                      h5('formula'))

    })
    formulaPars <- reactive({
            !!(input$formula)
    })

    newCol  = reactive({
            quo(formulaPars())
    })

    output$data <- renderDataTable({
            as.data.table(mutate(xxmtcars(), cyl + (!!newCol())))


    })

}

 runApp(list(ui = ui, server = server))

【问题讨论】:

    标签: r shiny dplyr


    【解决方案1】:

    可以使用rlang::parse_expr() 更改代码,如下所示以获得所需的输出:

    server <- function(input, output, session) {
    
      mtcarsFile <- reactive({
        input$mtcars
      })
    
      xxmtcars <-
        reactive({
          if (!is.null(input$mtcars)) {
            as.data.table(read.csv(input$mtcars$datapath))
          } 
      })
    
      output$formula <- renderUI({
        textInput('formula',  h5('formula'))
    
      })
    
      formulaPars <- reactive({
        (input$formula)
      })
    
      newCol  = reactive({
        formula = formulaPars()
        if (formula != '') {
          rlang::parse_expr(formula)
        }
      })
    
      output$data <- renderDataTable({
        data <- xxmtcars()
        if (!is.null(data) && input$formula != '') {
          as.data.table(mutate(data , cyl + !!newCol()))
        }
      })  
    }
    
    runApp(list(ui = ui, server = server))
    

    【讨论】:

    • 太棒了..非常感谢您的帮助。它正在工作。
    猜你喜欢
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 2015-03-09
    • 1970-01-01
    • 2022-10-31
    相关资源
    最近更新 更多